MIDI Note On/Off not working

I am writing a code to send a note on and then a note off, using the Arduino MIDI Library. However, the note on, note off, and all other commands are only sending MIDI program changes (0, 56, or 120) to channel 1. It sometimes also sends a note off to C octave -1.

I am using Linux, so in order to test/use midi data I am running it through JACK(ALSA). I'm also using ttymidi in order to use the Arduino as a usb MIDI device in ALSA.

~$ ttymidi -s /dev/ttyACM0 -v

Then I am conecting the ttymidi of the Arduino into a program that simply reads and displays the midi data called Gmidimonitor. I also have Jack-Keyboard linked up to it to test input. The Tx led on my Uno flashes every time I send the NoteOn/Off, indicating that it is transmitting the data, and If I send MIDI data into the Arduino (via ttymidi) then the Rx light blinks indicating it has received the data.

The code is fairly straight forward...

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();

void setup()
  {
    pinMode(13, OUTPUT);
    MIDI.begin();
  }
  
void loop()
  {


        MIDI.sendNoteOn(42, 60, 1); // Send a Note 
        delay(2500); // Wait for 2.5 seconds
        MIDI.sendNoteOff(42, 0, 1); // Stop the note
        delay(2500); // Wait for 2.5 seconds

  }

Can anyone here see why it isn't working? I've been exhausting myself all day searching for a solution.

Can you hook it up to a regular keyboard?
There might be something up with the programs you are using. Code looks OK.

Aha, sorry this doesn't answer your question, but I have just been trying to get midi to work, following bitluni's midi synth tutorial on youtube, but even while using his precise code, the compile fails.

I notice your code for midi setup is far smaller. His was:

void noteOn(byte ch, byte n, byte v)
{
  note = n;
  velocity = v;
}
void noteOff(byte channel, byte pitch, byte velocity)
{
  velocity = 0;
}

void setup()
{
  MIDI.begin(MIDI_CHANNEL_OMNI);
  MIDI.setHandleNoteOn(noteOn);
  MIDI.setHandleNoteOff(noteOff);

does "MIDI_CREATE_DEFAULT_INSTANCE();" take care of all of that? Was bitluni working on an older midi library?

I think my best option may be to abandon MIDI and embrace Open Sound Control.

Arduino and OSC

I'm pretty sure that the baudrate of ttymidi (115200bps by default if I'm not mistaken) doesn't match the baudrate set by the MIDI library (31250bps), which causes weird issues like the one you're facing. Instead of using MIDI_CREATE_DEFAULT_INSTANCE(), perhaps the following would help (taken from the source code comments in the MIDI library):

struct MySettings : public midi::DefaultSettings
{
    static const long BaudRate = 115200;
};

MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial, MIDI, MySettings);

Remember to set it back to 31250bps again when you want to interface with actual MIDI equipment.

Hope this helps!

shanehutter:
I think my best option may be to abandon MIDI and embrace Open Sound Control.

If you're going to change directions, the easy path would be to use the class-compliant USB MIDI feature on Teensy (full disclosure, I'm the author - yes, this is a shameless plug...)

From Arduino, you use the Tools > USB Type menu to select MIDI (that menu isn't in the stock Arduino IDE, it's added when you run the Teensyduino installer). With MIDI selected, when you Upload, instead of USB Serial, you get true USB MIDI.

Then in your sketch, you can use usbMIDI.noteOn(). Your PC or Mac sees it as a native USB MIDI device, so there's no error-prone and latency-adding software translation step. It also runs at full 12 Mbit/sec USB speed, much faster than 31250 baud.