Midiusb work with max7219

i dont understan how the midiusb library receive the message.
what i want to do is when the arsuino receive note42 velocity 127 to turn on a led.
and when arduino receive note 42 velocity 0 to turn off the led.

something i do very wrong in the code.

i add a line to the setup just to see if the code will light the led, and is ok.. so no conection problem.

any idea what i am missing


#include "MIDIUSB.h"// Library from here https://github.com/FortySevenEffects/arduino_midi_library
#include "LedControl.h"
LedControl lc = LedControl(10, 8, 9, 1);

void setup() {
  
  lc.shutdown(0, false);
  
  lc.setIntensity(0, 8);
  
  lc.clearDisplay(0);

  Serial.begin(115200);
  lc.setLed (0, 1, 1, true); // turn 1 led on to see if the code is working
}

void loop() {
  midiEventPacket_t rx;
  do {
    rx = MidiUSB.read();
    if (rx.header == 42) {  // if note 42 comes in then one led will lit in max7219
      lc.setLed (0, 7, 7, true);

    }
  }  while (rx.header == 0); // if there no not come in the led will turn off
  lc.setLed (0, 7, 7, false);
}
void noteOff(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOff);
}

void controlChange(byte channel, byte control, byte value) {
  midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
  MidiUSB.sendMIDI(event);

}

void noteOn(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOn);
}

Why on earth do you need a library to turn on and off an LED? It is the single most basic thing to do on an Arduino. People who think this is helpful are very very wrong.
Also why are you using pin 1 to hang an LED on.

What Arduino are you using?

Your loop function is all wrong. Try the examples in the examples that come with that library, and build on that.

oups i miss to mension that i use the MAX7219.
i dont want just 1 led, i need more about 50 to do deferent think..
i start to make just one to be on and off when receive the midi message, and then i will expand the code to my need.. IF I understant how thinks working

i cant get it how thinks work.. and to adopt it to my need.

Use the MIDIUSB_loop.ino example. This reads a MIDI message from the USB and sends the same byte back.

What you need to do is when you get a noteOn message, modify the code to call another function that looks at the bytes in this message and decides what LED to turn on or off depending on the velocity. Then to be sure do the same thing for the MIDI note off message and irrespective of the velocity message turn your LED off.

good new, i manage it to work. but i assume is working anly because it read the velocity if is 127 or 0.
i want to make it read the note, in my case note is 42 and velocity is 127 or 0

will this work?

void led(byte note, byte velocity) {

  if (note == 42 & velocity == 127) {
    lc.setLed(0, 7, 1, true);
  }
  else {
    if (note == 42 & velocity == 0) {
    lc.setLed (0, 7, 1, false);

    }
  }
}

#include "MIDIUSB.h"// Library from here https://github.com/FortySevenEffects/arduino_midi_library
#include "LedControl.h"
LedControl lc = LedControl(10, 8, 9, 1);

void setup() {

  lc.shutdown(0, false);

  lc.setIntensity(0, 8);

  lc.clearDisplay(0);

  Serial.begin(115200);
  lc.setLed (0, 1, 1, true); // turn 1 led on to see if the code is working
}

void loop() {
  midiEventPacket_t rx;
  do {
    rx = MidiUSB.read();

    if (rx.header == 0x9) led (rx.byte1, rx.byte3);
    if (rx.header == 0x8) led(rx.byte1, rx.byte3);

  }
while (rx.header == 0); // hold until a MIDI message is received
}

void led(byte channel, byte velocity) {

  if (velocity == 127) {
    lc.setLed(0, 7, 1, true);
  }
  else {
    if (velocity == 0) {
      lc.setLed (0, 7, 1, false);
      
    }
  }
}
  void noteOff(byte channel, byte pitch, byte velocity) {
    midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
    MidiUSB.sendMIDI(noteOff);
  }


  void controlChange(byte channel, byte control, byte value) {
    midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
    MidiUSB.sendMIDI(event);

  }

  void noteOn(byte channel, byte pitch, byte velocity) {
    midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
    MidiUSB.sendMIDI(noteOn);
  }

Looks like it, but why not try it?

i thought so, but NO is not working

I don't think you are sending the correct bytes. If you want the note number and the velocity you need to send bytes 2 and 3.

Then at the function end

You totally ignore the first number it receives where as it should be the note number and not the channel byte, you still ignore it. I thought you wanted it to be limited to a specific note. The actual name you use is irrelevant but having a note number go by the name of channel number is confusing.

Try to avoid repetitive code by factoring conditional statements like:

void led(byte note, byte velocity) {

  if (note == 42 & velocity == 127) {
    lc.setLed(0, 7, 1, true);
  }
  else {
    if (note == 42 & velocity == 0) {
    lc.setLed (0, 7, 1, false);

    }
  }
}

Note that you incorrectly used a bitwise and '&' instead of the boolean and '&&'. This is the same code, factored:

void led(byte note, byte velocity) {

  if (note == 42) {
    if (velocity == 127) {
      lc.setLed(0, 7, 1, true);
    }
    else if (velocity == 0) {
      lc.setLed (0, 7, 1, false);
    }
  }
}

ok it works.

void loop() {
  midiEventPacket_t rx;
  do {
    rx = MidiUSB.read();

    if (rx.header == 0x9) led ( rx.byte2, rx.byte3);
    if (rx.header == 0x8) led( rx.byte2, rx.byte3);

  }
  while (rx.header == 0); // hold until a MIDI message is received
}

void led(byte channel, byte velocity) {


  if (channel == 42) {
    if (velocity == 127) {
      lc.setLed(0, 7, 1, true);
    }
    else if (velocity == 0) {
      lc.setLed (0, 7, 1, false);
    }
  }
}

but i didnt understand why when i use all 3 bytes and ignore the 1 code is not wotking.
i change many thinks but no luck.

if (pitch == 42) {
    if (velocity == 127) {
      lc.setLed(0, 7, 1, true);
if (channel == 42) {
    if (velocity == 127) {
      lc.setLed(0, 7, 1, true);
void loop() {
  midiEventPacket_t rx;
  do {
    rx = MidiUSB.read();

    if (rx.header == 0x9) led ( rx.byte1, rx.byte2, rx.byte3);
    if (rx.header == 0x8) led( rx.byte1, rx.byte2, rx.byte3);

  }
  while (rx.header == 0); // hold until a MIDI message is received




void led(byte channel, byte pitch, byte velocity) {


  if (channel == 42) {
    if (velocity == 127) {
      lc.setLed(0, 7, 1, true);
    }
    else if (velocity == 0) {
      lc.setLed (0, 7, 1, false);
    }
  }
}

any way lets stay with the one is working. now lets add more notes to light up leds. in my case will be around 40. i dont think i can use the same patern like below. because only the note 42 is working, the note 40 dos nothing. also the code will be so large.

void led(byte channel, byte velocity) {


  if (channel == 42) {
    if (velocity == 127) {
      lc.setLed(0, 7, 1, true);
    }
    else if (velocity == 0) {
      lc.setLed (0, 7, 1, false);
    }
    if (channel == 40) {
      if (velocity == 127) {
        lc.setLed(0, 6, 1, true);
      }
      else if (velocity == 0) {
        lc.setLed (0, 6, 1, false);
      }
    }
  }
}

i think i need to use case, or what is your idea how to write the code for so many leds. like i said leds will be around 40, each let will lit up with a specific note and velocity, and turn off with same way.
the code below is make the 2 leds lit all time, even if no midi note was send.
and when midi note sent, nothing happen.

void loop() {
  midiEventPacket_t rx;
  do {
    rx = MidiUSB.read();

    if (rx.header == 0x9) ledon( rx.byte2, rx.byte3);
    if (rx.header == 0x8) ledoff( rx.byte2, rx.byte3);

  }
  while (rx.header == 0); // hold until a MIDI message is received
}

void ledon(byte channel, byte velocity) {


  switch (channel) {
    case 42:
      lc.setLed(0, 7, 1, true);
      break;

    case 40:
      lc.setLed(0, 6, 1, true);
      break;
  }
}

void ledoff(byte channel, byte velocity) {


  switch (channel) {
    case 42:
      lc.setLed(0, 7, 1, false);
      break;

    case 40:
      lc.setLed(0, 6, 1, false);
      break;
  }
}

Why are you still defining the first parameter as channel and not note number? Makes no difference to the cod but an enormous difference to your comprehension.

That is what arrays are used for. Use a look up table for translating the pitch number into the led number.

sory for the bad defining the parameters, i am not a master of coding.

why to use pitch? i only have notes and velocity.

can you please give me un examble?

Pitch and MIDI note number are the same thing, as far as you are concerned here.

give me more details please.

you said to translate the pitch? explain me please how thinks work to figure out.
or any link to read.

because those 40 led, will be on or off. then i need more 20+ led for vu meter.
so there is a deferent code for those.

i found this post, but is not very clear to me how to impliment it in my code. i think my solution in my problem is here

on first line is the control change, how to make it to be the NOTE
on second line is the channel, ok this i understan it. if i want the channel 5 i need to change number 2 to 4
line number 3 is the velocity i assume. ok here is clear too if i want velocity 0 to do something i just neet to turn ==1 to ==0

1) )if((rx.header >> 4) == 0b1011) { // control change
2)  if((rx.header & 0b1111) == 2) { // channel 3 (channel 1 has value 0)
3)    if(rx.byte1 == 1) { // Modulation wheel
4)      digitalWrite(ledPin, rx.byte2 >> 6); // 0 >> 6 = 0 and 127 >> 6 = 1
      }

This could be useful: Control Surface: 6.MAX7219-NoteLED.ino

If you're interested in note 42 only, let's say on channel 5, then you would use:

// Instantiate the LED that lights up when note 42 is playing
NoteLED led {
  max7219.pin(0),
  {42, CHANNEL_5},
};

If you want e.g. 20 LEDs, you could use the NoteRangeLEDs class:

// Instantiate a range of LEDs that light up when notes 42-61 are playing
NoteRangeLEDs<20> led {
  max7219.pins().slice<0, 19>(), // First 20 pins of the MAX7219
  {42, CHANNEL_5},
};

no i am not interesting in note 42 only. in my mind is that, i have 127 note for each channel.
so with ch2 I CAN CONTROL 127 LEDS.

each button i press in softwear send it own note and channel to the arduino. if button A

+72.303 - MIDI In: Ch 2: Note 42 on velocity 127
+72.303 - MIDI In: Ch 2: Note 42 on velocity 0

BUTTON B

+361.932 - MIDI In: Ch 2: Note 43 on velocity 127
+362.39 - MIDI In: Ch 2: Note 43 on velocity 0

i assuming you suggest not to use the

#include "LedControl.h" library

but the

#include <Control_Surface.h>

You can use either, but Control Surface might be easier because its MAX7219 class integrates well with the MIDI-related parts of the library.
If you want to use LedControl.h, you can, of course, but you'll have to write a bit more logic by hand.