Midi note on with flashing led

Hi I've got the led to light when triggered via note on. But I struggling to get it to loop flash when activated. Many thanks

/* Receive Incoming USB MIDI by reading data.  This approach
   gives you access to incoming MIDI message data, but requires
   more work to use that data.  For the simpler function-based
   approach, see InputFunctionsBasic and InputFunctionsComplete.

   Use the Arduino Serial Monitor to view the messages
   as Teensy receives them by USB MIDI

   You must select MIDI from the "Tools > USB Type" menu

   This example code is in the public domain.
*/
int ledPin=13;

void setup() {
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  delay(400);                 // Blink LED once at startup
  digitalWrite(ledPin, LOW);
}

void loop() {
  // usbMIDI.read() needs to be called rapidly from loop().  When
  // each MIDI messages arrives, it return true.  The message must
  // be fully processed before usbMIDI.read() is called again.
  if (usbMIDI.read()) {
    processMIDI();
  }
}

void processMIDI(void) {
  byte type, channel, data1, data2, cable;

  // fetch the MIDI message, defined by these 5 numbers (except SysEX)
  //
  type = usbMIDI.getType();       // which MIDI message, 128-255
  channel = usbMIDI.getChannel(); // which MIDI channel, 1-16
  data1 = usbMIDI.getData1();     // first data byte of message, 0-127
  data2 = usbMIDI.getData2();     // second data byte of message, 0-127
  cable = usbMIDI.getCable();     // which virtual cable with MIDIx8, 0-7

  // uncomment if using multiple virtual cables
  //Serial.print("cable ");
  //Serial.print(cable, DEC);
  //Serial.print(": ");

  // print info about the message
  //
  switch (type) {
    case usbMIDI.NoteOff: // 0x80
    if (data1==0x3C){
       digitalWrite(ledPin, LOW);  // turns off LED
    }
      break;

    case usbMIDI.NoteOn: // 0x90
    if (data1==0x3C){
    digitalWrite(ledPin, HIGH);   // set the LED on
  delay(1000);                  // wait for a second
  digitalWrite(ledPin, LOW);    // set the LED off
  delay(1000);                  // wait for a second
    }
      break;

    case usbMIDI.AfterTouchPoly: // 0xA0
      Serial.print("AfterTouch Change, ch=");
      Serial.print(channel, DEC);
      Serial.print(", note=");
      Serial.print(data1, DEC);
      Serial.print(", velocity=");
      Serial.println(data2, DEC);
      break;

    case usbMIDI.ControlChange: // 0xB0
      Serial.print("Control Change, ch=");
      Serial.print(channel, DEC);
      Serial.print(", control=");
      Serial.print(data1, DEC);
      Serial.print(", value=");
      Serial.println(data2, DEC);
      break;

    case usbMIDI.ProgramChange: // 0xC0
      Serial.print("Program Change, ch=");
      Serial.print(channel, DEC);
      Serial.print(", program=");
      Serial.println(data1, DEC);
      break;

    case usbMIDI.AfterTouchChannel: // 0xD0
      Serial.print("After Touch, ch=");
      Serial.print(channel, DEC);
      Serial.print(", pressure=");
      Serial.println(data1, DEC);
      break;

    case usbMIDI.PitchBend: // 0xE0
      Serial.print("Pitch Change, ch=");
      Serial.print(channel, DEC);
      Serial.print(", pitch=");
      Serial.println(data1 + data2 * 128, DEC);
      break;

    case usbMIDI.SystemExclusive: // 0xF0
      // Messages larger than usbMIDI's internal buffer are truncated.
      // To receive large messages, you *must* use the 3-input function
      // handler.  See InputFunctionsComplete for details.
      Serial.print("SysEx Message: ");
      printBytes(usbMIDI.getSysExArray(), data1 + data2 * 256);
      Serial.println();
      break;

    case usbMIDI.TimeCodeQuarterFrame: // 0xF1
      Serial.print("TimeCode, index=");
      Serial.print(data1 >> 4, DEC);
      Serial.print(", digit=");
      Serial.println(data1 & 15, DEC);
      break;

    case usbMIDI.SongPosition: // 0xF2
      Serial.print("Song Position, beat=");
      Serial.println(data1 + data2 * 128);
      break;

    case usbMIDI.SongSelect: // 0xF3
      Serial.print("Sond Select, song=");
      Serial.println(data1, DEC);
      break;

    case usbMIDI.TuneRequest: // 0xF6
      Serial.println("Tune Request");
      break;

    case usbMIDI.Clock: // 0xF8
      Serial.println("Clock");
      break;

    case usbMIDI.Start: // 0xFA
      Serial.println("Start");
      break;

    case usbMIDI.Continue: // 0xFB
      Serial.println("Continue");
      break;

    case usbMIDI.Stop: // 0xFC
      Serial.println("Stop");
      break;

    case usbMIDI.ActiveSensing: // 0xFE
      Serial.println("Actvice Sensing");
      break;

    case usbMIDI.SystemReset: // 0xFF
      Serial.println("System Reset");
      break;

    default:
      Serial.println("Opps, an unknown MIDI message type!");
  }
}


void printBytes(const byte *data, unsigned int size) {
  while (size > 0) {
    byte b = *data++;
    if (b < 16) Serial.print('0');
    Serial.print(b, HEX);
    if (size > 1) Serial.print(' ');
    size = size - 1;
  }
}

Welcome to the forum.'

TBC, you would like the LED to blink when (as long as) a note is on, that is to say between note on and note off.

If that is right, what means holding two keys down, or more? Blink while any key is (still) down?

Also further wondering… should the blinking be synchronized? Should the first light from the lED when you press the key be a full one second long, or whatever lenth you choose, or would it be OK to come into the middle of a continuing on/off rhythm of the LED?

If the period was 500 ms on, 500 off, you might not see the LED for 500 ms worst case after pressing a key.

I vote for synchronized, but it makes a bit more challenge.

Say anything more we might wonder about. Too much info better than too little.

a7

Hi, I'm linking the teensy midi to Vmix which will send the note on via activator while the record is on. so yes I would like the led to blink whilst it is receiving the note on. Thanks

I thought you get a note on message when the key goes down, and a note off message when the key goes up.

So whatever receives the note on only gets it once.

AmIRight? So again, the LED should blink after the not on and until the note off?

And again, what about multiple keys?

And again, does the blinking need to start fresh with the first note on, so you get LED right away, or can we fall into an implicit rhythm of blinking that is not synchronized?

If you code without answering questions like this, the code will do something and provide its own answers. The more you can specify what the code should do, the easier it will be to write the first time.

Conceptually simple, see if this makes sense:

    if the note goes on, set a flag
    if the note goes off, clear that flag


// elsewhere in your loop

   if the flag is set
       advance the blinking process

Where the blinking process must be non-blocking, as since in the IDE example "blink without delay", which also used as a google search will turn up the hole idea of non-blocking code and how to blink an LED with one hand whilst looking for keyboard activities and stuff on the other hand.

a7

Thanks - AmIRight ? So again, the LED should blink after the not on and until the note off?
Yes you are correct. It will only be triggered via 1 key. Thanks

OK, again again…

Does the blinking need to start fresh with the first note on, so you get LED right away, or can we fall into an implicit rhythm of blinking that is not synchronized?

To which I might add what are you planning for on time and off time of this alleged blinking LED?

Also, it is OK to extinguish the LED immediately the note off message is sent, or should it stay on (if it was at that instant) for the full on period you have selected?

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.