Leonardo - MIDI device is freezing

My 1st project with Leonardo and having an issue, right from the beginning. What a disappointment :frowning:

I installed MIDI USB library, everything compiles fine, gets uploaded fine, Leonardo is detected from Windows as Midi device.
I then open MIDIOX, select the Arduino, i get the first Note On, Note Off in the Display, and then the device freezes somehow. MIDIOX doesn't read any further, i get the spinning mouse wheel, cant even close MIDIOX unless i unplug Arduino from USB.

Any ideas ?

My code is very simple ..

#include "MIDIUSB.h"

void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:

  noteOn(0, 35, 127);
  MidiUSB.flush();
  delay(1000);
  noteOff(0,35, 0);
  MidiUSB.flush();
  delay(1000);
}

void noteOn(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOn);
}

void noteOff(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOff);
}

You're not clearing the receive buffer, possibly causing the software to hang when it's trying to send data to the Arduino.

If this turns out to be the cause, it indicates that the software was poorly designed, but you should keep reading incoming data on the Arduino (and discard it if you don't care about it) to prevent this issue.

Indeed, i found out that adding MidiUSB.read(); inside the loop, solves the issue. But i wonder why this "trick" is not part of the basic example for MIDI USB library.

But even after adding this , and getting all the sent Midi Notes in MIDIOX, it takes several seconds MIDIOX to close, as it seems it's having troubles to release the device.
Is there some other "hidden" commands that i need to implement (some while loop perhaps) ?
Needless to note that other USB MIDI Class compliant devices connected to MIDIOX are not having such an issue.

Since i dont have any receive code, probably MIDIOX is also tryinng to send something on exit, so i need some code to discard MIDI input. Any quick one to add and try ?

You might want to try the USB-MIDI library instead. I have always found that to be better. It has its own examples as well.

But basically I would have thought that MIDIOX is the problem, have you tried an alternative?

It was the missing MIDI input read, but really the .read() function is poor documented.
Followed your suggestion and tried the USB-MIDI library which gives more options and managed to resolve my issue.
Thanks

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.