Store a midi keyboard value

Status
Not open for further replies.

mtiger

Well-known member
Hi Guys,

I have a midi keyboard which communicates over USB. Would it be possible when a key is pressed, for the noteOn value to be sent to Teensy and to store in a variable?

Cheers,
M
 
Thanks Oddson.

I've read through it, however i'm not exactly sure how it would work for what i'm trying to do? Maybe i just don't understand it or how i would implement it.
 
Depends on your Keyboard. If it has a "normal" DIN midi out, you'd add a DIN midi IN to your Teensy and you solve your problem simply by using the midi library. But if it has a "midi to USBhost" output, you'd need the new USB midi host code for the T3.6 and proceed from there.

In both cases, it's simple: Let the Teensy listen to the incoming Midi Data and as soon as there is a NoteOn event sent, do in your code whatever you want with it.

There is example code for both variants which comes with the Teensyduino installation.
 
Thanks Theremingenieur, the keyboard has both DIN and Usb. However, usb is more preferable.

but is this applicable only to the Teensy 3.6? I'm using 3.2 currently.
 
Only the T3.6 has a second USB port which can be configured as USB Host. If you want to stay with the Teensy 3.2 which has only one non-Host USB-Port, you've to go with DIN Midi.
 
Ah I see.

I was reading through the pjrc website relative to the usbMIDI page https://www.pjrc.com/teensy/td_midi.html.. and came across this code:

Code:
int ledPin = 13;

void OnNoteOn(byte channel, byte note, byte velocity) {
  digitalWrite(ledPin, HIGH); // Any Note-On turns on LED
}

void OnNoteOff(byte channel, byte note, byte velocity) {
  digitalWrite(ledPin, LOW);  // Any Note-Off turns off LED
}

void setup() {
  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();
}

Uploaded it to Teensy and used my DAW to send a midi note, and the LED did not work as described in the information.

I thought I might be able to use something like this?
 
Yes, this way, where the DAW is a USB host (and thus the Teensy 3.2 the USB slave) it works. But if you use a Keyboard (which normally is a USB slave and on its port it's written USB to host), you'll need an USB host at the other end of the cable and that's where the T3.2 would not work.
 
Ok that makes sense.

However, why would the example shown above not work when my DAW plays a midi note, or my keyboard plays a midi note. I don't get any response on the Teensy LED 13.
 
Because a simple and quick but analytic look at the code tells you that the LED would flash only once at startup (in the setup() routine). There is no other code running continuously (the loop() routine) to do whatever with the LED. This code example just reads the USB Midi input and does exactly nothing with it.
 
Hmm missed that part. So would I test to see if a key is played, the LED lights up on noteOn, and off when noteOff?
 
Yes, just write the code which does what you want. There is no witchcraft, just systematic h/w and s/w engineering. If you haven't yet learned coding in C++, it's time to learn it now. You might study the reference on the PJRC midi and usb-midi documentation pages and load different example sketches and learn from these. But please do not expect the volunteers here to write custom code for you.
 
I did not expect anyone to write anything for me? I just simply asked if that was the procedure. Some may not be as proficient as yourself, hence why they are here to clarify their thoughts on proceeding down a certain logical path.
 
Sorry, I thought it was a rhetoric question because it's indeed the common way to write two event handler functions, one for NoteOn and one for NoteOff, and in your specific case the NoteOn handler would light the LED and the NoteOff handler would switch it off.
 
Status
Not open for further replies.
Back
Top