#define maxSamples 50 #define backoff maxSamples+50 #define offset 36 int maxValue[6]; int threshold[6] ={40,30,45,40,20,45}; int counter[6] = {-1,-1,-1,-1,-1,-1}; int maximum[6] = {-1,-1,-1,-1,-1,-1}; void setup() { // put your setup code here, to run once: analogReference(INTERNAL); } void loop() { // put your main code here, to run repeatedly: for (int pad = 0;pad<6;pad++) { int value = analogRead(pad); if (counter[pad] < 0) { if (value > threshold[pad]) { maxValue[pad] = value; counter[pad] = 0; } } else { counter[pad]++; if(value> maxValue[pad]) maxValue[pad] = value; if(counter[pad] == maxSamples) { if (maxValue[pad] > maximum[pad]) maximum[pad] = maxValue[pad]; float velocity = maxValue[pad] - threshold[pad]; velocity *= 127; velocity /= maximum[pad] - threshold[pad]; MIDIEvent noteOn{0x09, 0x91, pad + offset, velocity}; MIDIUSB.write(noteOn); MIDIUSB.flush(); } if (counter[pad] == backoff) counter[pad] = -1; } } }