Infinite loop triggered by MIDI communication between two Teensy 4.0 boards
I want to establish a two-way asynchronous link between two Teensy 4.0 boards (I'll call them TeensyA and TeensyB) and pass MIDI messages between them. However, after sending a single MIDI message from TeensyA, it looks like that message triggers an infinite reading loop on both boards - as they keep reading that single message over and over again.
More details below.
I connected the Serial3 interface from TeensyA with the Serial4 interface of TeensyB (wiring diagram attached).
The code on both boards uses the Arduino Midi library (version 5.0.2, latest at the time of writing) and, in short:
TeensyA code
TeensyB code
Commenting the last line in TeensyA's loop function
'fixes' the issue (a single message gets sent by TeensyA and then logged by TeensyB), but as soon as I attempt to read incoming messages on TeensyA (to establish the two-way asynchronous link I'm looking for), the infinite-loop behaviour kicks in - even though at no point does TeensyB send anything over to TeensyA. I don't know if the message gets stuck in an internal buffer that never gets flushed, or if this is normal behaviour when working with Serial communication that I'm not aware of. Either way, I appreciate any help in troubleshooting this issue!
I want to establish a two-way asynchronous link between two Teensy 4.0 boards (I'll call them TeensyA and TeensyB) and pass MIDI messages between them. However, after sending a single MIDI message from TeensyA, it looks like that message triggers an infinite reading loop on both boards - as they keep reading that single message over and over again.
More details below.
I connected the Serial3 interface from TeensyA with the Serial4 interface of TeensyB (wiring diagram attached).
The code on both boards uses the Arduino Midi library (version 5.0.2, latest at the time of writing) and, in short:
- Instructs both boards to listen for and log any incoming MIDI messages (supposedly coming from the other board)
- Instructs only TeensyA to send a single one-off message to TeensyB
TeensyA code
Code:
#include <MIDI.h>
MIDI_CREATE_INSTANCE(HardwareSerial, Serial3, MidiTeensyA);
void noteOnHandler(byte chn, byte note, byte vel) {
Serial.print("Teensy A received noteOn ");
Serial.println(note);
}
void setup() {
MidiTeensyA.setHandleNoteOn(noteOnHandler);
MidiTeensyA.begin(MIDI_CHANNEL_OMNI);
Serial.begin(9600);
// wait 1s and send a single note to TeensyB
delay(1000);
MidiTeensyA.sendNoteOn(42, 127, 1);
}
void loop() {
delay(20);
// read incoming messages from TeensyB
MidiTeensyA.read();
}
TeensyB code
Code:
#include <MIDI.h>
MIDI_CREATE_INSTANCE(HardwareSerial, Serial4, MidiTeensyB);
void noteOnHandler(byte chn, byte note, byte vel) {
Serial.print("Teensy B received noteOn ");
Serial.println(note);
}
void setup() {
MidiTeensyB.setHandleNoteOn(noteOnHandler);
MidiTeensyB.begin(MIDI_CHANNEL_OMNI);
Serial.begin(31250);
}
void loop() {
// read incoming messages from TeensyA
MidiTeensyB.read();
}
Commenting the last line in TeensyA's loop function
Code:
MidiTeensyA.read();
'fixes' the issue (a single message gets sent by TeensyA and then logged by TeensyB), but as soon as I attempt to read incoming messages on TeensyA (to establish the two-way asynchronous link I'm looking for), the infinite-loop behaviour kicks in - even though at no point does TeensyB send anything over to TeensyA. I don't know if the message gets stuck in an internal buffer that never gets flushed, or if this is normal behaviour when working with Serial communication that I'm not aware of. Either way, I appreciate any help in troubleshooting this issue!
Last edited: