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

Thread: Store a midi keyboard value

  1. #1

    Store a midi keyboard value

    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

  2. #2
    Senior Member oddson's Avatar
    Join Date
    Feb 2013
    Location
    Isle in the Salish Sea
    Posts
    1,351
    Paul has updated usbMIDI support and with Teensy 3.6's native USB Host option you can read incoming usbMIDI from USB devices.

    I've not tried it yet.

    https://forum.pjrc.com/threads/43450...lopment-status
    Paul gets it mostly working near the bottom of the first page and then it's all data dumps and debugging

  3. #3
    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.

  4. #4
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,583
    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.

  5. #5
    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.

  6. #6
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,583
    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.

  7. #7
    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?

  8. #8
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,583
    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.

  9. #9
    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.

  10. #10
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,583
    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.

  11. #11
    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?

  12. #12
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,583
    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.

  13. #13
    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.

  14. #14
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,583
    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.

Posting Permissions

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