getting wrong midi messeges data

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 ...

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>

// OLED display TWI address
#define OLED_ADDR   0x3C     //     SDA = A4   ,  SCL =A5

Adafruit_SSD1306 display(-1);

const uint8_t NOTE_OFF = 0x80;
const uint8_t NOTE_ON = 0x90;
const uint8_t KEY_PRESSURE = 0xA0;
const uint8_t CC = 0xB0;
const uint8_t PROGRAM_CHANGE = 0xC0;
const uint8_t CHANNEL_PRESSURE = 0xD0;
const uint8_t PITCH_BEND = 0xE0;

byte Byte1;
byte Byte2;
byte Byte3;

void setup(){
  
  Serial.begin(31250);
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.clearDisplay();
  display.display();
  
    display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.print("check MIDI Inputs");

  display.display(); 

}

void loop(){
  if (Serial.available()){
      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(); 
      delay(700);
  }
    }
}

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.

c1.jpg

c1.jpg

if (Serial.available()){
      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 may have information of interest.

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.

Steve

groundFungus:

if (Serial.available()){

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.

Steve