I have a midi controller connected the USB host on the Teensy 4.1 and am sending out midi through the main USB using USBMidi, if I assign a button on the midi controller to play a chord, when I press the button it just causes the Teensy to reset. If I remove the PlayChord() functions and just put in some delays of a few seconds its fine so it's definitely calling USBMidi that's crashing it. I'm also using a standard MIDI jack as well but I'm not sending anything to it yet so I don't think it could be the cause. (The playChord() functions work fine when not being triggered using the midi controller).
C++:
#include <Bounce.h> // Bounce library makes button change detection easy
#include <MidiFile.h>
#include <Options.h>
#include <algorithm>
#include <string>
#include <random>
#include <Adafruit_GFX.h> // Include the Adafruit GFX Library
#include <Adafruit_ST7735.h>
#include <SPI.h>
#include <SD.h>
#include <MIDI.h>
#include <Adafruit_NeoTrellis.h>
#include <Wire.h>
#include <USBHost_t36.h>
#include <Arduino.h>
USBHost myusb;
USBHub hub1(myusb);
USBHub hub2(myusb);
MIDIDevice midi1(myusb);
#define TFT_CS 10
#define TFT_RST 7
#define TFT_DC 15
#define MAX_FILENAME_LENGTH 22
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);
const int channel = 1;
void myControlChange(unsigned char channel, unsigned char control, unsigned char value) {
if (control == 23) {
if (value == 127) {
//delay(1000);
playChord(scaleChords[currentChordBank][1]);
} else if (value == 0) {
//delay(1000);
stopChord(scaleChords[currentChordBank][1]);
}
}
Serial.print("Control Change, ch=");
Serial.print(channel, DEC);
Serial.print(", control=");
Serial.print(control, DEC);
Serial.print(", value=");
Serial.println(value, DEC);
}
void playChord(const std::vector<int>& chord) {
for (int note : chord) {
usbMIDI.sendNoteOn(KeyNum + note, 99, 1);
//MIDI.sendNoteOn(KeyNum + note, 99, 1);
}
}
void stopChord(const std::vector<int>& chord) {
for (int note : chord) {
usbMIDI.sendNoteOff(KeyNum + note, 99, 1);
//MIDI.sendNoteOn(KeyNum + note, 99, 1);
}
}
void setup() {
midi1.setHandleControlChange(myControlChange);
Serial.begin(9600);
delay(1500);
myusb.begin();
MIDI.begin();
}
void loop() {
myusb.Task();
midi1.read();
}