MIDI code strange behaviour

Hi,
I have this code where I use MIDI to control some LEDs.
Everything works when I use my MIDI keyboard, but when I use an audio software (Ableton Live) some functions don't work.
The keyboard sends NoteOn message 90 3C 64 and NoteOff 90 3C 00, and Ableton sends NoteOn 90 3C 64 and NoteOff 80 3C 40
I use the MIDI.h library.
Can anyone see why this is?
I can't
Cheers Hal

//MIDI
#include <MIDI.h> //MIDI Library
MIDI_CREATE_DEFAULT_INSTANCE();

const byte OMNI_CHANNEL = 8; // Change this to the desired OMNI channel

//LED
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

#define PCA9685_1_ADDRESS 0x40
#define PCA9685_2_ADDRESS 0x41

Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(PCA9685_1_ADDRESS);
Adafruit_PWMServoDriver pwm2 = Adafruit_PWMServoDriver(PCA9685_2_ADDRESS);

// Pins for each spotlight
const uint8_t spotlight1_pins[4] = {0, 1, 2, 3};      // Spotlight 1 pins (RGBW) on PCA9685 1
const uint8_t spotlight2_pins[4] = {4, 5, 6, 7};      // Spotlight 2 pins (RGBW) on PCA9685 1
const uint8_t spotlight3_pins[4] = {8, 9, 10, 11};    // Spotlight 3 pins (RGBW) on PCA9685 1
const uint8_t spotlight4_pins[4] = {12, 13, 14, 15};  // Spotlight 4 pins (RGBW) on PCA9685 2
const uint8_t spotlight5_pins[4] = {16, 17, 18, 19};  // Spotlight 5 pins (RGBW) on PCA9685 2
const uint8_t spotlight6_pins[4] = {20, 21, 22, 23};  // Spotlight 6 pins (RGBW) on PCA9685 2

// Predefined colors
const uint16_t red[4] = {3448, 0, 0, 0};
const uint16_t green[4] = {0, 3825, 0, 0};
const uint16_t blue[4] = {0, 0, 3825, 0};
const uint16_t white[4] = {0, 0, 0, 4088};
const uint16_t yellow[4] = {1917, 1917, 0, 0};
const uint16_t magenta[4] = {1917, 0, 1917, 0};
const uint16_t cyan[4] = {0, 1917, 1917, 0};

void setup() {
  MIDI.begin(MIDI_CHANNEL_OMNI); // Set OMNI channel
  //Serial.begin(9600);
  pwm1.begin();
  pwm2.begin();
  pwm1.setPWMFreq(1600);  // Frequency set to 1600 Hz
  pwm2.setPWMFreq(1600);
}

void setColor(uint8_t* pins, const uint16_t* color) {
  for (int i = 0; i < 4; i++) {
    if (pins[i] < 12) {
      pwm1.setPWM(pins[i], 0, color[i]);
    } else {
      pwm2.setPWM(pins[i] - 12, 0, color[i]);
    }
  }
}

void turnOffSpotlight(uint8_t* pins) {
  for (int i = 0; i < 4; i++) {
    if (pins[i] < 12) {
      pwm1.setPWM(pins[i], 0, 0);
    } else {
      pwm2.setPWM(pins[i] - 12, 0, 0);
    }
  }
}

void turnOffAllSpotlights() {
  turnOffSpotlight(spotlight1_pins);
  turnOffSpotlight(spotlight2_pins);
  turnOffSpotlight(spotlight3_pins);
  turnOffSpotlight(spotlight4_pins);
  turnOffSpotlight(spotlight5_pins);
  turnOffSpotlight(spotlight6_pins);
}

void allSpotlightsColor(const uint16_t* color) {
  setColor(spotlight1_pins, color);
  setColor(spotlight2_pins, color);
  setColor(spotlight3_pins, color);
  setColor(spotlight4_pins, color);
  setColor(spotlight5_pins, color);
  setColor(spotlight6_pins, color);
}

void setSpotlightColorWithVelocity(uint8_t* pins, const uint16_t* color, byte velocity) {
  uint16_t scaledColor[4];
  for (int i = 0; i < 4; i++) {
    scaledColor[i] = map(velocity, 0, 127, 0, color[i]); // Scale brightness based on velocity
  }
  setColor(pins, scaledColor);
}

void allSpotlightsColorWithVelocity(const uint16_t* color, byte velocity) {
  setSpotlightColorWithVelocity(spotlight1_pins, color, velocity);
  setSpotlightColorWithVelocity(spotlight2_pins, color, velocity);
  setSpotlightColorWithVelocity(spotlight3_pins, color, velocity);
  setSpotlightColorWithVelocity(spotlight4_pins, color, velocity);
  setSpotlightColorWithVelocity(spotlight5_pins, color, velocity);
  setSpotlightColorWithVelocity(spotlight6_pins, color, velocity);
}


void loop() {
  if (MIDI.read()) { // Process MIDI messages
    byte channel = MIDI.getChannel();
    byte data1 = MIDI.getData1();
    byte data2 = MIDI.getData2();

    // Check if the MIDI message is on the correct channel
    if (channel == OMNI_CHANNEL) {
      if (MIDI.getType() == midi::NoteOn) { // Note On message
        NoteOn(data1, data2);
      } else if (MIDI.getType() == midi::NoteOff) { // Note Off message
        NoteOff(data1, data2);
      } else if (MIDI.getType() == midi::ControlChange) { // Control Change message
        CCChange(data1, data2);
      }
    }
  }
}


void NoteOn(byte pitch, byte velocity) {
  switch (pitch) {
    //Spotlight 1
    case 24:  // Spotlight 1, RED
      setSpotlightColorWithVelocity(spotlight1_pins, red, velocity);
      break;
    case 25:  // Spotlight 1, GREEN
      setSpotlightColorWithVelocity(spotlight1_pins, green, velocity);
      break;
    case 26:  // Spotlight 1, BLUE
      setSpotlightColorWithVelocity(spotlight1_pins, blue, velocity);
      break;
    case 27:  // Spotlight 1, WHITE
      setSpotlightColorWithVelocity(spotlight1_pins, white, velocity);
      break;
    case 28:  // Spotlight 1, CYAN
      setSpotlightColorWithVelocity(spotlight1_pins, cyan, velocity);
      break;
    case 29:  // Spotlight 1, MAGENTA
      setSpotlightColorWithVelocity(spotlight1_pins, magenta, velocity);
      break;
    case 30:  // Spotlight 1, YELLOW
      setSpotlightColorWithVelocity(spotlight1_pins, yellow, velocity);
      break;
    case 31:  // Spotlight 1, WHITE
      setSpotlightColorWithVelocity(spotlight1_pins, white, velocity);
      break;
    case 32:
      turnOffSpotlight(spotlight1_pins);
      break;

  }
}

void NoteOff(byte pitch, byte velocity) {
  switch (pitch) {
    case 31:
      turnOffSpotlight(spotlight1_pins);
      break;    
    case 32:
      turnOffSpotlight(spotlight1_pins);
      break;
  }
}

void CCChange(byte controller, byte value) {
  if (controller == 9) { // Check if CC number is 9
  }
}


so NoteOn case 24 - 30 works on both keyboard and Ableton, but NoteOn case 31 and 32 and NoteOff case 31 and 32 don't

I know the code is only looking for MIDI on channel 8, and the MIDI I send is on that channel, so really it's sending 97 3C 64 and 87 3C 40

Basically your keyboard is sending a NoteOn with velocity 0, which is the same as a noteOff. While Ableton is sending an 'actual' noteOff. You handle then in different way, but you shouldn't.

easiest solution is just to change a received noteOff into a noteOn with velocity 0, like that you will have consistent behavior.

If a noteOn with velocity 0 doesn't result in the proper action, then you should modify your code so that it does.
Edit, or vice versa of course.
a simple

void NoteOn(byte pitch, byte velocity) {
  if (!velocity) {
     noteOff(pitch, velocity);
     return;
  }
  switch (pitch) {   // etc...

Hi,
Thank you for your answer.
The MIDI.h library already reads NoteOn Velocity 0 as NoteOff

Also in NoteOn cases 24-30 works with both keyboard and software, and even if I let go of the key, and the keyboard actually sends a NoteOn with velocity 0, the spotlight does not turn off.
That part works like it should.
What I don't understand is why NoteOn case 31 and 32 works with the keyboard, and not with the software.

I AM AN IDIOT!
Sorry for wasting your time.
Totally my stupid mistake.
The problem was that I had made a MIDI Effect rack on the channel, with most notes, but not all, so when I introduced new MIDI notes in the code, nothing happened because they where not in the effect rack.

That is the default setting, yes.

Yeah, none of us has ever made any kind of mistake like that. :expressionless:

But no a waste of time. I try to learn something every day, threads about MIDI, about which I do not know nearly what I want, always tip me off to something.

And in general, stripping away as much of what isn't oart of the problem as possible before you start tearing at your (or our!) hair is a good approach. Here it would have meant simplifying your MIDI setup or your f/x chain.

a7