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

Thread: T3.6: How to detect when a new USB device is connected to the host port/hub

  1. #1

    T3.6: How to detect when a new USB device is connected to the host port/hub

    Does anyone have an example of how to detect when a new USB device (in this case MIDI) is connected to the host port of T3.6? It doesn't need to be immediate, but if there was a flag to test for new devices during the main loop, that would be very helpful.


  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Sorry I am not a MIDI person, but my assumption is for specific things like this you can do it the same way we do it for other devices like joysticks...

    That is you should be able to do something like:
    Note some of this comes from InputFunctions.ino...

    I again have no idea how this works in particular to calling a bunch of these functions if the MIDI is not connected... So again I am guessing!
    So again No Promises! Also since typed on fly.

    /* Receive Incoming USB Host MIDI using functions.  As usbMIDI
       reads incoming messages, handler functions are run.
       See the InputRead example for the non-function alterative.
       This very long example demonstrates all possible handler
       functions.  Most applications need only some of these.
       This example is meant to allow easy copy-and-paste of the
       desired functions.
       Use the Arduino Serial Monitor to view the messages
       as Teensy receives them by USB MIDI
       You must select MIDI from the "Tools > USB Type" menu
       This example code is in the public domain.
    #include <USBHost_t36.h>
    USBHost myusb;
    USBHub hub1(myusb);
    USBHub hub2(myusb);
    MIDIDevice midi1(myusb);
    bool midi_was_active = false;
    void setup() {
      // Wait 1.5 seconds before turning on USB Host.  If connected USB devices
      // use too much power, Teensy at least completes USB enumeration, which
      // makes isolating the power issue easier.
      Serial.println("USB Host InputFunctions example");
    void loop() {
      if (midi1) {
        if (!midi_was_active) {
          // midi was not active before so lets set stuff up...
          midi_was_active = true;
          // Only one of these System Exclusive handlers will actually be
          // used.  See the comments below for the difference between them.
          // This generic System Real Time handler is only used if the
          // more specific ones are not set.
        // The handler functions are called when midi1 reads data.  They
        // will not be called automatically.  You must call
        // regularly from loop() for midi1 to actually read incoming
        // data and run the handler functions as messages arrive.
      } else {
        if (midi_was_active) {
          // do we need to cleanup anything:
          midi_was_active = false;
    void myNoteOn(byte channel, byte note, byte velocity) {
      // When a USB device with multiple virtual cables is used,
      // midi1.getCable() can be used to read which of the virtual
      // MIDI cables received this message.
      Serial.print("Note On, ch=");
      Serial.print(channel, DEC);
      Serial.print(", note=");
      Serial.print(note, DEC);
      Serial.print(", velocity=");
      Serial.println(velocity, DEC);
    void myNoteOff(byte channel, byte note, byte velocity) {
      Serial.print("Note Off, ch=");
      Serial.print(channel, DEC);
      Serial.print(", note=");
      Serial.print(note, DEC);
      Serial.print(", velocity=");
      Serial.println(velocity, DEC);
    void myAfterTouchPoly(byte channel, byte note, byte velocity) {
      Serial.print("AfterTouch Change, ch=");
      Serial.print(channel, DEC);
      Serial.print(", note=");
      Serial.print(note, DEC);
      Serial.print(", velocity=");
      Serial.println(velocity, DEC);
    void myControlChange(byte channel, byte control, byte value) {
      Serial.print("Control Change, ch=");
      Serial.print(channel, DEC);
      Serial.print(", control=");
      Serial.print(control, DEC);
      Serial.print(", value=");
      Serial.println(value, DEC);
    void myProgramChange(byte channel, byte program) {
      Serial.print("Program Change, ch=");
      Serial.print(channel, DEC);
      Serial.print(", program=");
      Serial.println(program, DEC);
    void myAfterTouchChannel(byte channel, byte pressure) {
      Serial.print("After Touch, ch=");
      Serial.print(channel, DEC);
      Serial.print(", pressure=");
      Serial.println(pressure, DEC);
    void myPitchChange(byte channel, int pitch) {
      Serial.print("Pitch Change, ch=");
      Serial.print(channel, DEC);
      Serial.print(", pitch=");
      Serial.println(pitch, DEC);
    // This 3-input System Exclusive function is more complex, but allows you to
    // process very large messages which do not fully fit within the midi1's
    // internal buffer.  Large messages are given to you in chunks, with the
    // 3rd parameter to tell you which is the last chunk.  This function is
    // a Teensy extension, not available in the Arduino MIDI library.
    void mySystemExclusiveChunk(const byte *data, uint16_t length, bool last) {
      Serial.print("SysEx Message: ");
      printBytes(data, length);
      if (last) {
        Serial.println(" (end)");
      } else {
        Serial.println(" (to be continued)");
    // This simpler 2-input System Exclusive function can only receive messages
    // up to the size of the internal buffer.  Larger messages are truncated, with
    // no way to receive the data which did not fit in the buffer.  If both types
    // of SysEx functions are set, the 3-input version will be called by midi1.
    void mySystemExclusive(byte *data, unsigned int length) {
      Serial.print("SysEx Message: ");
      printBytes(data, length);
    void myTimeCodeQuarterFrame(byte data) {
      static char SMPTE[8] = {'0', '0', '0', '0', '0', '0', '0', '0'};
      static byte fps = 0;
      byte index = data >> 4;
      byte number = data & 15;
      if (index == 7) {
        fps = (number >> 1) & 3;
        number = number & 1;
      if (index < 8 || number < 10) {
        SMPTE[index] = number + '0';
        Serial.print("TimeCode: ");  // perhaps only print when index == 7
        Serial.print(SMPTE[1]);  // perhaps add 2 to compensate for MIDI latency?
        switch (fps) {
          case 0: Serial.println(" 24 fps"); break;
          case 1: Serial.println(" 25 fps"); break;
          case 2: Serial.println(" 29.97 fps"); break;
          case 3: Serial.println(" 30 fps"); break;
      } else {
        Serial.print("TimeCode: invalid data = ");
        Serial.println(data, HEX);
    void mySongPosition(uint16_t beats) {
      Serial.print("Song Position, beat=");
    void mySongSelect(byte songNumber) {
      Serial.print("Song Select, song=");
      Serial.println(songNumber, DEC);
    void myTuneRequest() {
      Serial.println("Tune Request");
    void myClock() {
    void myStart() {
    void myContinue() {
    void myStop() {
    void myActiveSensing() {
      Serial.println("Actvice Sensing");
    void mySystemReset() {
      Serial.println("System Reset");
    void myRealTimeSystem(uint8_t realtimebyte) {
      Serial.print("Real Time Message, code=");
      Serial.println(realtimebyte, HEX);
    void printBytes(const byte *data, unsigned int size) {
      while (size > 0) {
        byte b = *data++;
        if (b < 16) Serial.print('0');
        Serial.print(b, HEX);
        if (size > 1) Serial.print(' ');
        size = size - 1;
    Again the object act like if(Serial) like tests...
    So in this case you have if(midi1)

    But I have no idea if this works or not.

  3. #3
    Thanks KurtE, that worked! It seems that repeatedly disconnecting and reconnecting certain USB MIDI devices will cause the sketch to hang, but that may likely be something in my code. I'll report back if I find out more.

  4. #4
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    It is also possible that there is some device or resource that is not getting properly released. Hopefully you or someone else who uses Midi will figure out,

    I will keep my fingers crossed

Posting Permissions

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