Skip to content

Commit 4077e29

Browse files
committed
add non-blocking delay to MIDI example
1 parent 89d8892 commit 4077e29

File tree

1 file changed

+16
-10
lines changed
  • libraries/Bluefruit52Lib/examples/Peripheral/blemidi

1 file changed

+16
-10
lines changed

libraries/Bluefruit52Lib/examples/Peripheral/blemidi/blemidi.ino

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ byte note_sequence[] = {
3131
56,61,64,68,74,78,81,86,90,93,98,102
3232
};
3333

34+
// Variable to hold the last time we sent a note
35+
unsigned long previousSend = 0;
36+
3437
void setup()
3538
{
3639

@@ -48,9 +51,6 @@ void setup()
4851
bledis.setModel("Bluefruit Feather52");
4952
bledis.begin();
5053

51-
// Set BLE write callback
52-
blemidi.setWriteCallback(handleWrite);
53-
5454
// Initialize MIDI, and listen to all MIDI channels
5555
MIDI.begin(MIDI_CHANNEL_OMNI);
5656

@@ -92,11 +92,6 @@ void handleNoteOff(byte channel, byte pitch, byte velocity)
9292
Serial.println();
9393
}
9494

95-
void handleWrite() {
96-
// Tell the MIDI instance that we have new data.
97-
MIDI.read();
98-
}
99-
10095
void loop()
10196
{
10297

@@ -110,6 +105,17 @@ void loop()
110105
return;
111106
}
112107

108+
// read any new MIDI messages
109+
MIDI.read();
110+
111+
// Store the current time
112+
unsigned long now = millis();
113+
114+
// Check if enough time has passed since last send
115+
if (now - previousSend < 286) {
116+
return;
117+
}
118+
113119
// Setup variables for the current and previous
114120
// positions in the note sequence.
115121
int current = position;
@@ -135,7 +141,7 @@ void loop()
135141
position = 0;
136142
}
137143

138-
// Wait before sending next note
139-
delay(286);
144+
// Log the send time
145+
previousSend = now;
140146

141147
}

0 commit comments

Comments
 (0)