USBMidi & USBHost Conflicting?

Salchicha

Active member
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();

}
 
Could you also post your "MidiFile.h" file (and any others that form part of your project) so we can build and/or analyze your complete sketch ??

Thanks,

Mark J Culross
KD5RXT
 
I copied your program into Arduino IDE. Of course it doesn't compile because MidiFile.h and Options.h are missing. When I comment those out, it seems "scaleChords" and "KeyNum" are the things used that must exist somewhere else in your code.

Can you copy that stuff into this program and delete the includes, and most important... test that the small self-contained program truly does still reproduce the problem? Please know we will copy this into Arduino IDE and try running it. Make sure it really is a good use of time to work with the code you share here on this forum.
 
As I was creating the small program to post here I got it working, the problem was I hadn't populated the chord bank it was trying to play, not sure why it was crashing the teensy though but its working ok now.
 
Back
Top