Created
August 29, 2019 16:34
-
-
Save benevpi/6996c259f38077d6cf86f5b353b1bb4f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Adafruit_NeoTrellisM4.h" | |
#include <Audio.h> | |
//audio setup from the audio tool | |
AudioSynthSimpleDrum drum3; //xy=76,123 | |
AudioSynthSimpleDrum drum4; //xy=77,181 | |
AudioSynthSimpleDrum drum1; //xy=78,31 | |
AudioSynthSimpleDrum drum2; //xy=78,77 | |
AudioMixer4 mixer1; //xy=246,100 | |
AudioEffectBitcrusher bitcrusher1; //xy=295,221 | |
AudioEffectReverb reverb1; //xy=395,125 | |
AudioMixer4 mixer2; //xy=483,222 | |
AudioMixer4 mixer3; //xy=647,200 | |
AudioOutputAnalogStereo dacs1; //xy=766,115 | |
AudioConnection patchCord1(drum3, 0, mixer1, 2); | |
AudioConnection patchCord2(drum4, 0, mixer1, 3); | |
AudioConnection patchCord3(drum1, 0, mixer1, 0); | |
AudioConnection patchCord4(drum2, 0, mixer1, 1); | |
AudioConnection patchCord5(mixer1, bitcrusher1); | |
AudioConnection patchCord6(bitcrusher1, reverb1); | |
AudioConnection patchCord7(bitcrusher1, 0, mixer2, 1); | |
AudioConnection patchCord8(reverb1, 0, mixer2, 0); | |
AudioConnection patchCord9(mixer2, 0, mixer3, 0); | |
AudioConnection patchCord10(mixer3, 0, dacs1, 0); | |
AudioConnection patchCord11(mixer3, 0, dacs1, 1); | |
// | |
// The NeoTrellisM4 object is a keypad and neopixel strip subclass | |
// that does things like auto-update the NeoPixels and stuff! | |
Adafruit_NeoTrellisM4 trellis = Adafruit_NeoTrellisM4(); | |
boolean *lit_keys; | |
int timetobeat = 750; | |
int beatcounter = 3; | |
int lastbeat = 0; | |
int beatkeys[] = {3,11,19,27}; | |
int drum1keys[] = {0,8,16,24}; | |
int drum2keys[] = {1,9,17,25}; | |
int drum3keys[] = {2,10,18,26}; | |
int bitcrushbitkeys[] = {4,12,20,28}; | |
int bitcrushsamplekeys[] = {5,13,21,29}; | |
int reverbkeys[] = {6,14,22,30}; | |
int ampkeys[] = {7,15,23,31}; | |
void setup(){ | |
Serial.begin(115200); | |
AudioMemory(120); | |
trellis.begin(); | |
trellis.setBrightness(80); | |
bitcrusher1.bits(16); | |
bitcrusher1.sampleRate(44100); | |
reverb1.reverbTime(0); | |
mixer2.gain(0,0); | |
mixer2.gain(1,1); | |
mixer3.gain(0,1); | |
drum1.frequency(40); | |
drum2.frequency(80); | |
drum3.frequency(160); | |
Serial.println("Bens effect drum machine"); | |
lit_keys = new boolean[trellis.num_keys()]; | |
for (int i=0; i<trellis.num_keys(); i++) { | |
lit_keys[i] = false; | |
} | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
trellis.tick(); | |
//has a beat taken place | |
if (millis() > lastbeat + timetobeat) { | |
lastbeat = millis(); | |
beatcounter++; | |
if (beatcounter > 3) { beatcounter = 0; } | |
//light up the beat keys | |
for (int i = 0; i<4; i++) { | |
trellis.setPixelColor(beatkeys[i], 0); | |
} | |
trellis.setPixelColor(beatkeys[beatcounter], trellis.Color(50, 50, 50)); | |
//drums | |
if (lit_keys[drum1keys[beatcounter]]) { | |
drum1.noteOn(); | |
} | |
if (lit_keys[drum2keys[beatcounter]]) { | |
drum2.noteOn(); | |
} | |
if (lit_keys[drum3keys[beatcounter]]) { | |
drum3.noteOn(); | |
} | |
//bitcrusher | |
if(lit_keys[bitcrushbitkeys[beatcounter]]) { | |
bitcrusher1.bits(2); | |
} | |
else { | |
bitcrusher1.bits(16); | |
} | |
if(lit_keys[bitcrushsamplekeys[beatcounter]]) { | |
bitcrusher1.sampleRate(8000); | |
} | |
else { | |
bitcrusher1.sampleRate(44100); | |
} | |
//reverb | |
if(lit_keys[reverbkeys[beatcounter]]) { | |
mixer2.gain(0,1); | |
mixer2.gain(1,0); | |
} | |
else { | |
mixer2.gain(0,0); | |
mixer2.gain(1,1); | |
} | |
} | |
//amp | |
if(lit_keys[ampkeys[beatcounter]]) { | |
mixer3.gain(0,3); | |
} | |
else { | |
mixer3.gain(0,0.5); | |
} | |
while (trellis.available()){ | |
keypadEvent e = trellis.read(); | |
if (e.bit.EVENT == KEY_JUST_PRESSED) { | |
int key = e.bit.KEY; // shorthand for what was pressed | |
if(key != 3 and key != 11 and key != 19 and key != 27) { // ignore it if it's a sequence led | |
Serial.print(key); Serial.println(" pressed"); | |
lit_keys[key] = !lit_keys[key]; | |
if (lit_keys[key]) { | |
trellis.setPixelColor(key, Wheel(random(255))); | |
} else { | |
trellis.setPixelColor(key, 0); | |
} | |
} | |
} | |
} | |
delay(10); | |
} | |
// Input a value 0 to 255 to get a color value. | |
// The colours are a transition r - g - b - back to r. | |
uint32_t Wheel(byte WheelPos) { | |
WheelPos = 255 - WheelPos; | |
if(WheelPos < 85) { | |
return trellis.Color(255 - WheelPos * 3, 0, WheelPos * 3); | |
} | |
if(WheelPos < 170) { | |
WheelPos -= 85; | |
return trellis.Color(0, WheelPos * 3, 255 - WheelPos * 3); | |
} | |
WheelPos -= 170; | |
return trellis.Color(WheelPos * 3, 255 - WheelPos * 3, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment