Midi IN With ardruino nano getting wrong result

i tried Arduino nano to receive midi PG change msg from my synyhesizer

i want to see my program changed in serial monitor.
i make this sketch to monitor incoming midi msg

#include <MIDI.h>

 MIDI_CREATE_DEFAULT_INSTANCE();
void setup() {
  MIDI.begin(MIDI_CHANNEL_OMNI);
  Serial.begin(57600);
  Serial.println("MIDI Input Test");
}

void loop() {
  if  ( MIDI.read())   {
    byte Patch1,Patch2;
    if  (MIDI.getType()==(midi::ProgramChange))
    {
   Patch1 = MIDI.getData1();
   Patch2 = MIDI.getData2();
               
           Serial.print("Patch1 is -");
          Serial.print(Patch1); 
          Serial.print("    Patch2 is -");
          Serial.println(Patch2); 
               }
  }
}

but i am getting unknown data in serial monitor.
you can see attachments my circuit diagram & result in serial monitor

please tell where i am wrong /////

MIDI standard data rate is 31250 not 57600. And a PC message only has one data byte.

Steve

slipstick:
MIDI standard data rate is 31250 not 57600. And a PC message only has one data byte.

Steve

I tried ( 31250) in sketch , but I saw , in my serial monitor window , baud rate can be select from (300/1200/2400/4800/9600/19200/38400/ 57600/74880/115200/230400/250000) only
?????

but I saw , in my serial monitor window , baud rate can be select from (300/1200/2400/4800/9600/19200/38400/ 57600/74880/115200/230400/250000)

Yes that is right, you can not show MIDI data in the serial port sent at MIDI baud rates. That is life.

You could use software serial to read the MIDI and then the normal serial to print the results in the monitor at a rate you can display.

But. I want. To use midi incoming PC as variable in my sketch . And send it to midi out (PC) after much calculation .........

charnjit:
But. I want. To use midi incoming PC as variable in my sketch . And send it to midi out (PC) after much calculation .........

That's not a problem because MIDI In and MIDI Out work at the same rate, 31250.

If you really need to display it then use different terminal software. There are terminal emulators available which will work with selectable non-standard rates.

Steve

I am not familiar with software serial reading midi data . I have used i2c oled display some projects . Can use it to see midi incoming data????

I am not familiar with software serial reading midi data

You use it exactly the same as you use serial.

Can use it to see midi incoming data?

Yes.

Use of software serial is out of my knowledge, but I Will try & learn it. Filhal i2c oled display showing me incoming PC data.
Many many thanks to Grumpy_mike , Slapstick
. Now I can do next modifications in my midi I/O sketch .,,, Thank you very much

please look at my sketch & attached wiring.

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

#define OLED_ADDR   0x3C     //     SDA = A4   ,  SCL =A5                         pin 2 = Button , pin 13 = led
Adafruit_SSD1306 display(-1);

 MIDI_CREATE_DEFAULT_INSTANCE();

 byte Patch1,Patch2;
 int Button = 2;
 int led = 13;
 int shift = 0;
void setup() {
  pinMode(Button, INPUT_PULLUP);
  pinMode(led, OUTPUT);
  
  MIDI.begin(MIDI_CHANNEL_OMNI);
  Serial.begin(31250);
  Serial.println("MIDI Input Test");
  
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.clearDisplay();
  display.display();
}

void loop() {
  if  ( MIDI.read())   {
    
    if  (MIDI.getType()==(midi::ProgramChange))
    {
   Patch1 = MIDI.getData1();  //  this incoming Prog change MIDI messege

               }
  }
  // ------------------------------------------------------------------------------------------------------------------------------
    if (digitalRead(Button) == LOW) {   //  is pressed
    digitalWrite(led, HIGH);
    shift = 1;
    MIDI.sendProgramChange(40, 1);
  } else {
    digitalWrite(led, LOW);     //   
    shift = 0;
    
  }
  // ------------------------------------------------------------------------------------------------------------------------------
 
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.print(Patch1);  // ===================Patch 1== cuurent incoming prog change
  display.setCursor(60, 0);
  display.print(shift);  // ===================shift Status=============
  display.display(); 
}

when only midi in is connected to Arduino, It is working according to my sketch. when only Midi Out is connected ,it also work according to sketch.
but when both Midi In & Out are connected , Arduino givining a flood of Midi messeges (Note on/off, Prog change, etc) to my synthesizor …where is wrong ???

charnjit:
when both Midi In & Out are connected , Arduino givining a flood of Midi messeges

Sounds like a midi loop. Whatever comes in via MIDI IN is being sent back out via MIDI OUT, by both your arduino and your synthesiser. So the MIDI messages just keep going round and round and round.

Some Arduino MIDI libraries have a TurnThruOff() method to stop this happening. Or you could change settings on your synth (if I remember right, you need "Local Off"). One or the other should break the troublesome loop.

I want to play my synthesizer having connected to arduino .I want to send pc from arduino to my synthesizer & synthesizer to arduino by manually .how to get rid of these auto midi msg. Local off will mute my
keyboard sound , please tell arduino board side solution.