# MIDI code strange behaviour

**URL:** <https://forum.arduino.cc/t/midi-code-strange-behaviour/1356622>\
**Category:** Programming\
**Created:** [February 21, 2025, 10:49pm UTC](https://forum.arduino.cc/t/midi-code-strange-behaviour/1356622 "2025-02-21T22:49:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![eptheca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/eptheca/32/214090_2.png) [@eptheca](https://forum.arduino.cc/u/eptheca)\
**Post date:** [February 21, 2025, 10:49pm UTC](https://forum.arduino.cc/t/midi-code-strange-behaviour/1356622/1 "2025-02-21T22:49:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eptheca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/eptheca/32/214090_2.png) [@eptheca](https://forum.arduino.cc/u/eptheca)\
**Post date:** [February 21, 2025, 10:53pm UTC](https://forum.arduino.cc/t/midi-code-strange-behaviour/1356622/2 "2025-02-21T22:53:38Z")

</div>

```arduino
//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
  }
}

```

---

<div class="post-metadata">

**Author:** ![eptheca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/eptheca/32/214090_2.png) [@eptheca](https://forum.arduino.cc/u/eptheca)\
**Post date:** [February 21, 2025, 10:56pm UTC](https://forum.arduino.cc/t/midi-code-strange-behaviour/1356622/3 "2025-02-21T22:56:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eptheca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/eptheca/32/214090_2.png) [@eptheca](https://forum.arduino.cc/u/eptheca)\
**Post date:** [February 22, 2025, 8:47am UTC](https://forum.arduino.cc/t/midi-code-strange-behaviour/1356622/4 "2025-02-22T08:47:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Deva\_Rishi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/deva_rishi/32/227254_2.png) [@Deva\_Rishi](https://forum.arduino.cc/u/Deva_Rishi)\
**Post date:** [February 22, 2025, 9:42am UTC](https://forum.arduino.cc/t/midi-code-strange-behaviour/1356622/5 "2025-02-22T09:42:25Z")

</div>

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.

> [@eptheca](#):
>
> ```arduino
> case 24: // Spotlight 1, RED
> setSpotlightColorWithVelocity(spotlight1_pins, red, velocity);
> 
> ```

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

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

```

---

<div class="post-metadata">

**Author:** ![eptheca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/eptheca/32/214090_2.png) [@eptheca](https://forum.arduino.cc/u/eptheca)\
**Post date:** [February 22, 2025, 10:25am UTC](https://forum.arduino.cc/t/midi-code-strange-behaviour/1356622/6 "2025-02-22T10:25:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![eptheca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/eptheca/32/214090_2.png) [@eptheca](https://forum.arduino.cc/u/eptheca)\
**Post date:** [February 22, 2025, 10:58am UTC](https://forum.arduino.cc/t/midi-code-strange-behaviour/1356622/7 "2025-02-22T10:58:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Deva\_Rishi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/deva_rishi/32/227254_2.png) [@Deva\_Rishi](https://forum.arduino.cc/u/Deva_Rishi)\
**Post date:** [February 22, 2025, 11:09am UTC](https://forum.arduino.cc/t/midi-code-strange-behaviour/1356622/8 "2025-02-22T11:09:24Z")

</div>

> [@eptheca](#):
>
> The MIDI.h library already reads NoteOn Velocity 0 as NoteOff

That is the default setting, yes.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [February 22, 2025, 3:32pm UTC](https://forum.arduino.cc/t/midi-code-strange-behaviour/1356622/9 "2025-02-22T15:32:10Z")

</div>

> [@eptheca](#):
>
> I AM AN IDIOT!  
> Sorry for wasting your time.

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

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 21, 2025, 3:32pm UTC](https://forum.arduino.cc/t/midi-code-strange-behaviour/1356622/10 "2025-08-21T15:32:38Z")

</div>

Closed on August 21, 2025, 3:32pm UTC

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