Computer receives the wrong MIDI messages when the messages are send through a MIDI OUT port

A while ago I built a MIDI foot controller to trigger samples from my mac (in Mainstage) while playing the keyboard, it was pretty easy and worked flawlessly.

The app that i was using to send the MIDI messages to the mac (hairless midi serial) is not working in the most recent versions of macOS, so i thought to add a MIDI OUT port to the controller and send the MIDI messages to the mac through a USB to MIDI interface like this one:

but it doesn't work, the mac receives some MIDI messages but not the ones it's supposed to, instead of receiving note on messages it's receiving reset messages and program messages with random values.

here's a sample of the messages i receive:

Captura de Pantalla 2022-02-12 a la(s) 23.07.18

here's the code:


#include <MIDI.h>
// Button 1:
int button = 2;
int buttonState;
int prevButtonState = 1;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

//Button2:
int button2 = 3;
int buttonState2;
int prevButtonState2 = 1;
unsigned long lastDebounceTime2 = 0;
unsigned long debounceDelay2 = 50;

//Button3:
int button3 = 4;
int buttonState3;
int prevButtonState3 = 1;
unsigned long lastDebounceTime3 = 0;
unsigned long debounceDelay3 = 50;

//Button4:
int button4 = 5;
int buttonState4;
int prevButtonState4 = 1;
unsigned long lastDebounceTime4 = 0;
unsigned long debounceDelay4 = 50;

//Button5:
int button5 = 6;
int buttonState5;
int prevButtonState5 = 1;
unsigned long lastDebounceTime5 = 0;
unsigned long debounceDelay5 = 50;


MIDI_CREATE_DEFAULT_INSTANCE();

void setup() {
  //Global
  MIDI.begin(MIDI_CHANNEL_OFF);

  //Button1
  pinMode(button, INPUT_PULLUP);

  //Button2
  pinMode(button2, INPUT_PULLUP);

  //Button3
  pinMode(button3, INPUT_PULLUP);

  //Button4
  pinMode(button4, INPUT_PULLUP);

  //Button5
  pinMode(button5, INPUT_PULLUP);
}

void loop() {
  //Button1
  int reading = digitalRead(button);
  if (reading != prevButtonState) {
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;
      if (buttonState == 0) {
        MIDI.sendNoteOn(36, 127, 1);
        delay(200);
        MIDI.sendNoteOn(36, 0, 1);
      }
    }
  }
  prevButtonState = reading;

  //Button2
  int reading2 = digitalRead(button2);
  if (reading2 != prevButtonState2) {
    lastDebounceTime2 = millis();
  }
  if ((millis() - lastDebounceTime2) > debounceDelay2) {
    if (reading2 != buttonState2) {
      buttonState2 = reading2;
      if (buttonState2 == 0) {
        MIDI.sendNoteOn(38, 127, 1);
        delay(200);
        MIDI.sendNoteOn(38, 0, 1);
      }
    }
  }
  prevButtonState2 = reading2;

  //Button3
  int reading3 = digitalRead(button3);
  if (reading3 != prevButtonState3) {
    lastDebounceTime3 = millis();
  }
  if ((millis() - lastDebounceTime3) > debounceDelay3) {
    if (reading3 != buttonState3) {
      buttonState3 = reading3;
      if (buttonState3 == 0) {
        MIDI.sendNoteOn(40, 127, 1);
        delay(200);
        MIDI.sendNoteOn(40, 0, 1);
      }
    }
  }
  prevButtonState3 = reading3;

  //Button4
  int reading4 = digitalRead(button4);
  if (reading4 != prevButtonState4) {
    lastDebounceTime4 = millis();
  }
  if ((millis() - lastDebounceTime4) > debounceDelay4) {
    if (reading4 != buttonState4) {
      buttonState4 = reading4;
      if (buttonState4 == 0) {
        MIDI.sendNoteOn(42, 127, 1);
        delay(200);
        MIDI.sendNoteOn(42, 0, 1);
      }
    }
  }
  prevButtonState4 = reading4;

  //Button5
  int reading5 = digitalRead(button5);
  if (reading5 != prevButtonState5) {
    lastDebounceTime5 = millis();
  }
  if ((millis() - lastDebounceTime5) > debounceDelay5) {
    if (reading5 != buttonState5) {
      buttonState5 = reading5;
      if (buttonState5 == 0) {
        MIDI.sendNoteOn(44, 127, 1);
        delay(200);
        MIDI.sendNoteOn(44, 0, 1);
      }
    }
  }
  prevButtonState5 = reading5;
}</>

as you see, i'm only using MIDI OUT messages in the code.

For the MIDI OUT port i followed the diagram from this site:

https://learn.sparkfun.com/tutorials/midi-tutorial/hardware--electronic-implementation

at the bottom is the MIDI OUT port diagram.

Does anyone have any idea why is not working correctly?, the code is exactly the same that i used with hairless midi serial (i only removed a Serial.begin() in the setup function).

Hello
I´ve made a low level flight about the sketch.
Take some time and go step by step through the button handling. I have see there is a worth considering mixture between state event and time evemt handling.
All in all it would be more conviened to use a structure array to collect all information and one service to process these information.
Have a nice day and enjoy coding in C++.

But that's not a complete diagram, it doesn't identify the inverters used.

Please post you exact MIDI OUT circuit, all components identified (including the particular Arduino involved).

Why - surely you are still using the serial pin? That's the MIDI.h default.

Yeah, removing the Serial.begin() was wrong but even before i removed it the sketch wasn't working. it made no difference whether it was there or not, the MIDI messages my mac received where equally wrong.

I fixed the issue yesterday by adding the Serial.begin back and by changing the baud rate (which was the main issue).

Before i removed the Serial it looked like these:

Serial.begin(115200);

The correct one is these:

Serial.begin(31250);

i was using the first one because hairless midi serial needs it:

https://projectgus.github.io/hairless-midiserial/

Now is working perfectly.

As for the MIDI Out port, i omitted the inverters when i built the port, watched a lot of setups from other people building MIDI controllers and most don't include them in the MIDI OUT port (some diagrams don't include them at all as well), it's been working fine like these in any case but i will search for the exact model an add them.