#include <MIDI.h>
const int InputCount = 4;
int piezo[InputCount] = {A9, A10, A11, A12};
int note[InputCount] = {50, 51, 52, 53};
const int threshold = 50; //anything over fifty means we've hit the piezo
void setup() {
MIDI.begin(1);
}
void loop() {
int sensorReading[InputCount];
for(int i=0; i<InputCount; i++) {
sensorReading[i]= analogRead(i);
if (sensorReading[i] >= threshold) {
int maxPiezoVal = getMaxVal(i, sensorReading[i]);
byte velocity = map(maxPiezoVal, 0, 1023, 0, 127);//velocity between 50 and 127 based on max val from piezo
MIDI.sendNoteOn(note[i], velocity, 1);
delay(10);
MIDI.sendNoteOff(note[i], 0, 1);
delay(100);
}
}
}
int getMaxVal(int pin, int lastVal) {
int currentVal = analogRead(pin);
while (currentVal>lastVal){
lastVal = currentVal;
currentVal = analogRead(pin);
}
return lastVal;
}