Very weird problems with Switch Case structure

Garug

Well-known member
Hi,

It is not first time I am using Case structure, but having so weird problems that maybe I am just blind to my own mistakes. Never mind the other code the case structure is what is failing.

The problem is that the order on what the cases are affects if they get executed. If I put the case 0xB0: after case 0xF0: it does not get executed. The default does not get executed unless it is the first case.

Please help, can you spot what is the problem.

Code:
void process_usbMIDI(void) {
  uint8_t type, channel, data1, data2, cable;

  type    = usbMIDI.getType();      
  channel = usbMIDI.getChannel();   
  data1   = usbMIDI.getData1();     
  data2   = usbMIDI.getData2();    
  cable   = usbMIDI.getCable();    

    switch (type) {
      
      case 0xB0:
              USBhostMIDI.send( type, data1, data2, channel, cable );
              Serial1.print("1<"); printMIDI(type, channel, data1, data2, cable, usbMIDI.getSysExArray());
        break;
        
      case 0xF0:
              uint16_t SysExLength = data1 + data2 * 256;
              USBhostMIDI.sendSysEx(SysExLength, usbMIDI.getSysExArray(), true);
              Serial1.print("2<"); decodeLCXL_SysEx(usbMIDI.getSysExArray(), SysExLength );
        break;
        
      default:
              Serial1.print("UNKNOWN TYPE");
        break;
        }
}
 
I don't see any obvious reason why your stuff would fail that way,

But guessing that you are getting some compiler warnings. At least I usually do, if I try to define variables directly within the switch case.
Code:
      case 0xF0:
              uint16_t SysExLength = data1 + data2 * 256;
              USBhostMIDI.sendSysEx(SysExLength, usbMIDI.getSysExArray(), true);
              Serial1.print("2<"); decodeLCXL_SysEx(usbMIDI.getSysExArray(), SysExLength );
        break;
Which I usually can remove by putting within {} like:
Code:
      case 0xF0:
        {
              uint16_t SysExLength = data1 + data2 * 256;
              USBhostMIDI.sendSysEx(SysExLength, usbMIDI.getSysExArray(), true);
              Serial1.print("2<"); decodeLCXL_SysEx(usbMIDI.getSysExArray(), SysExLength );
        }
        break;

You might try it and see if solves it
 
So I made this simplified example of above and it works exactly as expected. On this one I have changed the case 0xB0: to case 0xA0: so that the default get executed and it does. But that is not the case with the above code?

I am seriously considering dumbing this Switch and replacing with if, just to proceed.

Code:
void setup() {
  // put your setup code here, to run once:

}

void loop() {
delay(1000);
process_usbMIDI();

}

void process_usbMIDI() { 


  uint8_t type; 
   
   
  type    = 0xB0;         
    
       
  switch (type) {   
               
  case 0xA0:               
              Serial.println("TYPE 0xB0");         
    break; 
                   
  case 0xF0:               
              Serial.println("TYPE 0xF0");         
    break;  
                  
  default:               
              Serial.println("UNKNOWN TYPE");         
    break;        
    } 
  
}
 
Back
Top