Hello, this code works but sometimes the values are wrong
countdown=64;
do{
if (Serial.available()){
digitalWrite(13,HIGH);
commandByte = Serial.read();//read first byte
noteByte = Serial.read();//read next byte
velocityByte = Serial.read();//read final byte
if (commandByte == 176) {
// ... doing something
}
countdown--;
}
while ((Serial.available() > 2) && (countdown <= 0));//when at least three bytes available
}
Normaly midi datas sends three bytes, but there is a problem.
Because sometimes the values of noteByte or velocityByte appears in commandByte. Sometimes I see only one Byte of midi. It seems this code is not very effective to sync three bytes of serial midi datas.
So, do I need algorithms ? Which are good?
Whatever is sending the MIDI to you is probably using running status to improve the throughput. See, for example, MIDI Specification: Running Status.
If that's what is happening, you will have to write your code to handle this case.
Also, you may be receiving other kinds of MIDI messages which aren't 3 bytes long. For example, my synthesizer (Yamaha SY77) continuously sends an Active Sensing message which is only one byte long (0xFE). If you are receiving those you'll have to ignore them.
Pete
Maybe you should have a look at the MIDI message specification. There are many legitimate MIDI messages that are not 3 bytes long so you really need to analyse the command byte to see how many other bytes to expect (and then running status adds another complication as do SYSEX messages which can be really long).
Or you could use one of the MIDI libraries like MIDI.h which sort almost all of that out for you.
Steve
Hmm, yes I want to use MIDI.h but low memory will turn from 83% to 92%
it's more danger of collisions, isn't it?