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

Thread: Midi

  1. #1
    Junior Member
    Join Date
    Oct 2021
    Posts
    3

    Midi

    Hi I'm trying to get in to Teensy 4.0 with a simple project.

    By midi notes I would like to make a digitalWrite to specifics pins if it's a MIDI send G, E, and so one. If I'm using the code i can get it to work with key down. But how do i select a specific note?

    I think that i could do something like

    void setup() {
    if(note=60){
    pinMode(ledPin, OUTPUT);
    usbMIDI.setHandleNoteOff(OnNoteOff);
    usbMIDI.setHandleNoteOn(OnNoteOn) ;
    digitalWrite(ledPin, HIGH);
    delay(400); // Blink LED once at startup
    digitalWrite(ledPin, LOW);
    }
    }


    Code from tutorial.

    // 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 PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    651
    You could do something like this:
    Code:
    void myNoteOn(byte channel, byte note, byte velocity) {
      if (note = 35) {
        digitalWrite(ledPin, HIGH); // Note 35 turns on onboard LED
      }
    }
    
    void myNoteOff(byte channel, byte note, byte velocity) {
      if (note > 35) {
        digitalWrite(ledPin, LOW);  // Note 35 turns off oboard LED
      }
    }
    Paul

  3. #3
    Junior Member
    Join Date
    Oct 2021
    Posts
    3
    Thanks a lot.

    As I understand it, only pin 13 should be activated when a C4 is sent (note60) But no matter which note I send, it is activated.

    Any ideas for what I'm doing wrong?

    Code:
    int ledPin = 13;
    
    void OnNoteOn(byte channel, byte note, byte velocity) {
      if (note = 60) {
        digitalWrite(ledPin, HIGH); // Note 60 turns on onboard LED
      }
    }
    
    void OnNoteOff(byte channel, byte note, byte velocity) {
        if (note > 60) {
        digitalWrite(ledPin, LOW);  // Note 60 turns off oboard 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();
    }

  4. #4
    Junior Member
    Join Date
    Oct 2021
    Posts
    3
    It's working now! Thanks a lot.

    Code:
    void OnNoteOn(byte channel, byte note, byte velocity) {
      if (note == 60) {
        digitalWrite(ledPin, HIGH); // Note 60 turns on onboard LED
      }
    }

  5. #5
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    651
    Yeah... the == got me again...

    Paul

Posting Permissions

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