add non-blocking delay to MIDI example

This commit is contained in:
Todd Treece committed 2017-03-10 11:38:56 -05:00
1 parent 89d88920c4
commit 4077e29482
1 file changed
+16 -10
@@ -31,6 +31,9 @@ byte note_sequence[] = {
56,61,64,68,74,78,81,86,90,93,98,102
};
// Variable to hold the last time we sent a note
unsigned long previousSend = 0;
void setup()
{
@@ -48,9 +51,6 @@ void setup()
bledis.setModel("Bluefruit Feather52");
bledis.begin();
// Set BLE write callback
blemidi.setWriteCallback(handleWrite);
// Initialize MIDI, and listen to all MIDI channels
MIDI.begin(MIDI_CHANNEL_OMNI);
@@ -92,11 +92,6 @@ void handleNoteOff(byte channel, byte pitch, byte velocity)
Serial.println();
}
void handleWrite() {
// Tell the MIDI instance that we have new data.
MIDI.read();
}
void loop()
{
@@ -110,6 +105,17 @@ void loop()
return;
}
// read any new MIDI messages
MIDI.read();
// Store the current time
unsigned long now = millis();
// Check if enough time has passed since last send
if (now - previousSend < 286) {
return;
}
// Setup variables for the current and previous
// positions in the note sequence.
int current = position;
@@ -135,7 +141,7 @@ void loop()
position = 0;
}
// Wait before sending next note
delay(286);
// Log the send time
previousSend = now;
}