Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: Strange behaviour on MIDI full duplex communication between two Teensy 4.0 boards

  1. #1
    Junior Member
    Join Date
    Dec 2020
    Posts
    4

    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).

    Click image for larger version. 

Name:	2xteensy4.0-midi-wiring.png 
Views:	26 
Size:	14.6 KB 
ID:	25187

    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!

    Click image for larger version. 

Name:	serial-monitor-2.gif 
Views:	31 
Size:	101.6 KB 
ID:	25188
    Last edited by meronym; 07-02-2021 at 08:07 PM.

  2. #2
    Senior Member
    Join Date
    Aug 2019
    Location
    Melbourne Australia
    Posts
    281
    The Midi library instantiates with Thru turned on by default so you need to turn it off with:-
    Code:
    void setup() {
      MidiTeensyB.setHandleNoteOn(noteOnHandler);
      MidiTeensyB.begin(MIDI_CHANNEL_OMNI);
      MidiTeensyB.turnThruOff();
     // Serial.begin(31250); // Not needed as the Midi lib sets up the baud rate
    }

  3. #3
    Junior Member
    Join Date
    Dec 2020
    Posts
    4
    This fixed it, thanks a lot!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •