// the MIDI channel number to send messages const int channel = 1; // the MIDI continuous controller for each analog input const int controllerA0 = 1; // const int controllerA1 = 2; // const int controllerA2 = 3; // const int controllerA3 = 4; // const int controllerA4 = 5; // const int controllerA5 = 6; // const int controllerA6 = 7; // const int controllerA7 = 8; // const int controllerA8 = 9; // const int controllerA9 = 10; // void setup() { } // store previously sent values, to detect changes int previousA0 = -1; int previousA1 = -1; int previousA2 = -1; int previousA3 = -1; int previousA4 = -1; int previousA5 = -1; int previousA6 = -1; int previousA7 = -1; int previousA8 = -1; int previousA9 = -1; elapsedmillis msec = 0; void loop() { // only check the analog inputs 50 times per second, // to prevent a flood of MIDI messages if (msec >= 50) { msec = 0; int n0 = analogRead(A0) / 8; int n1 = analogRead(A1) / 8; int n2 = analogRead(A2) / 8; int n3 = analogRead(A3) / 8; int n4 = analogRead(A4) / 8; int n5 = analogRead(A5) / 8; int n6 = analogRead(A6) / 8; int n7 = analogRead(A7) / 8; int n8 = analogRead(A8) / 8; int n9 = analogRead(A9) / 8; // only transmit MIDI messages if analog input changed if (n0 != previousA0) { usbMIDI.sendControlChange(controllerA0, n0, channel); previousA0 = n0; } if (n1 != previousA1) { usbMIDI.sendControlChange(controllerA1, n1, channel); previousA1 = n1; } if (n2 != previousA2) { usbMIDI.sendControlChange(controllerA2, n2, channel); previousA2 = n2; } if (n3 != previousA3) { usbMIDI.sendControlChange(controllerA3, n3, channel); previousA3 = n3; } if (n4 != previousA4) { usbMIDI.sendControlChange(controllerA4, n4, channel); previousA4 = n4; } if (n5 != previousA5) { usbMIDI.sendControlChange(controllerA5, n5, channel); previousA5 = n5; } if (n6 != previousA6) { usbMIDI.sendControlChange(controllerA6, n6, channel); previousA6 = n6; } if (n7 != previousA7) { usbMIDI.sendControlChange(controllerA7, n7, channel); previousA7 = n7; } if (n8!= previousA8) { usbMIDI.sendControlChange(controllerA8, n8, channel); previousA8 = n8; } if (n9 != previousA9) { usbMIDI.sendControlChange(controllerA9, n9, channel); previousA9 = n9; } } // MIDI Controllers should discard incoming MIDI messages. // http://forum.pjrc.com/threads/24179-Teensy-3-Ableton-Analog-CC-causes-midi-crash while (usbMIDI.read()) { // ignore incoming messages } }