Switch(case) and MIDI

Dear people,

I’ve got a problem using the MIDI-library in conjunction with the switch-command.
What I’d like to do is, filtering out the velocity of a note and forward it to an LED which is just turned on / off.
Here is my code so far:

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();

void setup(){
  MIDI.begin(MIDI_CHANNEL_OMNI);
  MIDI.setHandleNoteOn(Note);
  digitalWrite(13,LOW);
}

void loop() {
  MIDI.read();
}

void Note(byte channel, byte note, byte velocity) {
  switch (velocity) {
    case 5:
      digitalWrite(13,HIGH);
      break;
    case 10:
      digitalWrite(13,LOW);
      break;
  }
}

So in theory the pin 13 should be high when receiving whatever note with velocity 5 and low when receiving a velocity of 10. But it’s not working.
I tried it with this code:

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();

void setup(){
  MIDI.begin(MIDI_CHANNEL_OMNI);
  MIDI.setHandleNoteOn(Note);
  digitalWrite(13,LOW);
}

void loop() {
  MIDI.read();
}

void Note(byte channel, byte note, byte velocity) {
  switch (note) {
    case 60:
      digitalWrite(13,HIGH);
      break;
    case 61:
      digitalWrite(13,LOW);
      break;
  }
}

And that’s working! So… what I’m missing?

Any help on this?

Thanks,
Chris

You might want to add a Serial.println to that handler to see if it actually gets called and
if so what parameters are passed to it - you assume it will get velocity=5 or 10, but
assumptions may be wrong.

(If the handler happens to be called in an interrupt that won't work though)

  switch (note) {

You are looking at the note value, not the velocity. There is absolutely no reference to 5 or 10 in your code.
And unless you have a way of specifically controlling the velocity, using a switch to look for 5 or 10 is probably not going to work either.

Pete

There are two versions of the code, its the first one that refers to 5 and 10

MarkT:
You might want to add a Serial.println to that handler to see if it actually gets called and
if so what parameters are passed to it - you assume it will get velocity=5 or 10, but
assumptions may be wrong.

(If the handler happens to be called in an interrupt that won't work though)

The problem is, that the serial monitor doesn't support the baudrate of a MIDI... so I println or serial.print won't work here...

Thanks MarkT. I managed to misread the whole thing!.

If you are generating those notes by pressing a note on a keyboard, what are the odds that the velocity will be either exactly 5 or exactly 10?

In the version which uses switch(velocity) remove the entire switch statement and try this instead:

    if(velocity > 30)digitalWrite(13,HIGH);
    else digitalWrite(13,LOW);

I picked 30 almost at random. You can try it with other values.
Pete

Dear Pete,

that works!

Okay guys… I don’t know why… but now my code posted in the first post works perfectly…

Strange…

Thanks anyway! If there are any problems again I’ll let you know!