Wish to filter multiple Midi "note on" messages (just want Note on, Note off) ..

Hello,... I have an Arduino Uno and an Olimex Midi Shield (3 ports on it). I have a situation where I have a piece of music gear that outputs wayy to many extraneous "note on" Midi firings before it sends the "note off" command. I want to build a filter using my above Arduino circuits... so that what passes through is just the initial "note on" command, and then the subsequent "note off" for any given note. There are no other types of data to take into consideration (no velocity, no aftertouch etc). Don't know if anyone has ever made a code for this application... but any info one might have would be appreciated. Thank you ! Jim

No experience with MIDI, but if you can detect the first "note on", you can set a flag. Once the flag is set, do not pass other "not on" messages through. Reset the flag when you detect a "note off" message.

Something in the line of

void loop()
  static bool noteOnDetected = false;

  cmd = readMidi();
  if(cmd == NOTE_ON)
    if(noteOnDetected == false)
      noteOnDetected = true;

  if(cmd == NOTE_OFF)
    noteOnDetected = false;

As said, no experience with midi, so I have no idea how to receive and send; you'll need to replace the readMidi and sendMidi by the correct functionality.

Thank you much sterretje for your reply. I will try with my limited knowledge to see if I can figure out how to adapt that code for my purposes.

I ran across some code from someone else who was trying something similar. With his MIDI filter he simply wanted to filter out anything that wasn't a "note on" or "note off" command. Obviously I want to go a bit further and filter out any additional "note on" commands until a "note off" command takes place. So keeping just the initial "note on" (ignore any other "note on" commands, until the "note off" command happens). I'm guessing that if I somehow combine what sterretje wrote for code, along with this gentleman's code (see below)... I would likely have my answer :).

// MIDI filter to only allow note OFF or note ON and data (0-127)
// note OFF: 128-143
// note ON : 144-159

#define NOTE 0
#define VEL 1

int data = 0;
int command = 0;
int note = 60;
int velocity = 100;
boolean nORv = NOTE;

void setup() {
Serial.begin(31250); // open the serial port for MIDI

void loop() {

if (Serial.available()) {
data = Serial.read();
if (data > 159) {
return; // other MIDI command
else if (data > 127) {
command = data; // note OFF or ON command
else if (nORv == NOTE) {
note = data;
nORv = VEL;
else if (nORv == VEL) {
velocity = data;
nORv = NOTE;


Have you looked at using the MIDI library, MIDI.h? That already does all the sorting out of MIDI message types and data for you and has a very useful callback mechanism. The examples that come with it are pretty good.

You're presumably going to need to keep Note On flags for each individual note because you only want to ignore the second and subsequent ones if they are for the same note.

And there's another minor complication because some MIDI devices don't send a specific Note Off (0x08) command but instead send a Note On (0x09) for the same note with Velocity = 0 which is interpreted as Note Off. So in the general case you can't just ignore all subsequent Note Ons for a note because one of them may be intended to act as the Note Off. That may not be a problem if you only want your code to work for one specific MIDI device.


Steve,… thank you for your input. It was helpful. Since then I have determined that my midi output that I wish to filter does output note off commands as 0x08. But, on the contrary,… after playing around with the midi.h library… it does indeed use the “note on with 0 velocity” as it’s note off command (bummer). So I don’t think the library is going to benefit me. My device is for 13 notes only (they never change) and there isn’t extraneous info going on, only note on / note off , all notes same velocity.

I would welcome any strategies anyone might think of code-wise. Conversely… I have an old Casio light up keyboard around here and I have run my midi instrument’s output through that keyboard and watched the LED lights. They don’t perceptively blink… so i think if nothing else, I can take the circuit out… trigger another midi circuit with the LEDs and it would erase the extraneous note on firings (kind of like a big optoisolator). Before I resort to that… I am downloading Mike Cook’s Midi Glockenspiel code to see if I can modify that for my purposes (it outputs only 8 notes, but I’m thinking I can use an Arduino Mega and have enough pins for 13 notes)… because I’m thinking that relays (whether mechanical or solid state) wouldn’t react quick enough to rapidfire note on issue… so I could have a separate Arduino taking in the midi input and convert it to a mechanical on/off switch… and then I could connect that to a small midi circuit from a keyboard and I’d be done… effectively filtering out the problem.

I’m already using an Arduino to generate my instrument (firing from a USB keyboard… which I have made work as controller as opposed to a PS2 keyboard - I have to use “this particular keyboard” for reasons that are too long to get into. I fixed every problem,… but the keyboard itself outputs autokeying if you hold a note down… which means too many midi on notes.

I have been aware that I could have tackled this one of two ways… by either filtering the midi, or filtering the USB signal going in. I am using the PS2 library… and try as I might I can’t get the rapid fire keying issue from a USB keyboard to go away (I understand that the USB keyboards are a different beast from the PS2). So I thought on the midi end was my only real hope.

Anyway,… I’m thinking this post might get some kind of award for the most convoluted ever. But that’s the story…

MIDI.h works with either a zero velocity 0x09 or a 0x08 message as Note Off (which is what the MIDI standard says). But if you have only 13 notes and you know that they always send standard Note On (0x09) and Note Off (0x08) messages then isn't it simple enough that you don't really need a library?

Just set up an array of 13 boolean entries, one per note. When a Note On turns up check the appropriate entry for that note and if it isn't set pass the message on and set the entry. If it is already set don't pass the message on. When a Note Off turns up unset the appropriate entry.

It is getting late here so I may have missed something important but it seems pretty straightforward.


Wow,... once again an enlightening post! Based on what you said, I did a google search on "boolean array midi" and made some major progress. It appears that this question has been asked before. For one person it was in reverse... they were getting too many "note off" commands. I found this link:

Which led to this one:

And then I found this (which funny enough is kind of like incorporating my light up Casio keyboard idea):

So tomorrow I have some experimenting to do ! Really appreciate the perceptive suggestions. Probably will have made all of the difference once the smoke clears!! Many Thanks