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

Thread: incoming midi note to trigger LEDs on different Teensy pins

  1. #1
    Junior Member
    Join Date
    Aug 2018
    Posts
    8

    incoming midi note to trigger LEDs on different Teensy pins

    i need some help understanding the way the below example by Alessandro Fasan works, so that i can then expand it to do something a bit different: rather than just flashing an LED on pin 13, respond to 3 different incoming midi note values (any, totally arbitrary) so that each trigger an LED on different pins.

    i'm using Teensy 3.5, btw.

    any pointers will be much appreciated. i'm sure something along these lines must exist in the forums already, cause it's quite basic i assume, but i was not able to find it.


    // 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
    }

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

  2. #2
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,108
    In the OnNoteOn and OnNoteOff functions you just need to examine the value of note and use that to determine which LED to use.
    A simplistic version might be:
    Code:
    void OnNoteOn(byte channel, byte note, byte velocity) {
      if(note == C4) {
        digitalWrite(ledC4, HIGH);
      }
      if(note == D4) {
        digitalWrite(ledD4, HIGH);
      }
      if(note == E4) {
        digitalWrite(ledE4, HIGH);
      }
    }
    
    void OnNoteOff(byte channel, byte note, byte velocity) {
      if(note == C4) {
        digitalWrite(ledC4, LOW);
      }
      if(note == D4) {
        digitalWrite(ledD4, LOW);
      }
      if(note == E4) {
        digitalWrite(ledE4, LOW);
      }
    }
    You can define C4, D4, E4 to be whatever note numbers you like and similarly define which pin numbers to use for ledC4, ledD4, ledE4

    Pete

  3. #3
    Junior Member
    Join Date
    Aug 2018
    Posts
    8
    thanks! so useful!

Posting Permissions

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