I am using an Arduino DUE with the aim of utilizing the Native USB capacity of the board for MIDI communication over USB. I can receive MIDI events via USB from external programs like MIDI-OX and Cubase without problems but when I send MIDI commands OUT from my sketch only the first command is initially sent and displayed within e.g. MIDI-OX and then nothing seems to be sent to MIDI-OX until I close the Serial Monitor and reopen it again. Once the Serial Monitor is reopened, the commands that were not displayed are quickly displayed before it stops at the first new MIDI command of the new Sketch - this can be repeated again and again.
Since I am repeating the sending of MIDI commands within the loop() I can see that my sketch is running fine via the print statements to the Serial Monitor, but as stated earlier. only the first message is displayed in MIDI-OX until I close and open the Serial Monitor again.
Here's the simple example code I'm using:
/*
* MIDIUSB_test.ino
*
* Created: 4/6/2015 10:47:08 AM
* Author: gurbrinder grewal
* Modified by Arduino LLC (2015)
*/
#include "MIDIUSB.h"
// First parameter is the event type (0x09 = note on, 0x08 = note off).
// Second parameter is note-on/note-off, combined with the channel.
// Channel can be anything between 0-15. Typically reported to the user as 1-16.
// Third parameter is the note number (48 = middle C).
// Fourth parameter is the velocity (64 = normal, 127 = fastest).
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);
}
// First parameter is the event type (0x0B = control change).
// Second parameter is the event type, combined with the channel.
// Third parameter is the control number number (0-119).
// Fourth parameter is the control value (0-127).
void controlChange(byte channel, byte control, byte value) {
midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
MidiUSB.sendMIDI(event);
}
void setup() {
Serial.begin(115200);
while (!Serial);
}
void loop() {
Serial.println(F("Sending note on"));
Serial.flush();
noteOn(1, 48, 64); // channel, pitch, velocity
MidiUSB.flush();
delay(200);
Serial.println(F("Sending note off"));
Serial.flush();
noteOff(1, 48, 64); // channel, pitch, velocity
MidiUSB.flush();
delay(200);
Serial.println(F("Sending control Change"));
Serial.flush();
controlChange(1, 10, 65); // channel, control, value
MidiUSB.flush();
delay(200);
}
I have applied extensive flush() commands, without any difference or improvements. I have included a screenshot of MIDI-OX where the red arrow indicates the fist MIDI command that is displayed when the sketch is running and even though the sketch is running and printing statements to the Serial Monitor those commands are not "pushed" to the MIDI-OX program unless I close the Serial Monitor and open it again - the group marked by the red X.
I am using version 1.0.3 of the MIDIUSB Library. The Arduino IDE is 1.8.7 and I'm running Windows 10. I am using a genuine Arduino DUE board.
Any tips, tricks or hints are most welcome!
Regards,
Mats