sir ,
i am trying to read midi messege and moniter in oled Display
with below sketch and i have tried both MIDI IN circuits in attachments
the code is ...
after sending prog change from my synthesizer , i am receiving this
Byte1 = 192
Byte2 = 255
Byte3 = 255
here , arduino is recieving three bytes . acord to code first byte is correct.
second & third byte showing " 255 " . whrere is mistake , in my code or in My circuit
please find it. i want to do next after assign recieving bytes with variable integer.
please help me.
See Serial.read() - Arduino Reference You should really be reading into an int not a byte. I suspect that if you do that you'll find that bytes 2 and 3 actually return -1 not 255. -1 = nothing to read.
Byte1 = Serial.read();//read first byte
Byte2 = Serial.read();//read next byte
Byte3 = Serial.read();//read final byte
If one byte is available, read 3 bytes. See the problem?
The [serial input basics tutorial](https://forum.arduino.cc/index.php?topic=396450.0) may have information of interest.
i tried this
if (Serial.available()>2){
Byte1 = Serial.read();//read first byte
Byte2 = Serial.read();//read next byte
Byte3 = Serial.read();//read final byte
if (Byte1 == PROGRAM_CHANGE) { //
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print(Byte1);
display.setCursor(0, 10);
display.print(Byte2);
display.setCursor(0, 20);
display.print(Byte3);
display.display();
}
}
now i am getting three bytes with my patch change data on oled Display. but data is not coming always when program is changed on my keyboard . some time coming and some time missing , some time late printing to Display. can you give any hint in my code ...........
If anything other than a Program Change code arrives you just throw away 3 bytes of data. Any of the two bytes you don't check might have been the PC code.
Try reading just one byte, check if that is a PC (0xC0) and only then read the next 2 bytes. If it isn't 0xC0 just keep checking one byte at a time until you find a PC code.