receiving wrong bytes

Please help!

Im trying to program arduino to read MIDI signal. I connected all components as it is shown on a scheme - a MIDI port is connected to optocoupler which is connected to RX1, 5v and GND. The MIDI signal has 31250 baud frequency so i tuned serial1 to the same speed.

Here is the code:

void setup() 
void loop() 
  if (Serial1.available()) 
    byte b =; 
    if (b >= 128) Serial.println(); 
    if (b < 16) Serial.print("0"); 
    Serial.print(b, HEX); 

Theoretically it should work but the byte values are not compatible with standard MIDI bytes. The MIDI signal should contain three bytes:

byte 1: information "note on" or "note off" and other informations
byte 2: information about which note is pressed
byte 3: information about velocity

The first byte is the only one which has "1" in first bit. Thats why all bytes higher or equal DEC 128 are printed in new lines as first bytes.

In my case the first byte doesnt contain information about note on or off. It is always HEX C0 which means "program change". The second byte appears only on pressing or releasing synthesizers key and it is always HEX 00. The third byte never appears.

What is wrong? HELP!

In your circuit, there is no current limiting resistor for the IRLED.
Also, I think your circuit could be improved for a stronger signal ... try this:

ok, ill try

Thanks man, that helped a lot! The only thing I had to change in your scheme was the 220 resistor - i replaced it to 1K and voila, system works!