In your original program, try changing the 0102 if statement to this ...
//SENSOR 0102---------------------------------------------------
if ((sensorValue01 > threshold || sensorValue02 > threshold) && Key1LastState0102 == false) { // at least 1 sensor off, so turn note off
MIDI_TX(128,sensornote0102,127);
Key1LastState0102 = true;
}
else if (sensorValue01 < threshold && sensorValue02 < threshold && Key1LastState0102 == true) { // sensors on, so turn note on
MIDI_TX(144,sensornote0102,127);
Key1LastState0102 = false;
}
I made a false assumption about what the Key1LastStateXXX booleans meant - I should have checked ![]()
If this works, I have some suggestions for improving the readability of the code, which will help as you develop the project.