switch case switch case construct comp w errors

I want to use two switches to execute 32 different commands.
Therefore there is a switch inside a switch. But this construction compiles with errors. here’s the sketch:

// ---------------------------------------------------------------------
// all used globals
int  baseDecoderAddress = 57;      // Default decoder address
int  testDecoderAddress ;      // Change this to 57..60
                                   // this equals turnouts 225..240
int  State = 0;                    // per address from 0..7


void setup()  {
 Serial.begin(115200); 
 testDecoderAddress = 57;
}


void executeCommand()  {
   switch(testDecoderAddress)  {
     case baseDecoderAddress:  
     switch(State)  {
       case 0:  Serial.println("225R off"); break;
       case 1:  Serial.println("225G on"); break;
       case 2:  Serial.println("226R step to next station right"); break;
       case 3:  Serial.println("226G step to next station left"); break;
       case 4:  Serial.println("227R do 180 degrees turn right"); break;
       case 5:  Serial.println("227G do 180 degrees turn left"); break;
       case 6:  Serial.println("228R turn right"); break;
       case 7:  Serial.println("228G turn left"); break;
       default: break;
     }
     default: break;
   }
   default: break;
}


void loop()  {
 executeCommand(); 
}

Do i mis something ?

Do i mis something ?

No, quite the opposite - too many "default:".

Consistent indentation would help.

Could you please offer a better help. I removed all the bottom part break's at no help.

The very last 'default: break;' is not needed, so remove it.

If this doesn't work you need to post your code again.

Thanks bur now i got a other error

void executeCommand()  
  {
   switch(testDecoderAddress)  
     {
     case baseDecoderAddress:  
     switch(State)  
       {
       case 0:  Serial.println("225R off"); break;
       case 1:  Serial.println("225G on"); break;
       case 2:  Serial.println("226R step to next station right"); break;
       case 3:  Serial.println("226G step to next station left"); break;
       case 4:  Serial.println("227R do 180 degrees turn right"); break;
       case 5:  Serial.println("227G do 180 degrees turn left"); break;
       case 6:  Serial.println("228R turn right"); break;
       case 7:  Serial.println("228G turn left"); break;
       default:  break;
       }
     default:  break;
     }
  }

And the error: sketch_dec01b.cpp: In function 'void executeCommand()': sketch_dec01b:18: error: 'baseDecoderAddress' cannot appear in a constant-expression

You can't have a variable case. Why would you want one?

The complete routine looks like this:

void executeCommand()
{
  switch(testDecoderAddress)
    {
    case baseDecoderAddress:
     switch(State)  
       {
       case 0:  Serial.println("225R off"); break;
       case 1:  Serial.println("225G on"); break;
       case 2:  Serial.println("226R step to next station right"); break;
       case 3:  Serial.println("226G step to next station left"); break;
       case 4:  Serial.println("227R do 180 degrees turn right"); break;
       case 5:  Serial.println("227G do 180 degrees turn left"); break;
       case 6:  Serial.println("228R turn right"); break;
       case 7:  Serial.println("228G turn left"); break;
       }
      break;
    case baseDecoderAddress+1:
     switch(State)  
       {
       case 0:  Serial.println("229R goto station1"); break;
       case 1:  Serial.println("229G goto station2"); break;
       case 2:  Serial.println("230R goto station3"); break;
       case 3:  Serial.println("230G goto station4"); break;
       case 4:  Serial.println("231R goto station5"); break;
       case 5:  Serial.println("231G goto station6"); break;
       case 6:  Serial.println("232R goto station7"); break;
       case 7:  Serial.println("232G goto station8"); break;
       }
      break;
    case baseDecoderAddress+2:
     switch(State)  
       {
       case 0:  Serial.println("233R goto station9"); break;
       case 1:  Serial.println("233G goto station10"); break;
       case 2:  Serial.println("234R goto station11"); break;
       case 3:  Serial.println("234G goto station12"); break;
       case 4:  Serial.println("235R goto station13"); break;
       case 5:  Serial.println("235G goto station14"); break;
       case 6:  Serial.println("236R goto station15"); break;
       case 7:  Serial.println("236G goto station16"); break;
       }
      break;
    case baseDecoderAddress+3:
     switch(State)  
       {
       case 0:  Serial.println("237R goto station17"); break;
       case 1:  Serial.println("237G goto station18"); break;
       case 2:  Serial.println("238R goto station19"); break;
       case 3:  Serial.println("238G goto station20"); break;
       case 4:  Serial.println("239R goto station21"); break;
       case 5:  Serial.println("239G goto station22"); break;
       case 6:  Serial.println("240R goto station23"); break;
       case 7:  Serial.println("240G goto station24"); break;
       }
      break;
    default: break;
    }
}

De value of baseDecoderAddress can have 4 values starting at any value. Maybe i should invent something that that moves it to 1,2,3 and 4

Use constant cases and add in the base offset afterwards.

For testing is came up with this solution:

  // First check if the command is for this decoder
  if(testDecoderAddress > baseDecoderAddress-1 || testDecoderAddress < baseDecoderAddress+1)
    {
      // Calculate the decoder switch value
      DecoderAddress = (testDecoderAddress - baseDecoderAddress)+1;
      executeCommand(); 
     }

if(testDecoderAddress > baseDecoderAddress-1 || testDecoderAddress < baseDecoderAddress+1)

Think this expression will allways be true … so what is tested? Or do you need to switch the + and - ?

I am coding with the final result in mind.

int  baseDecoderAddress = 57;      // Default decoder address
int  testDecoderAddress ;      // Change this to 57..60
                                   // this equals turnouts 225..240
int  State = 0;                    // per address from 0..7

As in the NMRA specs CV1 holds the short decoder address witch i simulated by baseDecoderAddress. The decoder receive software produces a receivedDecoderAddress that is simulated by testDecoderAddress.

So you are correct in the asumption that in this sketch this is always true with the numbers shown here.

So you are correct in the asumption that in this sketch this is always true with the numbers shown here

It isn’t an assumption - the test will be valid for ALL values, whether they’re shown here or not!

If you want to check a range,

if (x >= minVal && x <= maxVal)

As in the NMRA specs CV1 holds the short decoder address witch i simulated

I have no idea what this means.

You are correct, i now have changed the code. Thnx
If you are interested see http://www.nmra.org/standards/DCC/standards_rps/DCCStds.html for more information.