Howdy, im fairly new with Arduino and I am currently building a MIDI controller. I have one peizo sensor working great and I figured adding additional sensors would be easy enough, unfortunatley this has not been the case and any attempts i have made so far have resulted in error messages
Here is what i have so far
int noteOn = 144;
int piezo = A0;
int threshold = 50;//anything over fifty means we've hit the piezo
void setup(){
Serial.begin(9600);
}
void loop(){
int piezoVal = analogRead(piezo);
if (piezoVal>threshold){
int maxPiezoVal = getMaxVal(piezoVal);
byte velocity = map(maxPiezoVal, 0, 1023, 50, 127);//velocity between 50 and 127 based on max val from piezo
MIDImessage(noteOn, 60, velocity);
delay(500);
MIDImessage(noteOn, 60, 0);
}
}
//send MIDI message
void MIDImessage(byte command, byte data1, byte data2) {
Serial.write(command);
Serial.write(data1);
Serial.write(data2);
}
int getMaxVal(int lastVal){
int currentVal = analogRead(piezo);
while (currentVal>lastVal){
lastVal = currentVal;
currentVal = analogRead(piezo);
}
return lastVal;
}
some time ago i have made a MIDI Xylophone using piezos. I made a little page for it with all the parts, the schematics, code, etc...
maybe you can find some useful information there: http://popo.webatu.com/
Other wise, i would suggest you learn about arrays. i think they will be very useful on such a project.
Here is a good page to have an intro on the arrays:
So, if you understand how to use arrays you can change your code using them so instead of having one piezo you have several.
(you could of course write separate code for each of the piezos, but it is more efficient to use arrays ).
I would also suggest that you don't use the delay() function.
The delay() function is a blocking function, it basically "pauses" your sketch and nothing else can happen during that period. So if you are "pausing" your instrument each time you play a note, it means that on that half a second you won't be able to play any other notes.
You can look and the BlinkWithoutDelay page, to get an idea on how to achieve the same effect using a different technic.
And if after that half a second you want to send a note off, then instead of your