Nick Gammon's MIDI Decoder

Is there someone who has got this to work on the MEGA2560?
It compiles & loads fine, I get the header message (MIDI Decoder. Author: Nick Gammon) in the serial monitor, the MIDI input's as clean as I can get, down to ground, up to 4.5 v, & the sketch uses pins 2,3 for rx,tx. All good, but . . . not a word when I input MIDI (& that same MIDI drives other stuff just fine & the MIDI messages are correct).
I have set other pins for rx & tx, - to no avail. tried google a few other questions - all a bit vague.

The code was also not successful on other Arduinos either.
This is still a mystery as I get no errors, but no response.
I used the code right off Nick's site. Any ideas?

the sketch uses pins 2,3 for rx,tx

You must be using SoftwareSerial, then. On the Mega, SoftwareSerial does not work on those pins.

You have other hardware serial ports (and pins) available. Use them, and the appropriate SerialN instance, instead of SoftwareSerial.

Thanks for that - I replaced all relevant references to Serial1, & I’m back to the same results I got with the pro mini - that is I get midi messages, but they are wrong. For example, note c5 on & off gives me -
16170884: F#(6)Note on channel = 1, note = , velocity = 60
167816: C (-1)Note on channel = 1, note = , velocity = 60
The messages never show a note off, the velocities in this case should be 127 & 0, & the “note =” has no data.
I’ve tweaked the baud rate & finally got note off data, but the values are still a mess.
So, I repeat my original question, is there anyone out there who has got this sketch to work? I think it has some other bug.

I think it has some other bug.

I'm not at all interested in Midi, and I have no idea whether your changes are correct, nor do I understand how you are testing them. What I can do, though, is look at the code, and suggest avenues of exploration.

Except that I can't see your code.

I attach the code - it is substantially the same as Nick Gammon’s posted code, & I’d appreciate if you could check it out. It contains the mod suggested yesterday, & the code seems to think it is working. Basically, I input MIDI from a midi keyboard to a serial port’s rx, read the incoming data & I get the output which is incorrect, -
I have used this on the Mega 2560, the Uno & the pro mini, & the results are erroneous, in exactly the same way. I have also used 3 different MIDI keyboards. The feeling I have is that the MIDI baud is right, but there is some timing error right at the front end. I have cleaned up the MIDI data so it’s from supply voltage to ground, clean as you’d ever see, but the “synch” or something is out. I’m clocking at 16 meg,
zzzz - I might have to simplify this right down to just the front end to check what’s happening.
Thanks in advance for any help here.

Nick_Gammon_s_MIDI_decoder.ino (14.2 KB)


The baud rate for MIDI is always 31250 bps.

How are you connecting the keyboard to the Arduino? Show us the circuit.

Standard stuff, with opto isolator, just a 1k pullup resistor on the receive end - the midi's fine, it displays on the 'scope at the input pin, clean logic levels - the data goes in, but it's the decode process that doesn't seem to work.

OK, so we can take your word for it that your wiring is perfect.

What output do you get from the decoder with the corrected baud rate (31250)?

This is what I get with C5 ON then OFF, with velocity = 127.

16170884: F#(6)Note on channel = 1, note = , velocity = 60
167816: C (-1)Note on channel = 1, note = , velocity = 60

MidiOX reports the data for that same note as expected, ie correct.

I will "break into" the data just after the read to see just what the sketch is dealing with, as I cannot fault Nick's code. I will post what I find.

Exactly the same as before. Even the timestamps. Wow.

OK - Problem identified. Midi keyboards release a negative pulse (about 3 a second, called "active monitor"), or both of mine do, & I cannot turn these off. The pulse gets interpreted as 11111111,11111111, & so on, so it screws up everything subsequent. If I intercept the data at that first read point & print it out, I get 255, 255,255 etc. When this pulse quits half way through a read, you can get 11100000 or anything else, so no wonder garbage in = garbage out.
I can only assume the original sketch was used on a pre-filtered MIDI stream.
So . . . Filter it I will.
Having tested two other midi outs from two different PC's, different software, that pulse is always there.
For any of these midi-input based sketches to work, that pulse will have to be treated as legit data, & filtered out.

11111111 = 0xFF is MIDI reset. The decoder already knows how to catch & report those. It would be very unusual for an instrument to automatically send MIDI reset messages.

The behaviour you describe sounds more like active sense (0xFE = 11111110). The decoder knows how to catch and report those too.