Size limit to switch cases?

Hey All! First time arduino user - i’m building an LED light that can be controlled by midi (using an arduino due and addressable LEDs). I’m using switch cases to get midi notes to trigger certain settings - and everything works perfectly - except at a certain point of text in the cases or a certain amount of cases, and everything gets buggy. Is there a limit to the amount of characters in cases? Thanks in advance!

#include <MIDIUSB.h>
#include <FastLED.h>

#define NUM_LEDS_PER_STRIP 64
#define NUM_STRIPS 1


int cccolor20 = 0;
int cccolor21 = 0;
int cccolor22 = 0;
int cccolor23 = 0;
int ccsat24 = 255;
int ccsat25 = 255;
int rando1 = 0;

CRGB leds[NUM_STRIPS * NUM_LEDS_PER_STRIP];


void noteOn(byte channel, byte note, byte velocity) {
     switch (note){
       case 0: 
          for(int led = 0; led < 8; led++) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }  
          break;
      case 1:
          for(int led = 8; led < 16; led++) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;
       case 2: 
          for(int led = 16; led < 24; led++) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }  
          break;
      case 3:
          for(int led = 24; led < 32; led++) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;
       case 4: 
          for(int led = 32; led < 40; led++) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }  
          break;
      case 5:
          for(int led = 40; led < 48; led++) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;
       case 6: 
          for(int led = 48; led < 56; led++) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }  
          break;
      case 7:
          for(int led = 56; led < 64; led++) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;
      case 8: 
          rando1 = random(0,8);         
          for(int led = rando1*8; led < rando1*8+8; led++) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;
      case 9: 
          for(int led = 0; led < 64; led = led + 8) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;       
      case 10: 
          for(int led = 1; led < 64; led = led + 8) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;    
      case 11: 
          for(int led = 2; led < 64; led = led + 8) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;    
      case 12: 
          for(int led = 3; led < 64; led = led + 8) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;    
      case 13: 
          for(int led = 4; led < 64; led = led + 8) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;    
      case 14: 
          for(int led = 5; led < 64; led = led + 8) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;       
      case 15: 
          for(int led = 6; led < 64; led = led + 8) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;    
      case 16: 
          for(int led = 7; led < 64; led = led + 8) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;    
           
          default:
          break;
  }
          FastLED.show();
}

void noteOff(byte channel, byte note, byte velocity) {
     switch (note){
       case 0: 
          for(int led = 0; led < 8; led++) { 
          leds[led] = CHSV( 0, 0, 0); 
          }  
          break;
      case 1:
          for(int led = 8; led < 16; led++) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;
       case 2: 
          for(int led = 16; led < 24; led++) { 
          leds[led] = CHSV( 0, 0, 0); 
          }  
          break;
      case 3:
          for(int led = 24; led < 32; led++) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;
       case 4: 
          for(int led = 32; led < 40; led++) { 
          leds[led] = CHSV( 0, 0, 0); 
          }  
          break;
      case 5:
          for(int led = 40; led < 48; led++) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;
       case 6: 
          for(int led = 48; led < 56; led++) { 
          leds[led] = CHSV( 0, 0, 0); 
          }  
          break;
      case 7:
          for(int led = 56; led < 64; led++) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;
      case 8: 
          for(int led = 0; led < 64; led++) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;
      case 9: 
          for(int led = 0; led < 64; led = led + 8) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;  
      case 10: 
          for(int led = 1; led < 64; led = led + 8) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;    
      case 11: 
          for(int led = 2; led < 64; led = led + 8) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;    
      case 12: 
          for(int led = 3; led < 64; led = led + 8) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;    
      case 13: 
          for(int led = 4; led < 64; led = led + 8) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;    
      case 14: 
          for(int led = 5; led < 64; led = led + 8) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;       
      case 15: 
          for(int led = 6; led < 64; led = led + 8) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;    
      case 16: 
          for(int led = 7; led < 64; led = led + 8) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;    

        default:
          break;
  }
          FastLED.show();
}
void controlChange(byte channel, byte control, byte value) {
    switch (control){
       case 20:
         cccolor20 = value*2;
        break;   
       case 21:
         cccolor21 = value*2;
        break;   
        case 22:
         cccolor22 = value*2;
        break;   
       case 23:
         cccolor23 = value*2;
        break;   
        case 24:
         ccsat24 = value*2;
        break;   
       case 25:
         ccsat25 = value*2;
        break;            
       }    
}

void setup() {
  Serial.begin(56000);
  LEDS.addLeds<WS2811_PORTD,NUM_STRIPS>(leds, NUM_LEDS_PER_STRIP).setCorrection(TypicalLEDStrip);
}

void loop() {
  midiEventPacket_t rx = MidiUSB.read();
  switch (rx.header) {
    case 0:
      break; //No pending events
      
    case 0x9:
      noteOn(
        rx.byte1 & 0xF,  //channel
        rx.byte2,        //pitch
        rx.byte3         //velocity
      );
      break;
      
    case 0x8:
      noteOff(
        rx.byte1 & 0xF,  //channel
        rx.byte2,        //pitch
        rx.byte3         //velocity
      );
      break;
            
    case 0xB:
      controlChange(
        rx.byte1 & 0xF,  //channel
        rx.byte2,        //control
        rx.byte3         //value
      );
      break;
      
    default:
      break;
  }
}

Is there a limit to the amount of characters in cases?

As long as you have enough memory you can put as much code in a case code block as you like if that is what you are asking. If you declare any variables in a case code block then note any warning messages produced by the compiler and eliminate them by enclosing the case code block in {} pairs.

Have you tried adding print statements, perhaps before each switch/case, to discover exactly what circumstances cause the “bugs” and how are you identifying the “bugs” ?

Greetings! First time arduino user - asked this question in a different forum but realized maybe its specific to LEDs… But anyway, i’m building an LED light that can be controlled by midi (using an arduino due with the midiUSB library and addressable LEDs). I’m using switch cases to get midi notes to trigger certain LEDs to turn on - and everything works perfectly - until I get to a certain amount of cases in the noteon/noteoff switch or have too much text. At this point, it will send buggy info to the LEDs and they flash extra colors or dont turn off. It doesnt seem to stop consistently at a certain number of cases - in the example here there are 16 short cases which bugs it out, but ive also done less but with more complex cases and it bugs out then. Is there a limit to the amount of characters in cases when dealing with LEDs? Maybe theres something wrong in my code but ive tried it many times and keep getting problems… Thanks in advance!

#include <MIDIUSB.h>
#include <FastLED.h>

#define NUM_LEDS_PER_STRIP 64
#define NUM_STRIPS 1


int cccolor20 = 0;
int cccolor21 = 0;
int cccolor22 = 0;
int cccolor23 = 0;
int ccsat24 = 255;
int ccsat25 = 255;
int rando1 = 0;

CRGB leds[NUM_STRIPS * NUM_LEDS_PER_STRIP];


void noteOn(byte channel, byte note, byte velocity) {
     switch (note){
       case 0: 
          for(int led = 0; led < 8; led++) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }  
          break;
      case 1:
          for(int led = 8; led < 16; led++) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;
       case 2: 
          for(int led = 16; led < 24; led++) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }  
          break;
      case 3:
          for(int led = 24; led < 32; led++) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;
       case 4: 
          for(int led = 32; led < 40; led++) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }  
          break;
      case 5:
          for(int led = 40; led < 48; led++) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;
       case 6: 
          for(int led = 48; led < 56; led++) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }  
          break;
      case 7:
          for(int led = 56; led < 64; led++) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;
      case 8: 
          rando1 = random(0,8);         
          for(int led = rando1*8; led < rando1*8+8; led++) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;
      case 9: 
          for(int led = 0; led < 64; led = led + 8) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;       
      case 10: 
          for(int led = 1; led < 64; led = led + 8) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;    
      case 11: 
          for(int led = 2; led < 64; led = led + 8) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;    
      case 12: 
          for(int led = 3; led < 64; led = led + 8) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;    
      case 13: 
          for(int led = 4; led < 64; led = led + 8) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;    
      case 14: 
          for(int led = 5; led < 64; led = led + 8) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;       
      case 15: 
          for(int led = 6; led < 64; led = led + 8) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;    
      case 16: 
          for(int led = 7; led < 64; led = led + 8) { 
          leds[led] = CHSV( cccolor20, 255, velocity*2); 
          }   
          break;    
       
  }
          FastLED.show();
}

void noteOff(byte channel, byte note, byte velocity) {
     switch (note){
       case 0: 
          for(int led = 0; led < 8; led++) { 
          leds[led] = CHSV( 0, 0, 0); 
          }  
          break;
      case 1:
          for(int led = 8; led < 16; led++) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;
       case 2: 
          for(int led = 16; led < 24; led++) { 
          leds[led] = CHSV( 0, 0, 0); 
          }  
          break;
      case 3:
          for(int led = 24; led < 32; led++) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;
       case 4: 
          for(int led = 32; led < 40; led++) { 
          leds[led] = CHSV( 0, 0, 0); 
          }  
          break;
      case 5:
          for(int led = 40; led < 48; led++) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;
       case 6: 
          for(int led = 48; led < 56; led++) { 
          leds[led] = CHSV( 0, 0, 0); 
          }  
          break;
      case 7:
          for(int led = 56; led < 64; led++) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;
      case 8: 
          for(int led = 0; led < 64; led++) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;
      case 9: 
          for(int led = 0; led < 64; led = led + 8) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;  
      case 10: 
          for(int led = 1; led < 64; led = led + 8) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;    
      case 11: 
          for(int led = 2; led < 64; led = led + 8) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;    
      case 12: 
          for(int led = 3; led < 64; led = led + 8) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;    
      case 13: 
          for(int led = 4; led < 64; led = led + 8) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;    
      case 14: 
          for(int led = 5; led < 64; led = led + 8) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;       
      case 15: 
          for(int led = 6; led < 64; led = led + 8) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;    
      case 16: 
          for(int led = 7; led < 64; led = led + 8) { 
          leds[led] = CHSV( 0, 0, 0); 
          }   
          break;    
  }
          FastLED.show();
}
void controlChange(byte channel, byte control, byte value) {
    switch (control){
       case 20:
         cccolor20 = value*2;
        break;   
       case 21:
         cccolor21 = value*2;
        break;   
        case 22:
         cccolor22 = value*2;
        break;   
       case 23:
         cccolor23 = value*2;
        break;   
        case 24:
         ccsat24 = value*2;
        break;   
       case 25:
         ccsat25 = value*2;
        break;            
       }    
}

void setup() {
  Serial.begin(115200);
  LEDS.addLeds<WS2811_PORTD,NUM_STRIPS>(leds, NUM_LEDS_PER_STRIP).setCorrection(TypicalLEDStrip);
}

void loop() {
  midiEventPacket_t rx = MidiUSB.read();
  switch (rx.header) {
    case 0:
      break; //No pending events
      
    case 0x9:
      noteOn(
        rx.byte1 & 0xF,  //channel
        rx.byte2,        //pitch
        rx.byte3         //velocity
      );
      break;
      
    case 0x8:
      noteOff(
        rx.byte1 & 0xF,  //channel
        rx.byte2,        //pitch
        rx.byte3         //velocity
      );
      break;
            
    case 0xB:
      controlChange(
        rx.byte1 & 0xF,  //channel
        rx.byte2,        //control
        rx.byte3         //value
      );
      break;
      
    default:
      break;
  }
}

Duplicates merged. @doronsadja in the future, if you decide you want your topic moved to a different forum board, just click the "Report to moderator" link at the bottom of the post and request it to be moved.

In this case, I think the original "Programming Questions" board is the most appropriate place for it.

Just on programming style...
You may be a lot better off ny using the switch
() to set the range & colour value, then a single function to write the required LEDs.

Your code will become a lot shorter, more readable, easier to maintain, and expand.

Thanks so much for your help - I'll try printing to see if i can find the error.

I'm curious exactly what you mean @lastchancename - a cleaner programming style would be great here since its already getting hard to sift through and I'm just getting started~