Board: Arduino Due
OS: Windows 10
I'm learning how to use this library and I…'m having trouble getting messages sent in quick succession to send reliably. It seems I need to add a delay in between each message.
Is this expected behavior? Or is this a bug?
This is what I would expect to work:
```
#include <MIDIUSB.h>
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);
}
void setup() {
}
void loop() {
noteOn(0, 52, 100);
noteOn(0, 53, 100);
noteOn(0, 57, 100);
noteOn(0, 60, 100);
MidiUSB.flush();
delay(200);
noteOff(0, 52, 100);
noteOff(0, 53, 100);
noteOff(0, 57, 100);
noteOff(0, 60, 100);
MidiUSB.flush();
delay(200);
}
```
But only the first message of each group sends reliably. (recorded in FL Studio):
![image](https://user-images.githubusercontent.com/9014937/36358937-6bdde664-14db-11e8-9ad8-ef4701ae7915.png)
I tried adding a flush after each event:
```
#include <MIDIUSB.h>
void noteOn(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOn);
MidiUSB.flush();
}
void noteOff(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOff);
MidiUSB.flush();
}
void setup() {
}
void loop() {
noteOn(0, 52, 100);
noteOn(0, 53, 100);
noteOn(0, 57, 100);
noteOn(0, 60, 100);
delay(200);
noteOff(0, 52, 100);
noteOff(0, 53, 100);
noteOff(0, 57, 100);
noteOff(0, 60, 100);
delay(200);
}
```
Same result:
![image](https://user-images.githubusercontent.com/9014937/36358948-96f5a652-14db-11e8-8585-41df1d9bf996.png)
If I add a 50 microsecond delay it works.
```
#include <MIDIUSB.h>
void noteOn(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOn);
delayMicroseconds(50);
}
void noteOff(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOff);
delayMicroseconds(50);
}
void setup() {
}
void loop() {
noteOn(0, 52, 100);
noteOn(0, 53, 100);
noteOn(0, 57, 100);
noteOn(0, 60, 100);
MidiUSB.flush();
delay(200);
noteOff(0, 52, 100);
noteOff(0, 53, 100);
noteOff(0, 57, 100);
noteOff(0, 60, 100);
MidiUSB.flush();
delay(200);
}
```
![image](https://user-images.githubusercontent.com/9014937/36358969-ea43de82-14db-11e8-969a-3274c700ebf0.png)
25 microseconds is not quite enough:
![image](https://user-images.githubusercontent.com/9014937/36358974-f64b8324-14db-11e8-9e10-52fe1ae6dc82.png)
Thanks for your help.