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

Thread: MIDI causes crash on Teensy 4.1

  1. #1

    MIDI causes crash on Teensy 4.1

    Running on a Teensy 4.1, this gets up to 185 bytes sent every time and then stops. Any ideas? Thanks.

    Code:
    #include <MIDI.h>
    
    HardwareSerial serialPort0 = {Serial1}; //bi
    HardwareSerial serialPort1 = {Serial7}; //bi
    HardwareSerial serialPort2 = {Serial8}; //bi
    HardwareSerial serialPort3 = {Serial4}; //out
    HardwareSerial serialPort4 = {Serial3}; //out
    
    	
    MIDI_CREATE_INSTANCE(HardwareSerial, serialPort0, port0);
    MIDI_CREATE_INSTANCE(HardwareSerial, serialPort1, port1);
    MIDI_CREATE_INSTANCE(HardwareSerial, serialPort2, port2);
    MIDI_CREATE_INSTANCE(HardwareSerial, serialPort3, port3);
    MIDI_CREATE_INSTANCE(HardwareSerial, serialPort4, port4);
    
    decltype(port0)* outHardwarePorts[] = {&port0, &port1, &port2, &port3, &port4};
    decltype(port0)* inHardwarePorts[] = {&port0, &port1, &port2};
    
    	void begin(){
    		for(auto p : outHardwarePorts){
    			p->begin();
    		}
    	}
    
    void setup(){
    	Serial.begin(9600);
    	begin();
    }
    
    int bytesSent = 0;
    
    void loop(){
    	for(int p = 0; p<128; p++){
    		delay(10);
    		for(int po = 0; po<5; po++){
    			int c =1;
    			// MIDIports::MIDIPort mpo = static_cast<MIDIports::MIDIPort>(po);
    			Serial.print("PITCH: ");
    			Serial.println(p);
    			Serial.print("PORT: ");
    			Serial.println(po);
    			delay(20);
    			outHardwarePorts[po]->sendNoteOn(p, 100, c);
    			bytesSent+=3;
    			Serial.println(bytesSent);
    			delay(20);
    			outHardwarePorts[po]->sendNoteOff(p, 97, c);
    			bytesSent+=3;
    			Serial.println(bytesSent);
    			delay(20);
    			outHardwarePorts[po]->sendRealTime(midi::Continue);
    			bytesSent++;
    			Serial.println(bytesSent);
    		}
    	}
    }

  2. #2
    This should be easy for someone to solve. If nobody can solve it, looks like I am the most intelligent person here...

  3. #3
    I don't think you're supposed to make copies of the serial ports. Not sure why the HardwareSerial copy constructor isn't deleted.
    If you want to declare the serial ports separately, use references instead: HardwareSerial &serialPort0 = Serial1;

    Pieter

  4. #4
    Quote Originally Posted by PieterP View Post
    I don't think you're supposed to make copies of the serial ports. Not sure why the HardwareSerial copy constructor isn't deleted.
    If you want to declare the serial ports separately, use references instead: HardwareSerial &serialPort0 = Serial1;

    Pieter
    That is wonderful, Pieter. Thank you very much for your help.

Posting Permissions

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