Grabbing Midi Chanels at once

Dear Members,
Im a newbie in programming arduino. I just created a code to grab piezo sensors and send the signal to different midi chanels. The same time i push the piezo a LED should glow. It works pretty well but i noticed that it dont work when i hit 2 sensors the same time, Could you please help me with that issue? Furthermore I am shure that I could make the code more efficient by defining more variables. THX for help....Her is the code:

int noteOn = 144;
int piezo = A0;
int piezo1 = A1;
int piezo2 = A2;
int threshold = 500;//anything over fifty means we've hit the piezo

void setup(){
pinMode (2, OUTPUT);
pinMode (3, OUTPUT);
pinMode (4, OUTPUT);
Serial.begin(9600);
}

void loop(){
int piezoVal = analogRead(piezo);
int piezoVal1 = analogRead(piezo1);
int piezoVal2 = analogRead(piezo2);

if (piezoVal>threshold){
Serial.write (noteOn);
Serial.write (36);
Serial.write (127);
digitalWrite(2, LOW);
delay(100);

digitalWrite(2, HIGH);
}

if (piezoVal1>threshold){
Serial.write (noteOn);
Serial.write (37);
Serial.write (127);
digitalWrite(3, LOW);
delay(100);

digitalWrite(3, HIGH);

}
if (piezoVal2>threshold){
Serial.write (noteOn);
Serial.write (38);
Serial.write (127);
digitalWrite(4, LOW);
delay(100);

digitalWrite(4, HIGH);
}}

If (for example) piezo1 exceeds the threshol,d you write some info, do a digitalWrite and then delay(100). If, say, piezo2 had also exceeded the threshold at the same time, by the time the delay for piezo1 is over, piezo2 will probably have dropped below the threshold so you don't see it happen.
You need to implement a variation of "Blink without delay" so that the code can respond to simultaneous events.

Pete

Hi Pete,

first thanx for the fast answer :slight_smile: But the dealy concerns as well the midi output, so if I cancel the delay the midi output is too short to play an instrumet like drums clearly...

Take a look at the blink without delay example. Save the millis() value when you send a note on message, and compare this to the current millis() value. When the difference between the current time and the "note on time" is greater than, say, 100ms, you send a note off event.

Pieter