If you want to continue to use the MIDI library, you can simplify your code a lot by using the event handlers. To do that, in your setup function:
// Initiate MIDI communications, listen to all channels, turn off thru mode
MIDI.begin(MIDI_CHANNEL_OMNI);
// By default, the MIDI library sends everything THRU. We do NOT want that!
MIDI.turnThruOff();
// Connect MIDI status changes involving a channel to handlers
MIDI.setHandleNoteOn(HandleNoteOn);
MIDI.setHandleNoteOff(HandleNoteOff);
Then, create a function for handling note on and note off:
void HandleNoteOn(byte channel, byte pitch, byte velocity) {
// check your pitch, velocity, and channel here
// turn on the LED matching pitch
}
void HandleNoteOff(byte channel, byte pitch, byte velocity) {
// check your pitch, velocity, and channel here
// turn off the LED matching pitch
}
Now your loop function is simple:
void loop() {
MIDI.read();
}