incoming midi note to trigger LEDs on different Teensy pins

Status
Not open for further replies.

beeble

Member
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();
}
 
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
 
Status
Not open for further replies.
Back
Top