Permalink
Cannot retrieve contributors at this time
| // Trellis M4 MIDI Keypad CC | |
| // sends 32 notes, pitch bend & a CC from accelerometer tilt over USB MIDI | |
| #include <Adafruit_Sensor.h> | |
| #include <Adafruit_ADXL343.h> | |
| #include <Adafruit_NeoTrellisM4.h> | |
| #define MIDI_CHANNEL 0 // default channel # is 0 | |
| // Set the value of first note, C is a good choice. Lowest C is 0. | |
| // 36 is a good default. 48 is a high range. Set to 24 for a bass machine. | |
| #define FIRST_MIDI_NOTE 36 | |
| Adafruit_ADXL343 accel = Adafruit_ADXL343(123, &Wire1); | |
| int xCC = 1; //choose a CC number to control with x axis tilting of the board. 1 is mod wheel, for example. | |
| int last_xbend = 0; | |
| int last_ybend = 0; | |
| Adafruit_NeoTrellisM4 trellis = Adafruit_NeoTrellisM4(); | |
| void setup(){ | |
| Serial.begin(115200); | |
| //while (!Serial); | |
| Serial.println("MIDI keypad & pitchbend!"); | |
| trellis.begin(); | |
| trellis.setBrightness(80); | |
| // USB MIDI messages sent over the micro B USB port | |
| Serial.println("Enabling MIDI on USB"); | |
| trellis.enableUSBMIDI(true); | |
| trellis.setUSBMIDIchannel(MIDI_CHANNEL); | |
| // UART MIDI messages sent over the 4-pin STEMMA connector (3.3V logic) | |
| Serial.println("Enabling MIDI on UART"); | |
| trellis.enableUARTMIDI(true); | |
| trellis.setUARTMIDIchannel(MIDI_CHANNEL); | |
| if(!accel.begin()) { | |
| Serial.println("No accelerometer found"); | |
| while(1); | |
| } | |
| } | |
| void loop() { | |
| // put your main code here, to run repeatedly: | |
| trellis.tick(); | |
| while (trellis.available()){ | |
| keypadEvent e = trellis.read(); | |
| int key = e.bit.KEY; | |
| Serial.print("Keypad key: "); Serial.println(key); | |
| Serial.print("MIDI note: "); Serial.println(FIRST_MIDI_NOTE+key); | |
| if (e.bit.EVENT == KEY_JUST_PRESSED) { | |
| Serial.println(" pressed\n"); | |
| trellis.setPixelColor(key, 0xFFFFFF); | |
| trellis.noteOn(FIRST_MIDI_NOTE+key, 64); | |
| } | |
| else if (e.bit.EVENT == KEY_JUST_RELEASED) { | |
| Serial.println(" released\n"); | |
| trellis.setPixelColor(key, 0x0); | |
| trellis.noteOff(FIRST_MIDI_NOTE+key, 64); | |
| } | |
| } | |
| // Check for accelerometer | |
| sensors_event_t event; | |
| accel.getEvent(&event); | |
| /* Display the results (acceleration is measured in m/s^2) */ | |
| //Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print(" "); | |
| //Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print(" "); | |
| //Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.print(" ");Serial.println("m/s^2 "); | |
| int xbend = 0; | |
| int ybend = 0; | |
| if (abs(event.acceleration.y) < 2.0) { // 2.0 m/s^2 | |
| // don't make any bend unless they've really started moving it | |
| ybend = 8192; // 8192 means no bend | |
| } else { | |
| if (event.acceleration.y > 0) { | |
| ybend = ofMap(event.acceleration.y, 2.0, 10.0, 8192, 0, true); // 2 ~ 10 m/s^2 is downward bend | |
| } else { | |
| ybend = ofMap(event.acceleration.y, -2.0, -10.0, 8192, 16383, true); // -2 ~ -10 m/s^2 is upward bend | |
| } | |
| } | |
| if (ybend != last_ybend) { | |
| Serial.print("Y pitchbend: "); Serial.println(ybend); | |
| trellis.pitchBend(ybend); | |
| last_ybend = ybend; | |
| } | |
| if (abs(event.acceleration.x) < 2.0) { // 2.0 m/s^2 | |
| // don't make any bend unless they've really started moving it | |
| xbend = 0; | |
| } else { | |
| if (event.acceleration.x > 0) { | |
| xbend = ofMap(event.acceleration.x, 2.0, 10.0, 0, 127, true); // 2 ~ 10 m/s^2 is upward bend | |
| } else { | |
| xbend = ofMap(event.acceleration.x, -2.0, -10.0, 0, 127, true); // -2 ~ -10 m/s^2 is downward bend | |
| } | |
| } | |
| if (xbend != last_xbend) { | |
| Serial.print("X mod: "); Serial.println(xbend); | |
| trellis.controlChange(xCC, xbend); //xCC is set at top of sketch. e.g., CC 1 is Mod Wheel | |
| last_xbend = xbend; | |
| } | |
| trellis.sendMIDI(); // send any pending MIDI messages | |
| delay(10); | |
| } | |
| // floating point map | |
| float ofMap(float value, float inputMin, float inputMax, float outputMin, float outputMax, bool clamp) { | |
| float outVal = ((value - inputMin) / (inputMax - inputMin) * (outputMax - outputMin) + outputMin); | |
| if (clamp) { | |
| if (outputMax < outputMin) { | |
| if (outVal < outputMax) outVal = outputMax; | |
| else if (outVal > outputMin) outVal = outputMin; | |
| } else { | |
| if (outVal > outputMax) outVal = outputMax; | |
| else if (outVal < outputMin) outVal = outputMin; | |
| } | |
| } | |
| return outVal; | |
| } | |
| // 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); | |
| } |