# Not sure why my LED won't light up?

**URL:** <https://forum.arduino.cc/t/not-sure-why-my-led-wont-light-up/682736>\
**Category:** Programming\
**Created:** [November 14, 2020, 5:55pm UTC](https://forum.arduino.cc/t/not-sure-why-my-led-wont-light-up/682736 "2020-11-14T17:55:17Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![markbowenpiano](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/markbowenpiano/32/541742_2.png) [@markbowenpiano](https://forum.arduino.cc/u/markbowenpiano)\
**Post date:** [November 15, 2020, 1:55pm UTC](https://forum.arduino.cc/t/not-sure-why-my-led-wont-light-up/682736/21 "2020-11-15T13:55:57Z")

</div>

Is there something wrong with for instance this very simple code?

```cpp
      int startLED = ledToLight * 2; // LED to light up first for given key
      int endLED = startLED + 1; // LED + 1

```

When I try to use those variables (hope I'm using the right word there?) in my LED light up code as below :

```cpp
      leds[startLED] = CRGB::Red;
      leds[endLED] = CRGB::Red;

```

then it all goes wrong but if I comment those lines out and instead hard code them to be something like :

```cpp
int startLED = 0;
int endLED = 1;
leds[startLED] = CRGB::Red;
leds[endLED] = CRGB::Red;

```

then the LEDs light up fine!

I must be missing some very simple programming aspect somewhere but just unfortunately not sure of what?  
Am I perhaps not making the variables in the right way and when I hard code them I am?

---

<div class="post-metadata">

**Author:** ![Blackfin](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blackfin/32/238871_2.png) [@Blackfin](https://forum.arduino.cc/u/Blackfin)\
**Post date:** [November 15, 2020, 2:15pm UTC](https://forum.arduino.cc/t/not-sure-why-my-led-wont-light-up/682736/22 "2020-11-15T14:15:33Z")

</div>

> markbowenpiano:  
> Hi again,  
> Thanks so much for keeping going with me on this one, it's really very much appreciated.  
> I'd done that in my code because my coding isn't very good at all and what I was doing was taking the note number that's being played so lets say the second key on the piano which is 1 (as they start at 0) and then if I times the number by 2 then I get the first LED which needs to be lit as I ultimately want to have 2 LEDs lit up per key. So taking the note number I can find the first LED by timesing by 2. Then using that number + 1 gives me the second LED to light up for that key.

Ah right. I'm an idiot: Yes, you want two LEDs per so have to count up by 2s...

I'll edit my last code to reflect that.

> I'm really honestly seriously greatful for the code you keep throwing my way although I'd love to be able to understand why the code I posted before doesn't work in any way I'm expecting it to.  
> It seems to be that I can either get one thing or another working but never the two together ☹  
> So as above where I posted my code, if I hard-code one of the LEDs to turn on when a key is pressed then it works but as soon as I try to insert those variables which figure out which LEDs to light up then it goes completely haywire on the serial monitor!  
> I'm really at a bit of a loss as to what I'm doing wrong though?  
> Best wishes,  
> Mark

Can you try changing the number of LEDs:

```arduino
#define NUM_LEDS 176  // # of LEDS in the strip

```

The behavior that appears when you enable writes to the led array suggests you may be writing outside the bounds of the array and stomping on other memory. If you go to the full size see if the behavior changes.

---

<div class="post-metadata">

**Author:** ![PieterP](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@PieterP](https://forum.arduino.cc/u/PieterP)\
**Post date:** [November 15, 2020, 5:44pm UTC](https://forum.arduino.cc/t/not-sure-why-my-led-wont-light-up/682736/23 "2020-11-15T17:44:00Z")

</div>

> markbowenpiano:  
> Trust me, I'm sure you're understanding this a LOT more than me 🙂 😉 🙂  
> To be honest I wasn't even sure what those 3 bytes represented and I was guessing just from what I was seeing coming up when I was pressing different keys that byte 3 is the velocity of how hard the key is pressed as it's always 0 when you let go of the key and different numbers when you hit the keys harder or softer so I was using that in my **if** clause to see if a note had been pressed or not.  
> I believe the first byte is the MIDI channel that is being transmitted, the second byte is the note value and the third byte is the velocity. That's what I took from it all anyway.

Don't guess, take a look at the standard: [https://www.usb.org/sites/default/files/midi10.pdf#page=16](https://www.usb.org/sites/default/files/midi10.pdf#page=16)

The first byte (`rx.header`) consists of two parts: the Cable Number (which should always be 0 for the MIDIUSB library) and the Code Index Number, which indicates what type of MIDI message is encoded in the USB packet. See the table in the link above, for simple note events, the Code Index Number is the same as the MIDI status byte but without the channel. For example, Note On is 0x9 and Note Off is 0x8.

The second, third and fourth bytes (`rx.byte1`, `rx.byte2`, `rx.byte3`) are the three MIDI bytes. The first one is the MIDI status byte, which consists of the message type and the MIDI channel. The status byte for a Note On event on channel 4 is 0x93, for example. A Note Off event on channel 1 is 0x80. Channels are zero-based.

The second MIDI byte is the note number in the case of note events. It's a number in [0, 127].

The third MIDI byte is the velocity of the note (how hard the key was struck). It's also a number in [0, 127]. A special case is a Note On event with velocity zero, which is equivalent to a Note Off event.

Pieter

---

<div class="post-metadata">

**Author:** ![Blackfin](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blackfin/32/238871_2.png) [@Blackfin](https://forum.arduino.cc/u/Blackfin)\
**Post date:** [November 15, 2020, 6:27pm UTC](https://forum.arduino.cc/t/not-sure-why-my-led-wont-light-up/682736/24 "2020-11-15T18:27:52Z")

</div>

The confusion for me is not the MIDI event structure but rather when @OP tried my code and got this result:

```cpp
Packet: 09 - 90 - 15 - 30
 A0 on
Packet: 09 - 90 - 15 - 00
 A0 on
.
.
.

```

Presumably a press and a release (he wasn't clear.) I never saw an 08 - 80 - xx - yy event indicating a note off event, which would be used to turn the LEDs off.

---

<div class="post-metadata">

**Author:** ![markbowenpiano](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/markbowenpiano/32/541742_2.png) [@markbowenpiano](https://forum.arduino.cc/u/markbowenpiano)\
**Post date:** [November 15, 2020, 6:56pm UTC](https://forum.arduino.cc/t/not-sure-why-my-led-wont-light-up/682736/25 "2020-11-15T18:56:18Z")

</div>

Hiya,

Yes the output above has pretty much always been note on and note off based. I really don't understand anything at all about all the bytes and even after reading the documentation for MIDI it's all unfortunately still gobbledegook to me ☹  
I'm definitely not someone who gets that sort of thing unfortunately.

When I was referring to the bytes the MIDIUSB code spits out 3 of them and from what I could find online about the library (may have been wrong of course as lots of people write things and they might not all be correct) it said that byte1 was the MIDI channel, byte2 the note being played and byte3 the velocity it's being played at. Although I don't understand HEX values at all (think that's probably obvious by now 😃 ) the output I was getting in the serial monitor seemed to confirm that to me.

So I was never getting anything like 0x8 or 0x9 shown to me but perhaps the MIDIUSB library shows it as something different? Of that I really wouldn't know though as I literally only know enough code to be dangerous! Again something which I think is probably totally apparent by now! 😃

---

<div class="post-metadata">

**Author:** ![slipstick](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@slipstick](https://forum.arduino.cc/u/slipstick)\
**Post date:** [November 15, 2020, 7:01pm UTC](https://forum.arduino.cc/t/not-sure-why-my-led-wont-light-up/682736/26 "2020-11-15T19:01:38Z")

</div>

That NoteOn x09 with velocity (the last element) of 00 is directly equivalent to a NoteOff for that note. In code you need to check for both versions.

Steve

---

<div class="post-metadata">

**Author:** ![markbowenpiano](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/markbowenpiano/32/541742_2.png) [@markbowenpiano](https://forum.arduino.cc/u/markbowenpiano)\
**Post date:** [November 15, 2020, 10:12pm UTC](https://forum.arduino.cc/t/not-sure-why-my-led-wont-light-up/682736/27 "2020-11-15T22:12:58Z")

</div>

> slipstick:  
> That NoteOn x09 with velocity (the last element) of 00 is directly equivalent to a NoteOff for that note. In code you need to check for both versions.
> 
> Steve

Hi Steve,  
I had thought that's what I was doing here with this code isn't it?

```arduino
if (rx.byte3 != 0) {
        //        Serial.println("Note on received");
        // Turn the LEDs on
        //                leds[startLED] = CRGB::Red;
        //        leds[endLED] = CRGB::Red;
        FastLED.show();
      }
      else {
        //        Serial.println("Note off received");
        // Now turn the LEDs off
        //                leds[startLED] = CRGB::Black;
        //        leds[endLED] = CRGB::Black;
        FastLED.show();
      }

```

That's what I thought I was doing when I wrote it anyway 🙂

---

<div class="post-metadata">

**Author:** ![markbowenpiano](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/markbowenpiano/32/541742_2.png) [@markbowenpiano](https://forum.arduino.cc/u/markbowenpiano)\
**Post date:** [November 15, 2020, 10:30pm UTC](https://forum.arduino.cc/t/not-sure-why-my-led-wont-light-up/682736/28 "2020-11-15T22:30:07Z")

</div>

What I'm absolutely stumped on _(and I must be missing something totally obvious here to do with coding)_ is in my code here :

```cpp
void loop() {

  midiEventPacket_t rx;
  do {
    rx = MidiUSB.read();
    if (rx.header != 0) {

      int ledToLight = rx.byte2 - 21; // Gives a number from 0-88
      int startLED = ledToLight * 2; // LED to light up first for given key
      int endLED = startLED + 1; // LED + 1

      if (rx.byte3 != 0) {
        // NOTE ON RECEIVED
        // Turn the LEDs on
        Serial.print("Key pressed = ");
        Serial.println(ledToLight);
        Serial.print("Start LED = ");
        Serial.println(startLED);
        Serial.print("End LED = ");
        Serial.println(endLED);
        leds[ledToLight] = CRGB::Red;
        FastLED.show();
      }
      else {
        // NOTE OFF RECEIVED
        // Turn the LEDs off
        leds[ledToLight] = CRGB::Black;
        FastLED.show();
      }

    }
  } while (rx.header != 0);
}

```

If I run that code then everything works absolutely perfectly as you would expect it to with the first key on the piano lighting up the first LED, the second piano key the second LED and so on and so forth _(currently I only have 4 LEDs connected as still waiting for LED strip delivery although I have now set the number of LEDs to 176 as suggested)_.

However the problem I have is as soon as I try to swap out this line :

```cpp
leds[ledToLight] = CRGB::Red;

```

for this line instead :

```cpp
leds[startLED] = CRGB::Red;

```

and the corresponding one to turn off the LEDs and the serial monitor goes completely crazy spitting out all sorts of stuff, looping over and over.

My one question that I'd love to have answered as it may help me understand a lot more is, **_why_** am I able to make that **ledToLight** variable and use it to tell the code which LED to light _(I believe the words I'm looking for are using the variable as the index to the LED array?)_ however I'm **_NOT_** able to use the **startLED** variable which I created?

When using **ledToLight** it just works as a single LED per piano key as the piano keys come in as the numbers **0-88** and that's all fine but why when trying to use the **startLED** variable do I get serial monitor output all the while instead of it just lighting up every other LED for each key?

That has me completely and utterly stumped as to why I'm able to create one variable and it works fine yet another variable creates havoc?

Is there something that I'm fundamentally not understanding with variables or something else that I'm just not seeing right now?  
I really just can't see why one variable would work yet not another?

Thanks,

Mark

---

<div class="post-metadata">

**Author:** ![Blackfin](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blackfin/32/238871_2.png) [@Blackfin](https://forum.arduino.cc/u/Blackfin)\
**Post date:** [November 15, 2020, 11:14pm UTC](https://forum.arduino.cc/t/not-sure-why-my-led-wont-light-up/682736/29 "2020-11-15T23:14:53Z")

</div>

So, going back to post #16:

You have this code snippet:

```cpp
.
.
.
 do {
    rx = MidiUSB.read();
    if (rx.header != 0) {

      int ledToLight = rx.byte2 - 21; // Gives a number from 0-88
      Serial.println(ledToLight);
      Serial.println("---------------------------");
      int startLED = ledToLight * 2; // LED to light up first for given key
      Serial.println(startLED);
      int endLED = startLED + 1; // LED + 1
      Serial.println(endLED);
      Serial.println("------------------------------------------------------");
.
.
.

```

The output of part of this is:

```cpp
226
---------------------------
452
453
------------------------------------------------------
0
---------------------------
0
1

```

The first problem I see is that ledToLight has a value of 226, clearly larger than 88. If we add 21 to that we get 247 which is 0xF7.

My guess is that your code assumes that every MIDI message is a noteOn or noteOff but the message you're getting is not one of those. Because of this, even with 176 LEDs in your array you're still way out of bounds on it and stomping through memory.

I think you should add a check to ensure that the message header low-nibble is '8' or '9' (you could also check if the upper nibble of byte1 is '8' or '9'.) See Table 4-2 in [https://www.usb.org/sites/default/files/midi10.pdf](https://www.usb.org/sites/default/files/midi10.pdf)

```cpp
19 9n kk vv
 | +--------- make sure is 8 or 9 (optional)
 +----------- make sure is 8 or 9

```

I check the CIN (code index number) here:

```cpp
.
.
.
            evtType = (rx.byte1 >> 4) & 0xf;
            evtChan = rx.byte1 & 0xf;                       
            evtPitch = rx.byte2;
            evtVel = rx.byte3;
            switch( evtType )
            {
                case    0x8:
                case    0x9:
                    if( pitch >= 21 && pitch <= 108 )
                    {
                        pitch -= 21;                                         
                        leds[pitch << 1] = (evtType == 0x8) ? CRGB::Black : CRGB::Red;
                        leds[(pitch << 1)+1] = (evtType == 0x8) ? CRGB::Black : CRGB::Red;                   
                                                       
                    }//if
.
.
.

```

---

<div class="post-metadata">

**Author:** ![markbowenpiano](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/markbowenpiano/32/541742_2.png) [@markbowenpiano](https://forum.arduino.cc/u/markbowenpiano)\
**Post date:** [November 15, 2020, 11:36pm UTC](https://forum.arduino.cc/t/not-sure-why-my-led-wont-light-up/682736/30 "2020-11-15T23:36:02Z")

</div>

Hi Blackfin,

Thank you so much for the continued support on this.

The thing that I don't get is that yes you're right that it's (in the example above) spitting out 226 however it only does that if I attempt to use that startLED variable that I've created which should really only ever at the most be 174 (top note played on the piano is 87 and then times that by 2). If I literally just use the serial monitor to spit out the ledToLight value then it always comes out correct when I press a key and never goes above 87. There are quite literally no other MIDI messages of any sort being sent to the Arduino and just notes being played on the piano.

It's literally only when I try to use that startLED variable (which if I have my code correct to create that variable then it should never be any higher than 174?) inside the code to light up the LEDs that the serial monitor starts to go wonky and spit out loads of weird numbers like that. The fact that I'm also getting minus numbers too makes me think there's something else going on here.

As I say though I can spit out ledToLight with absolutely no problems and I only ever get values from 0-88 spat out so I would have thought that the startLED variable I've made should also be okay as it would never be any higher than 174?

The other weird thing is that when I try to use the startLED variable then the serial monitor goes into that weird loop spitting out all kinds of odd numbers but I'm not pressing any keys or sending any data to the board at all so really stumped as to why it's spitting anything out there anyway!?

I will however try out your code above tomorrow and thank you very much for that.

Thanks,

Mark

---

<div class="post-metadata">

**Author:** ![Blackfin](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blackfin/32/238871_2.png) [@Blackfin](https://forum.arduino.cc/u/Blackfin)\
**Post date:** [November 16, 2020, 12:39am UTC](https://forum.arduino.cc/t/not-sure-why-my-led-wont-light-up/682736/31 "2020-11-16T00:39:49Z")

</div>

> markbowenpiano:  
> The thing that I don't get is that yes you're right that it's (in the example above) spitting out 226 however it only does that if I attempt to use that startLED variable that I've created which should really only ever at the most be 174 (top note played on the piano is 87 and then times that by 2).

Maybe I'm missing something but when your code prints 226 it is printing the is the value from the event message itself (well, minus 21):

```arduino
     int ledToLight = rx.byte2 - 21; // Gives a number from 0-88
      Serial.println(ledToLight);
      Serial.println("---------------------------");

```

This is why I was saying you need to verify the event type and the bounds on rx.byte2. Don't just assume they're as you expect.

> I will however try out your code above tomorrow and thank you very much for that.

I've updated it to check for both noteOff events (0x8) and for noteOn (0x9) with a velocity of 0x0:

```arduino
/*
   MIDIUSB_test.ino

   Created: 4/6/2015 10:47:08 AM
   Author: gurbrinder grewal
   Modified by Arduino LLC (2015)
*/

// a lookup table to convert pitch to text
const char pitchLUT[][4] =
{
  " A0", "B0b", " B0", " C1", "D1b", " D1", "E1b", " E1", " F1", "G1b",
  " G1", "A1b", " A1", "B1b", " B1", " C2", "D2b", " D2", "E2b", " E2",
  " F2", "G2b", " G2", "A2b", " A2", "B2b", " B2", " C3", "D3b", " D3",
  "E3b", " E3", " F3", "G3b", " G3", "A3b", " A3", "B3b", " B3", " C4",
  "D4b", " D4", " E4b", " E4", " F4", "G4b", " G4", "A4b", "A4", "B4b",
  " B4", " C5", "D5b", " D5", "E5b", " E5", " F5", "G5b", " G5", "A5b",
  " A5", "B5b", " B5", " C6", "D6b", " D6", "E6b", " E6", " F6", "G6b",
  " G6", "A6b", " A6", "B6b", " B6", " C7", "D7b", " D7", "E7b", " E7",
  " F7", "G7b", " G7", "A7b", " A7", "B7b", " B7", " C8",
};

#include "MIDIUSB.h"
#include "FastLED.h"

#define DEBUG           1       //comment out to stop verbose serial messages

#define PIN             3       // Output Pin to Data Line on Strip
#define COLOR_ORDER     GRB     // I had to change this for my strip if your color is off then you know.
#define NUM_LEDS        176     // # of LEDS in the strip
#define BRIGHTNESS      10

CRGB leds[NUM_LEDS];
int fadeAmount = 5;  // Set the amount to fade I usually do 5, 10, 15, 20, 25 etc even up to 255.

uint8_t
    evtType,
    evtChan,
    evtPitch,
    evtVel,
    pitch;
char
    szStr[40];

void setup() 
{
    // sanity check delay - allows reprogramming if accidently blowing power w/leds
    delay(2000);
    FastLED.addLeds<WS2812, PIN, GRB>(leds, NUM_LEDS);  // GRB ordering is typical
    FastLED.setBrightness(BRIGHTNESS);
    FastLED.clear();
    Serial.begin(115200);
}

void loop() 
{ 
    midiEventPacket_t rx = MidiUSB.read();
    if( rx.header != 0 )
    {
        do
        {
#ifdef DEBUG            
            sprintf( szStr, "Packet: %02X - %02X - %02X - %02X",
                rx.header,
                rx.byte1,
                rx.byte2,
                rx.byte3 );                
            Serial.println( szStr ); 
#endif
            evtType = (rx.byte1 >> 4) & 0xf;
            evtChan = rx.byte1 & 0xf;                        
            evtPitch = rx.byte2;
            evtVel = rx.byte3;
            switch( evtType )
            {
                case    0x8:
                case    0x9:
                    if( pitch >= 21 && pitch <= 108 )
                    {                        
                        pitch -= 21;
                        if( evtType == 0x8 || (evtType == 0x9 && evtVel == 0) )
                        {
                            leds[pitch << 1] = CRGB::Black;
                            leds[(pitch << 1)+1] = CRGB::Black;                    
                            
                        }
                        else
                        {
                            leds[pitch << 1] = CRGB::Red;
                            leds[(pitch << 1)+1] = CRGB::Red;                    
                            
                        }//else
                                                        
                    }//if
                    else
                    {
                        //illegal note
                        Serial.println( "Unknown note" );
                         
                    }//else
                    
                break;

                default:
                break;
                
            }//switch

            FastLED.show();
#ifdef DEBUG                        
            sprintf( szStr, "Type: %1X Chan: %d Note: %s Velocity: %d",
                evtType,
                evtChan,
                pitchLUT[pitch],
                evtVel );
            Serial.print( szStr );
#endif        
            rx = MidiUSB.read();
            
        }while(rx.header != 0);
        
    }//if
    
}//loop

```

---

<div class="post-metadata">

**Author:** ![markbowenpiano](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/markbowenpiano/32/541742_2.png) [@markbowenpiano](https://forum.arduino.cc/u/markbowenpiano)\
**Post date:** [November 16, 2020, 9:02am UTC](https://forum.arduino.cc/t/not-sure-why-my-led-wont-light-up/682736/32 "2020-11-16T09:02:50Z")

</div>

Hiya,

Thank you for that code. Just tried it out now but sorry to say that every time I press a key on the piano now I get this sort of output in the serial monitor :

```cpp
Packet: 09 - 90 - 3C - 33
Unknown note
Type: 9 Chan: 0 Note:  A0 Velocity: 51
Packet: 09 - 90 - 3C - 00
Unknown note
Type: 9 Chan: 0 Note:  A0 Velocity: 0

```

Not too sure what much of it means however I can see that the velocity is coming through fine however no matter which key I press on the piano it is being reported back as being A0 so not too sure what's happening there. Also none of the LEDs light up with any presses on any of the keys unfortunately.

I will endeavour to try and understand some of it if I can and have a meddle to see if I can figure it out. Warning to all users on the internet that if the internet goes down then it's probably something I've done!! 😃

---

<div class="post-metadata">

**Author:** ![markbowenpiano](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/markbowenpiano/32/541742_2.png) [@markbowenpiano](https://forum.arduino.cc/u/markbowenpiano)\
**Post date:** [November 16, 2020, 9:11am UTC](https://forum.arduino.cc/t/not-sure-why-my-led-wont-light-up/682736/33 "2020-11-16T09:11:54Z")

</div>

Sheesh!!

I think I got it. Of course only because of your excellent code though!

I noticed that you had a variable called pitch but it wasn't being given any value. I changed everywhere it said pitch to evtPitch (apart from the initial creation of the variable - guessing that one can be deleted) and everything is working as far as I can tell!!

The notes show up correctly in the serial monitor and when I press the first piano key the first two LEDs light up and then the next key lights up the next two!

Absolutely fantastic. Thank you so so much for the code. I will try and add in fading now and then I'd like to add potentiometers to the circuit so that I can change the colour of the LEDs from outside the programming world. Small small steps though as this is seriously very new to me.

I want to fully understand all your excellent code first though.

Thanks so much again.

Best wishes,

Mark

---

<div class="post-metadata">

**Author:** ![markbowenpiano](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/markbowenpiano/32/541742_2.png) [@markbowenpiano](https://forum.arduino.cc/u/markbowenpiano)\
**Post date:** [November 16, 2020, 9:17am UTC](https://forum.arduino.cc/t/not-sure-why-my-led-wont-light-up/682736/34 "2020-11-16T09:17:19Z")

</div>

Gahhh!! Just noticed that when I press the pedals on the piano they are being recognised as notes! Really have no idea why that would be though as they are definitely not ever recognised as such in a DAW and should be recognised as CC messages so no idea why that would be? ☹

Actually just checked using a little app called Midi Monitor and they show up in there as CC messages as they should do but for some reason they're being spat out as notes by the Arduino so really have no idea why.

Thanks so much for the code though. It's so much further along than I was getting!

---

<div class="post-metadata">

**Author:** ![Blackfin](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blackfin/32/238871_2.png) [@Blackfin](https://forum.arduino.cc/u/Blackfin)\
**Post date:** [November 16, 2020, 12:53pm UTC](https://forum.arduino.cc/t/not-sure-why-my-led-wont-light-up/682736/35 "2020-11-16T12:53:38Z")

</div>

> markbowenpiano:  
> Hiya,
> 
> Thank you for that code. Just tried it out now but sorry to say that every time I press a key on the piano now I get this sort of output in the serial monitor :
> 
> ```arduino
> Packet: 09 - 90 - 3C - 33
> 
> ```

Unknown note  
Type: 9 Chan: 0 Note:&nbsp; A0 Velocity: 51  
Packet: 09 - 90 - 3C - 00  
Unknown note  
Type: 9 Chan: 0 Note:&nbsp; A0 Velocity: 0

> ```arduino
> 
> ```

_EDIT: I see you already found the bug but I'll leave this here for completeness..._

Oops, super-sorry: That's what I get for changing variable names but not all of them. The variable pitch should be changed to evtPitch:

```arduino
/*
  MIDIUSB_test.ino

  Created: 4/6/2015 10:47:08 AM
  Author: gurbrinder grewal
  Modified by Arduino LLC (2015)
*/

// a lookup table to convert pitch to text
const char pitchLUT[][4] =
{
  " A0", "B0b", " B0", " C1", "D1b", " D1", "E1b", " E1", " F1", "G1b",
  " G1", "A1b", " A1", "B1b", " B1", " C2", "D2b", " D2", "E2b", " E2",
  " F2", "G2b", " G2", "A2b", " A2", "B2b", " B2", " C3", "D3b", " D3",
  "E3b", " E3", " F3", "G3b", " G3", "A3b", " A3", "B3b", " B3", " C4",
  "D4b", " D4", " E4b", " E4", " F4", "G4b", " G4", "A4b", "A4", "B4b",
  " B4", " C5", "D5b", " D5", "E5b", " E5", " F5", "G5b", " G5", "A5b",
  " A5", "B5b", " B5", " C6", "D6b", " D6", "E6b", " E6", " F6", "G6b",
  " G6", "A6b", " A6", "B6b", " B6", " C7", "D7b", " D7", "E7b", " E7",
  " F7", "G7b", " G7", "A7b", " A7", "B7b", " B7", " C8",
};

#include "MIDIUSB.h"
#include "FastLED.h"

#define DEBUG          1      //comment out to stop verbose serial messages

#define PIN            3      // Output Pin to Data Line on Strip
#define COLOR_ORDER    GRB    // I had to change this for my strip if your color is off then you know.
#define NUM_LEDS        176    // # of LEDS in the strip
#define BRIGHTNESS      10

CRGB leds[NUM_LEDS];
int fadeAmount = 5;  // Set the amount to fade I usually do 5, 10, 15, 20, 25 etc even up to 255.

uint8_t
    evtType,
    evtChan,
    evtPitch,
    evtVel;
char
    szStr[40];

void setup() 
{
    // sanity check delay - allows reprogramming if accidently blowing power w/leds
    delay(2000);
    FastLED.addLeds<WS2812, PIN, GRB>(leds, NUM_LEDS);  // GRB ordering is typical
    FastLED.setBrightness(BRIGHTNESS);
    FastLED.clear();
    Serial.begin(115200);
}

void loop() 
{ 
    midiEventPacket_t rx = MidiUSB.read();
    if( rx.header != 0 )
    {
        do
        {
#ifdef DEBUG            
            sprintf( szStr, "Packet: %02X - %02X - %02X - %02X",
                rx.header,
                rx.byte1,
                rx.byte2,
                rx.byte3 );                
            Serial.println( szStr ); 
#endif
            evtType = (rx.byte1 >> 4) & 0xf;
            evtChan = rx.byte1 & 0xf;                        
            evtPitch = rx.byte2;
            evtVel = rx.byte3;
            switch( evtType )
            {
                case    0x8:
                case    0x9:
                    if( evtPitch >= 21 && evtPitch <= 108 )
                    {                        
                        evtPitch -= 21;
                        if( evtType == 0x8 || (evtType == 0x9 && evtVel == 0) )
                        {
                            leds[evtPitch << 1] = CRGB::Black;
                            leds[(evtPitch << 1)+1] = CRGB::Black;                    
                            
                        }
                        else
                        {
                            leds[evtPitch << 1] = CRGB::Red;
                            leds[(evtPitch << 1)+1] = CRGB::Red;                    
                            
                        }//else
                                                        
                    }//if
                    else
                    {
                        //illegal note
                        Serial.println( "Unknown note" );
                         
                    }//else
                    
                break;

                default:
                break;
                
            }//switch

            FastLED.show();
#ifdef DEBUG                        
            sprintf( szStr, "Type: %1X Chan: %d Note: %s Velocity: %d",
                evtType,
                evtChan,
                pitchLUT[evtPitch],
                evtVel );
            Serial.print( szStr );
#endif        
            rx = MidiUSB.read();
            
        }while(rx.header != 0);
        
    }//if
    
}//loop

```

---

<div class="post-metadata">

**Author:** ![Blackfin](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blackfin/32/238871_2.png) [@Blackfin](https://forum.arduino.cc/u/Blackfin)\
**Post date:** [November 16, 2020, 12:58pm UTC](https://forum.arduino.cc/t/not-sure-why-my-led-wont-light-up/682736/36 "2020-11-16T12:58:29Z")

</div>

> markbowenpiano:  
> Gahhh!! Just noticed that when I press the pedals on the piano they are being recognised as notes! Really have no idea why that would be though as they are definitely not ever recognised as such in a DAW and should be recognised as CC messages so no idea why that would be? ☹
> 
> Actually just checked using a little app called Midi Monitor and they show up in there as CC messages as they should do but for some reason they're being spat out as notes by the Arduino so really have no idea why.
> 
> Thanks so much for the code though. It's so much further along than I was getting!

Sorry, I don't know what DAW and CC mean. Is there a reference in:

> **[midi10.pdf](https://www.usb.org/sites/default/files/midi10.pdf)**
>
> 175.85 KB

that you could point me to?

I'm sure if the parsing of the messages was a little tighter you might be able to filter out these unwanted messages. Just need to know what to look for.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [November 16, 2020, 1:43pm UTC](https://forum.arduino.cc/t/not-sure-why-my-led-wont-light-up/682736/37 "2020-11-16T13:43:03Z")

</div>

DAW = Digital Audio Workstation.

---

<div class="post-metadata">

**Author:** ![markbowenpiano](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/markbowenpiano/32/541742_2.png) [@markbowenpiano](https://forum.arduino.cc/u/markbowenpiano)\
**Post date:** [November 16, 2020, 4:01pm UTC](https://forum.arduino.cc/t/not-sure-why-my-led-wont-light-up/682736/38 "2020-11-16T16:01:55Z")

</div>

Hiya,

Sorry on my phone at the moment and for some reason it won't allow me to press the quote button on your post above. Sorry for the abbreviations there. Yes as above DAW means the application in using to record into (in this case Logic Pro X) and CC is controller data which is first mentioned at the very top of page 17 of the PDF you linked to.

I'm really not at all sure why it's being picked up by the board and shown as note information though as controller messages and note information are two completely different things in MIDI however most of the code (at the moment) goes a little over my pay grade.

Oh and don't be sorry at all about the variable naming. You are being fantastic in helping me like this and what you have done so far is absolutely brilliant. I'm just a little glad I was actually able to get one thing working myself (well figure out what was wrong anyway) as still have literally no idea why my variables in my earlier code had one of them working but not the other. All very weird.

---

<div class="post-metadata">

**Author:** ![Blackfin](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blackfin/32/238871_2.png) [@Blackfin](https://forum.arduino.cc/u/Blackfin)\
**Post date:** [November 16, 2020, 5:42pm UTC](https://forum.arduino.cc/t/not-sure-why-my-led-wont-light-up/682736/39 "2020-11-16T17:42:31Z")

</div>

> markbowenpiano:  
> Gahhh!! Just noticed that when I press the pedals on the piano they are being recognised as notes! Really have no idea why that would be though as they are definitely not ever recognised as such in a DAW and should be recognised as CC messages so no idea why that would be? ☹

I think it's because we're looking at the high-nibble of byte1 for the event but aren't checking the header. It's entirely possible byte1 of some other message type contains 0x8 or 0x9 in its high nibble and we'd mistakenly use that.

So we need to check the header instead. Try this:

Add a variable evtCIN as shown:

```arduino
uint8_t
    evtCIN,  //<--- add this
    evtType,
    evtChan,
.
.
.

```

Then make the changes noted:

```arduino
            evtCIN = rx.header & 0xf;          //<--- add this
            evtType = (rx.byte1 >> 4) & 0xf;
            evtChan = rx.byte1 & 0xf;                       
            evtPitch = rx.byte2;
            evtVel = rx.byte3;
            switch( evtCIN )                    //<--- change this
            {
                case    0x8:
.
.
.

```

Also, check your PMs 🙂

---

<div class="post-metadata">

**Author:** ![markbowenpiano](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/markbowenpiano/32/541742_2.png) [@markbowenpiano](https://forum.arduino.cc/u/markbowenpiano)\
**Post date:** [November 16, 2020, 8:11pm UTC](https://forum.arduino.cc/t/not-sure-why-my-led-wont-light-up/682736/40 "2020-11-16T20:11:54Z")

</div>

Hi again,

Thank you so much for the code though unfortunately it's still seeing the pedals as notes ☹

The piano I use has three of them and the soft pedal spits out this to the serial monitor :

```cpp
Packet: 0B - B0 - 43 - 7F
Type: B Chan: 0 Note:  E6 Velocity: 127
Packet: 0B - B0 - 43 - 00
Type: B Chan: 0 Note:  E6 Velocity: 0

```

the next pedal, the sostenuto (which I never actually use) sends this out :

```cpp
Packet: 0B - B0 - 42 - 7F
Type: B Chan: 0 Note: E6b Velocity: 127
Packet: 0B - B0 - 42 - 00
Type: B Chan: 0 Note: E6b Velocity: 0

```

and the sustain pedal sends this out :

```cpp
Packet: 0B - B0 - 40 - 01
Type: B Chan: 0 Note: D6b Velocity: 1
Packet: 0B - B0 - 40 - 05
Type: B Chan: 0 Note: D6b Velocity: 5
Packet: 0B - B0 - 40 - 07
Type: B Chan: 0 Note: D6b Velocity: 7
Packet: 0B - B0 - 40 - 09
Type: B Chan: 0 Note: D6b Velocity: 9
Packet: 0B - B0 - 40 - 0B
Type: B Chan: 0 Note: D6b Velocity: 11
Packet: 0B - B0 - 40 - 0D
Type: B Chan: 0 Note: D6b Velocity: 13
Packet: 0B - B0 - 40 - 0F
Type: B Chan: 0 Note: D6b Velocity: 15
Packet: 0B - B0 - 40 - 13
Type: B Chan: 0 Note: D6b Velocity: 19
Packet: 0B - B0 - 40 - 15
Type: B Chan: 0 Note: D6b Velocity: 21
Packet: 0B - B0 - 40 - 17
Type: B Chan: 0 Note: D6b Velocity: 23
Packet: 0B - B0 - 40 - 1B
Type: B Chan: 0 Note: D6b Velocity: 27
Packet: 0B - B0 - 40 - 1D
Type: B Chan: 0 Note: D6b Velocity: 29
Packet: 0B - B0 - 40 - 1F
Type: B Chan: 0 Note: D6b Velocity: 31
Packet: 0B - B0 - 40 - 21
Type: B Chan: 0 Note: D6b Velocity: 33
Packet: 0B - B0 - 40 - 25
Type: B Chan: 0 Note: D6b Velocity: 37
Packet: 0B - B0 - 40 - 27
Type: B Chan: 0 Note: D6b Velocity: 39
Packet: 0B - B0 - 40 - 29
Type: B Chan: 0 Note: D6b Velocity: 41
Packet: 0B - B0 - 40 - 2B
Type: B Chan: 0 Note: D6b Velocity: 43
Packet: 0B - B0 - 40 - 2D
Type: B Chan: 0 Note: D6b Velocity: 45
Packet: 0B - B0 - 40 - 2F
Type: B Chan: 0 Note: D6b Velocity: 47
Packet: 0B - B0 - 40 - 31
Type: B Chan: 0 Note: D6b Velocity: 49
Packet: 0B - B0 - 40 - 33
Type: B Chan: 0 Note: D6b Velocity: 51
Packet: 0B - B0 - 40 - 35
Type: B Chan: 0 Note: D6b Velocity: 53
Packet: 0B - B0 - 40 - 39
Type: B Chan: 0 Note: D6b Velocity: 57
Packet: 0B - B0 - 40 - 3B
Type: B Chan: 0 Note: D6b Velocity: 59
Packet: 0B - B0 - 40 - 3D
Type: B Chan: 0 Note: D6b Velocity: 61
Packet: 0B - B0 - 40 - 3F
Type: B Chan: 0 Note: D6b Velocity: 63
Packet: 0B - B0 - 40 - 41
Type: B Chan: 0 Note: D6b Velocity: 65
Packet: 0B - B0 - 40 - 43
Type: B Chan: 0 Note: D6b Velocity: 67
Packet: 0B - B0 - 40 - 45
Type: B Chan: 0 Note: D6b Velocity: 69
Packet: 0B - B0 - 40 - 49
Type: B Chan: 0 Note: D6b Velocity: 73
Packet: 0B - B0 - 40 - 4B
Type: B Chan: 0 Note: D6b Velocity: 75
Packet: 0B - B0 - 40 - 4D
Type: B Chan: 0 Note: D6b Velocity: 77
Packet: 0B - B0 - 40 - 4F
Type: B Chan: 0 Note: D6b Velocity: 79
Packet: 0B - B0 - 40 - 51
Type: B Chan: 0 Note: D6b Velocity: 81
Packet: 0B - B0 - 40 - 53
Type: B Chan: 0 Note: D6b Velocity: 83
Packet: 0B - B0 - 40 - 55
Type: B Chan: 0 Note: D6b Velocity: 85
Packet: 0B - B0 - 40 - 59
Type: B Chan: 0 Note: D6b Velocity: 89
Packet: 0B - B0 - 40 - 5B
Type: B Chan: 0 Note: D6b Velocity: 91
Packet: 0B - B0 - 40 - 5F
Type: B Chan: 0 Note: D6b Velocity: 95
Packet: 0B - B0 - 40 - 61
Type: B Chan: 0 Note: D6b Velocity: 97
Packet: 0B - B0 - 40 - 65
Type: B Chan: 0 Note: D6b Velocity: 101
Packet: 0B - B0 - 40 - 67
Type: B Chan: 0 Note: D6b Velocity: 103
Packet: 0B - B0 - 40 - 69
Type: B Chan: 0 Note: D6b Velocity: 105
Packet: 0B - B0 - 40 - 6B
Type: B Chan: 0 Note: D6b Velocity: 107
Packet: 0B - B0 - 40 - 6D
Type: B Chan: 0 Note: D6b Velocity: 109
Packet: 0B - B0 - 40 - 6F
Type: B Chan: 0 Note: D6b Velocity: 111
Packet: 0B - B0 - 40 - 6B
Type: B Chan: 0 Note: D6b Velocity: 107
Packet: 0B - B0 - 40 - 67
Type: B Chan: 0 Note: D6b Velocity: 103
Packet: 0B - B0 - 40 - 65
Type: B Chan: 0 Note: D6b Velocity: 101
Packet: 0B - B0 - 40 - 61
Type: B Chan: 0 Note: D6b Velocity: 97
Packet: 0B - B0 - 40 - 5F
Type: B Chan: 0 Note: D6b Velocity: 95
Packet: 0B - B0 - 40 - 5B
Type: B Chan: 0 Note: D6b Velocity: 91
Packet: 0B - B0 - 40 - 59
Type: B Chan: 0 Note: D6b Velocity: 89
Packet: 0B - B0 - 40 - 57
Type: B Chan: 0 Note: D6b Velocity: 87
Packet: 0B - B0 - 40 - 53
Type: B Chan: 0 Note: D6b Velocity: 83
Packet: 0B - B0 - 40 - 51
Type: B Chan: 0 Note: D6b Velocity: 81
Packet: 0B - B0 - 40 - 4D
Type: B Chan: 0 Note: D6b Velocity: 77
Packet: 0B - B0 - 40 - 49
Type: B Chan: 0 Note: D6b Velocity: 73
Packet: 0B - B0 - 40 - 45
Type: B Chan: 0 Note: D6b Velocity: 69
Packet: 0B - B0 - 40 - 3F
Type: B Chan: 0 Note: D6b Velocity: 63
Packet: 0B - B0 - 40 - 39
Type: B Chan: 0 Note: D6b Velocity: 57
Packet: 0B - B0 - 40 - 35
Type: B Chan: 0 Note: D6b Velocity: 53
Packet: 0B - B0 - 40 - 2D
Type: B Chan: 0 Note: D6b Velocity: 45
Packet: 0B - B0 - 40 - 27
Type: B Chan: 0 Note: D6b Velocity: 39
Packet: 0B - B0 - 40 - 1F
Type: B Chan: 0 Note: D6b Velocity: 31
Packet: 0B - B0 - 40 - 19
Type: B Chan: 0 Note: D6b Velocity: 25
Packet: 0B - B0 - 40 - 13
Type: B Chan: 0 Note: D6b Velocity: 19
Packet: 0B - B0 - 40 - 0B
Type: B Chan: 0 Note: D6b Velocity: 11
Packet: 0B - B0 - 40 - 03
Type: B Chan: 0 Note: D6b Velocity: 3
Packet: 0B - B0 - 40 - 00
Type: B Chan: 0 Note: D6b Velocity: 0

```

That code could realistically go on forever as it seems to send out a whole load of information instead of just an on and off signal for some reason.

Will check PMs now and reply to you there. Many thanks.

Mark

[Previous page](https://forum.arduino.cc/t/not-sure-why-my-led-wont-light-up/682736.md?page=1)

[Next page](https://forum.arduino.cc/t/not-sure-why-my-led-wont-light-up/682736.md?page=3)
