Hi,
I am trying to use a Teensy 4.1 with USB Host cable to send Midi note data to another Teensy 4.1.
On the host Teensy I have the following code which has been adapted from the USBHost_t36 Interace_16x16 example (I have attempted to remove any unneeded code):
#include <MIDI.h> // access to serial (5 pin DIN) MIDI
#include <USBHost_t36.h> // access to USB MIDI devices (plugged into 2nd USB port)
#include <SerialFlash.h>
// Create the Serial MIDI ports
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI1);
// Create the ports for USB devices plugged into Teensy's 2nd USB port (via hubs)
USBHost myusb;
USBHub hub1(myusb);
MIDIDevice midi01(myusb);
// A variable to know how long the LED has been turned on
elapsedMillis ledOnMillis;
void setup() {
Serial.begin(115200);
pinMode(13, OUTPUT); // LED pin
digitalWrite(13, LOW);
MIDI1.begin(MIDI_CHANNEL_OMNI);
delay(1500);
myusb.begin();
}
unsigned long t=0;
void loop() {
int type, note, velocity, channel, d1, d2;
bool activity = false;
// First read messages from the 6 Serial MIDI IN ports
if (MIDI1.read()) {
activity = true;
byte type = MIDI1.getType();
note = MIDI1.getData1();
velocity = MIDI1.getData2();
channel = MIDI1.getChannel();
if (velocity > 0) {
Serial.println(String("Note On: ch=") + channel + ", note=" + note + ", velocity=" + velocity);
midi01.sendNoteOn(note, velocity, channel);
} else {
Serial.println(String("Note Off: ch=") + channel + ", note=" + note);
midi01.sendNoteOff(note, velocity, channel);
}
}
// blink the LED when any activity has happened
if (activity) {
digitalWriteFast(13, HIGH); // LED on
ledOnMillis = 0;
}
if (ledOnMillis > 15) {
digitalWriteFast(13, LOW); // LED off
}
}
On the second Teensy 4.1, the one that is to receive Midi data, I have the following code, which blinks on receiving Midi messages:
// USB MIDI receive example, Note on/off -> LED on/off
// contributed by Alessandro Fasan
int ledPin = 13;
void OnNoteOn(byte channel, byte note, byte velocity) {
digitalWrite(ledPin, HIGH); // Any Note-On turns on LED
Serial.println("noton");
Serial.println(note);
Serial.println(velocity);
}
void OnNoteOff(byte channel, byte note, byte velocity) {
digitalWrite(ledPin, LOW); // Any Note-Off turns off LED
Serial.println("notoff");
}
void setup() {
Serial.begin(9600); // USB is always 12 Mbit/sec
Serial.println("Hello, this is your synth speaking...");
pinMode(ledPin, OUTPUT);
usbMIDI.setHandleNoteOff(OnNoteOff);
usbMIDI.setHandleNoteOn(OnNoteOn) ;
digitalWrite(ledPin, HIGH);
delay(400); // Blink LED once at startup
digitalWrite(ledPin, LOW);
}
void loop() {
usbMIDI.read();
}
The host Teensy's LED does blink when receiving Midi, both from the PC and 5 Pin. It is also is able to print the correct Midi Messages to the Serial Monitor.
And when the second Teensy is connected to the PC it's LED blinks when receiving Midi.
I've been scratching my head all day trying to work out the issue, trying all the usual troubleshooting techniques.
Thanks for any help you are able to give.
(Apologies if my description of the issue is not adequate.)
I am trying to use a Teensy 4.1 with USB Host cable to send Midi note data to another Teensy 4.1.
On the host Teensy I have the following code which has been adapted from the USBHost_t36 Interace_16x16 example (I have attempted to remove any unneeded code):
#include <MIDI.h> // access to serial (5 pin DIN) MIDI
#include <USBHost_t36.h> // access to USB MIDI devices (plugged into 2nd USB port)
#include <SerialFlash.h>
// Create the Serial MIDI ports
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI1);
// Create the ports for USB devices plugged into Teensy's 2nd USB port (via hubs)
USBHost myusb;
USBHub hub1(myusb);
MIDIDevice midi01(myusb);
// A variable to know how long the LED has been turned on
elapsedMillis ledOnMillis;
void setup() {
Serial.begin(115200);
pinMode(13, OUTPUT); // LED pin
digitalWrite(13, LOW);
MIDI1.begin(MIDI_CHANNEL_OMNI);
delay(1500);
myusb.begin();
}
unsigned long t=0;
void loop() {
int type, note, velocity, channel, d1, d2;
bool activity = false;
// First read messages from the 6 Serial MIDI IN ports
if (MIDI1.read()) {
activity = true;
byte type = MIDI1.getType();
note = MIDI1.getData1();
velocity = MIDI1.getData2();
channel = MIDI1.getChannel();
if (velocity > 0) {
Serial.println(String("Note On: ch=") + channel + ", note=" + note + ", velocity=" + velocity);
midi01.sendNoteOn(note, velocity, channel);
} else {
Serial.println(String("Note Off: ch=") + channel + ", note=" + note);
midi01.sendNoteOff(note, velocity, channel);
}
}
// blink the LED when any activity has happened
if (activity) {
digitalWriteFast(13, HIGH); // LED on
ledOnMillis = 0;
}
if (ledOnMillis > 15) {
digitalWriteFast(13, LOW); // LED off
}
}
On the second Teensy 4.1, the one that is to receive Midi data, I have the following code, which blinks on receiving Midi messages:
// USB MIDI receive example, Note on/off -> LED on/off
// contributed by Alessandro Fasan
int ledPin = 13;
void OnNoteOn(byte channel, byte note, byte velocity) {
digitalWrite(ledPin, HIGH); // Any Note-On turns on LED
Serial.println("noton");
Serial.println(note);
Serial.println(velocity);
}
void OnNoteOff(byte channel, byte note, byte velocity) {
digitalWrite(ledPin, LOW); // Any Note-Off turns off LED
Serial.println("notoff");
}
void setup() {
Serial.begin(9600); // USB is always 12 Mbit/sec
Serial.println("Hello, this is your synth speaking...");
pinMode(ledPin, OUTPUT);
usbMIDI.setHandleNoteOff(OnNoteOff);
usbMIDI.setHandleNoteOn(OnNoteOn) ;
digitalWrite(ledPin, HIGH);
delay(400); // Blink LED once at startup
digitalWrite(ledPin, LOW);
}
void loop() {
usbMIDI.read();
}
The host Teensy's LED does blink when receiving Midi, both from the PC and 5 Pin. It is also is able to print the correct Midi Messages to the Serial Monitor.
And when the second Teensy is connected to the PC it's LED blinks when receiving Midi.
I've been scratching my head all day trying to work out the issue, trying all the usual troubleshooting techniques.
Thanks for any help you are able to give.
(Apologies if my description of the issue is not adequate.)