Simple MIDI Out sends wrong bytes

Hi there,

I made up a simple wiring like shown here in the MIDI tutorial (arduino.cc/en/tutorial/midi).
I tried different UNOs and a Mega 2560, all cheap chinese replicas.

Here is my code (last try on the Mega with Serial1):

void setup() {
  Serial1.begin(31250);
}

void loop() {

    sendMIDI(144, 136, 0);
    delay(500);
}


void sendMIDI(byte statusByte, byte dataByte1, byte dataByte2) {
  Serial1.write(statusByte);
  Serial1.write(dataByte1);
  Serial1.write(dataByte2);
}

So I want to send a NoteOn every 500 milliseconds.

I have a Edirol USB MIDI/Soundboard to my PC and running MIDI-OX as monitor.
I am expecting a MIDI-Input of 90-88-0 but what I get is constantly F3, 00, --(ignored).

When I change the 144 to a different value, a totally different statusByte is sent.
When I make a loopp rising the notenumber++ it gives total different statuasBytes, changing all the time.

Writing directly in hex Serial1.write(0x90); gives the same F3.

I played around with using SoftwareSerial and finally using the diffferent Serial outputs of the Mega. All the same.

When I change the serial speed of 31250 to another value, different Bytes are saent, not F3 but constantly another status byte (and different dataBytes).

When I connect a keyboard to the MIDI-Interface and play notes, everything is like expected, so the MIDI-interface seems to be ok.

So what's the problem? Do the chinese Arduinos have a problem with 31250 baud or what am I missing?

I am working on this since hours and am a bit frustrated...

How did you connect the MIDI interface to the Arduino?

Chinese Arduino clones should be able to handle 31250 baud just fine (at least the ones I've tested can).

Pieter

PieterP:
How did you connect the MIDI interface to the Arduino?

Like I wrote: "like shown here in the MIDI tutorial (arduino.cc/en/tutorial/midi)"

Abtzero:
Like I wrote: "like shown here in the MIDI tutorial (arduino.cc/en/tutorial/midi)"

My bad, I missed that.

Your first data byte is wrong, it can't be larger than 127. Also prefer using hexadecimal constants for the status byte, decimal values are harder to read and pretty meaningless in that context. The first hexadecimal digit is the message type, and the second digit is the channel.

Are you sure you connected it correctly? Male and female connectors are mirrored.

I am sending dec144 = 0x90 as a Byte. isn't that converted automatically?

But I also wrote sending Serial1.write(0x90); gives the same F3 in the MIDI Monitor (as if I sent dec243)
Serial1.write(144); behaves the same as Serial1.write(0x90); --> F3 is sent.

I used a Teensy 3.2 in serial mode (not in MIDI-USB Mode) sending to MIDI onSerial1. It behaves the same...

Abtzero:
I am sending dec144 = 0x90 as a Byte. isn't that converted automatically?

To the Arduino it's exactly the same, to humans, the latter is much easier to interpret.

Did you double check your connections? Did you fix your second byte? 136 > 127 is not a valid data byte.

Teensy is a 3.3V microcontroller, you need different resistors values.

Thanks for the sceme!
I found out before reading that: My wiring is definitely wrong! I followed a fritzing-image in a YouTube-video, that was wrong (-/+ mixed up).

So I get the F3 as statusByte.
But when I do it right, I have no readable output at all (on the teensy)... :frowning:

And: I think a int of 136 should be sent as hex 88. And is has the same wrong output when I send all 3 Bytes directly written in hex (i.e. Serial.write(0x88);).

Btw: When using a Teensy, which deals with 3.3V: Looking at Your sceme, I need a 33Ohm and a 10 Ohm resistor...but You wrote that I see in taht moment.

So I will switch back to an Uno for testing, because I do not have 33/10 resistors.

Abtzero:
And: I think a int of 136 should be sent as hex 88. And is has the same wrong output when I send all 3 Bytes directly written in hex (i.e. Serial.write(0x88);).

Writing 136 or 0x88 is exactly the same to the Arduino, but that's not the point: MIDI data bytes always have the most significant bit set to zero, so they have to be smaller than 0b10000000 = 0x80 = 128. Only the numbers in the range [0, 127] are valid data bytes. 0x88 has the most significant bit set to 1, so it's a header/status byte (it's the header of a Note Off event on channel 9).

Try a number below 128 instead of 136.

You are right, it's more than 7 bits.
It works now. The MIDI monitor ignored the triplet with the byte that was too high.

Thanks for putting Your head into this!

Glad to hear you got it working!

Abtzero:
The MIDI monitor ignored the triplet with the byte that was too high.

Most likely the MIDI USB interface was dropping it. It has to do some parsing to convert from serial MIDI to MIDI-over-USB, it has to know where messages start and end. The message you were sending was invalid (two incomplete messages), so it couldn't be parsed correctly, and the interface couldn't create any meaningful USB packets representing the message.