#define nDrums 5 const int drumNotes[nDrums] = { 36, 38, 42, 46, 49 }; const int drumChannel = 9; const int lowThreshold = 200, highThreshold = 600; const long period = 100; const int ledPin = 13; void setup() { Serial.begin(31250); pinMode(ledPin, OUTPUT); digitalWrite(ledPin, HIGH); } void midiMsg(byte cmd, byte data1, byte data2) { Serial.print(cmd, BYTE); Serial.print(data1, BYTE); Serial.print(data2, BYTE); } void noteOn(byte channel, byte note, byte velocity) { midiMsg(0x90 | channel, note, velocity); } void noteOff(byte channel, byte note, byte velocity) { midiMsg(0x80 | channel, note, velocity); } void loop() { bool on[nDrums]; long last[nDrums]; for (int i = 0; i < nDrums; i++) { on[i] = false; last[i] = 0; } while (true) { int val[nDrums]; long now = millis(); for (int i = 0; i < nDrums; i++) { val[i] = analogRead(i); } for (int i = 0; i < nDrums; i++) { if (on[i] && (now - last[i] > period)) { noteOff(drumChannel, drumNotes[i], 64); on[i] = false; } else if (!on[i] && val[i] >= lowThreshold) { if (val[i] > highThreshold) val[i] = highThreshold; long v = 127L * (val[i] - lowThreshold); noteOn(drumChannel, drumNotes[i], v / (highThreshold - lowThreshold)); on[i] = true; last[i] = now; } } } }