Teensy 4.0: Sending MIDI through USB

I want to use a Teensy 4.0 board to send MIDI messages through USB. I'm trying to connect it with LMMS. I have a simple sketch where bytes of data is sent on the press of a button. The code compiles and the sketch uploads to the board without error. LMMS is able to see the board as "Teensy MIDI" when selecting the board as input for a preset. But no sound is generated from the program when I press a button.

The header pins are already soldered to the board. I'm running the code in the Arduino IDE, with "USB Type" set to "MIDI" in the tools menu and using the correct port.

I feel like I'm overlooking something. Here is my code:
Code:
#include <Arduino.h>
#include <Metro.h>
#include <Bounce.h>
#include <Encoder.h>

#define btn0 22
#define led0 14

const long DEBOUNCE_DELAY = 5;
const byte MIDI_LISTEN_CH = 4;
const byte VELOCITY = 127;
const byte MIDI_CH = 1;
byte note[] = {36,37,38,39,40,41};

Metro buttonPress = Metro(3);
Bounce btn_0 = Bounce(btn0, DEBOUNCE_DELAY);

void setup() {
  pinMode(btn0, INPUT_PULLUP);
  pinMode(led0, OUTPUT);
  Serial.begin(115200);
  usbMIDI.begin();
}

void loop() {
  if(buttonPress.check()){
    btn_0.update();
    
    if(btn_0.fallingEdge()) {
      Serial.println("Button 0 Released");
      digitalWrite(led0, HIGH);
      usbMIDI.sendNoteOn(note[0], VELOCITY, MIDI_CH);
    }
    if(btn_0.risingEdge()) {
      Serial.println("Button 0 Pressed");
      digitalWrite(led0, LOW);
      usbMIDI.sendNoteOff(note[0], VELOCITY, MIDI_CH);
    }
  }

  while (usbMIDI.read()) {} //read and ingnore incoming messages
  
}

I've also referenced these guides and videos numerous times:
https://www.pjrc.com/teensy/td_midi.html
https://www.youtube.com/watch?v=E_GdsxTm6aM
 
I've not used Metro but maybe it's stopping bounce from seeing the tranistions.

I think you would test timing limit after transitions but bounce is usually sufficient on its own to avoid multiple messages so I'm not sure it's needed at all.

Is this original code or did you copy it?
 
Last edited:
I made these changes
Code:
#define btn0 2
#define led0 LED_BUILTIN
and your code works on a Teensy2 - I see the MIDI messages with Midi-Ox on my windows PC and also the messages to the Serial monitor.
It also works if I remove the Metro stuff so it doesn't seem to do anything useful.

P.S. It also works on T4.0

Pete
 
I've not used Metro but it maybe it's stopping bounce from seeing the tranistions.

I would thought you would test timing after transitions but bounce is usually sufficient on its own to avoid multiple messages so I'm not sure it's needed at all.

Is this original code or did you copy it?

The code was made in reference to the pjrc Transmit Example. I was using Metro for protothreading. I went and commented out the metro library and everything involving it. LMMS isn't generating sound still, with it gone.
 
I made these changes
Code:
#define btn0 2
#define led0 LED_BUILTIN
and your code works on a Teensy2 - I see the MIDI messages with Midi-Ox on my windows PC and also the messages to the Serial monitor.
It also works if I remove the Metro stuff so it doesn't seem to do anything useful.

P.S. It also works on T4.0

Pete

I'm glad to hear that it works. But its odd. My MIDI-OX isn't picking up any messages. Is it possible my anti virus is interfering? I'm using Vipre Anti Virus. This code is a small piece of my project. I guess for this sketch metro isn't needed.
 
When using MIDI-OX, have you selected Teensy MIDI in Options|MIDI devices. If you don't click on it to highlight it, MIDI-OX will ignore it.

Pete
 
I don't know why this would affect things, but I had my USB cable plugged into a USB hub. When I plugged it directly into my pc, MIDI-OX was able to respond. And LMMS now generates sound
 
Can you name (and shame) this troublesome USB hub? Or give us a photo, if it's some generic no-name thing without a clear model number?

If it's still on the market, I might buy one for testing. I'd really like to know what that hub is doing to cause this problem.
 
Can you name (and shame) this troublesome USB hub? Or give us a photo, if it's some generic no-name thing without a clear model number?

If it's still on the market, I might buy one for testing. I'd really like to know what that hub is doing to cause this problem.

I'm using a 7-Port USB 2.0 Multi Charger Hub + High Speed Adapter ON/OFF Switch Laptop PC US, I bought off eBay.
https://www.ebay.com/sch/i.html?_from=R40&_trksid=p2054502.m570.l1313&_nkw=7-Port+USB+2.0+Multi+Charger+Hub+%2B+High+Speed+Adapter+ON%2FOFF+Switch+Laptop+PC+US&_sacat=0
 
Yikes. I have two of those *useless* units. They backfeed the host, and behave erratically.

Device ID 05e3: 0606 Genesys logic

I can run the usbHost debug on it, or any other useful test or probe etc...
 
fwiw here's what the usbHostt36 debug print has to say with only one of those hubs plugged in


Code:
USB Host Testing
sizeof Device = 36
sizeof Pipe = 96
sizeof Transfer = 64
power up USBHS PHY
 reset waited 5
USBHS_ASYNCLISTADDR = 0
USBHS_PERIODICLISTBASE = 1FFF5000
periodictable = 1FFF5000
port change: 10001803
    connect
  begin reset
port change: 10001805
  port enabled
  end recovery
new_Device: 12 Mbit/sec
new_Pipe
enumeration:
enumeration:
enumeration:
Device Descriptor:
  12 01 00 02 09 00 00 08 E3 05 06 06 02 07 01 02 00 01 
    VendorID = 05E3, ProductID = 0606, Version = 0702
    Class/Subclass/Protocol = 9(Hub) / 0 / 0
    Number of Configurations = 1
enumeration:
enumeration:
Manufacturer: ALCOR
enumeration:
Product: USB Hub 2.0
enumeration:
Config data length = 25
enumeration:
Configuration Descriptor:
  09 02 19 00 01 01 00 E0 32 
    NumInterfaces = 1
    ConfigurationValue = 1
  09 04 00 00 01 09 00 00 00 
    Interface = 0
    Number of endpoints = 1
    Class/Subclass/Protocol = 9(Hub) / 0 / 0
  07 05 81 03 01 00 FF 
    Endpoint = 1 IN
    Type = Interrupt
    Max Size = 1
    Polling Interval = 255
enumeration:
USBHub memory usage = 960
USBHub claim_device this=1FFF3B20
found possible interface, altsetting=0
number of interfaces found = 1
USBHub control callback
09 29 04 09 00 16 64 00 FF 00 00 00 00 00 00 00 
Hub ports = 4
USBHub control callback
USBHub control callback
USBHub control callback
USBHub control callback
power turned on to all ports
device addr = 1
new_Pipe
allocate_interrupt_pipe_bandwidth
 best_bandwidth = 3, at offset = 0, shift= 0
pipe cap1 = F0010101
HUB Callback (member)
status = 10
getstatus, port = 4
USBHub control callback
01 01 01 00 
New Port Status
  status=10101  port=4
  state=0
  Device is present: 
  Has Power
USBHub control callback
Port Status Cleared, port=4
timer event (19999 us): Debounce Timer, this = 1FFF3B20, timer = 1FFF3E38
ports in use bitmask = 10
getstatus, port = 4
USBHub control callback
01 01 00 00 
New Port Status
  status=101  port=4
  state=2
  Device is present: 
  Has Power
timer event (19999 us): Debounce Timer, this = 1FFF3B20, timer = 1FFF3E38
ports in use bitmask = 10
getstatus, port = 4
USBHub control callback
01 01 00 00 
New Port Status
  status=101  port=4
  state=3
  Device is present: 
  Has Power
timer event (20000 us): Debounce Timer, this = 1FFF3B20, timer = 1FFF3E38
ports in use bitmask = 10
getstatus, port = 4
USBHub control callback
01 01 00 00 
New Port Status
  status=101  port=4
  state=4
  Device is present: 
  Has Power
timer event (20000 us): Debounce Timer, this = 1FFF3B20, timer = 1FFF3E38
ports in use bitmask = 10
getstatus, port = 4
USBHub control callback
01 01 00 00 
New Port Status
  status=101  port=4
  state=5
  Device is present: 
  Has Power
timer event (19999 us): Debounce Timer, this = 1FFF3B20, timer = 1FFF3E38
ports in use bitmask = 10
getstatus, port = 4
USBHub control callback
01 01 00 00 
New Port Status
  status=101  port=4
  state=6
  Device is present: 
  Has Power
sending reset
send_setreset
USBHub control callback
unhandled setup, message = 40323
timer event (20000 us): Debounce Timer, this = 1FFF3B20, timer = 1FFF3E38
ports in use bitmask = 0
HUB Callback (member)
status = 10
getstatus, port = 4
USBHub control callback
03 01 10 00 
New Port Status
  status=100103  port=4
  state=7
  Device is present: 
  Enabled, speed = 12 Mbit/sec
  Has Power
USBHub control callback
unhandled setup, message = 140123
timer event (25000 us): Hello, I'm resettimer, this = 1FFF3B20, timer = 1FFF3E54
port_doing_reset = 4
PORT_RECOVERY
new_Device: 12 Mbit/sec
new_Pipe
enumeration:
enumeration:
enumeration:
Device Descriptor:
  12 01 00 02 09 00 00 08 E3 05 06 06 02 07 01 02 00 01 
    VendorID = 05E3, ProductID = 0606, Version = 0702
    Class/Subclass/Protocol = 9(Hub) / 0 / 0
    Number of Configurations = 1
enumeration:
enumeration:
Manufacturer: ALCOR
enumeration:
Product: USB Hub 2.0
enumeration:
Config data length = 25
enumeration:
Configuration Descriptor:
  09 02 19 00 01 01 00 E0 32 
    NumInterfaces = 1
    ConfigurationValue = 1
  09 04 00 00 01 09 00 00 00 
    Interface = 0
    Number of endpoints = 1
    Class/Subclass/Protocol = 9(Hub) / 0 / 0
  07 05 81 03 01 00 FF 
    Endpoint = 1 IN
    Type = Interrupt
    Max Size = 1
    Polling Interval = 255
enumeration:
USBHub memory usage = 960
USBHub claim_device this=1FFF3EE0
found possible interface, altsetting=0
number of interfaces found = 1
USBHub control callback
09 29 04 09 00 16 64 00 FF 00 00 00 00 00 00 00 
Hub ports = 4
USBHub control callback
USBHub control callback
USBHub control callback
USBHub control callback
power turned on to all ports
device addr = 2
new_Pipe
allocate_interrupt_pipe_bandwidth
 best_bandwidth = 3, at offset = 1, shift= 0
pipe cap1 = F0010102
 
Back
Top