[SOLVED] "Switch case" within "Switch case"

I have had problems with "Switch case"-statements within a master menu "Switch case".
Although I can make another solution, I want to know if anybody else has experience the same problems.

Or (maybe) it is forbidden to do that.

It should be fine providing you don't try and initialise any variables in the code inside a switch.

It is not forbidden.

Post you code with problems or a smaller program that demonstrates the problem

I want to know if anybody else has experience the same problems

You haven't said what the problems are/were, so it's impossible to say.

I regularly use nested switch statements
they work fine
if you write them correctly!

Here the code of the program with the nested 'switch case'. It leads to wrong results!

char lcdDegrees =223 ;                // Setup a Degrees C LCD Symbol
int intMenu=1,  intSubmenu=3;
String strLine1, strLine2;               //Display lines 1 and 2

//=========================================================================================
void setup() {
Serial.begin(9600);
Mastercontroller();
}

//=========================================================================================
void loop() {
  // put your main code here, to run repeatedly: 
}
   
//=========================================================================================
void Mastercontroller() {
  
switch (intMenu){
      case 1:
          switch (intSubmenu){
             strLine1 = "Mainmenu 1"; 
             case 1:
                  strLine2 = "Gi: W/m2";     //Gi: W/m²
                  break;
             case 2:
                  strLine2 = "Tm: °C";       //Tm: °C
                  break;
             case 3:
                  strLine2 = "Tamb: °C";     //Tamb: °C
                  break;
             case 4:
                  strLine2 = "Pac:  W";      //Pac: W
                  break;
             case 5:
                  strLine2 = "Efi: kWh";     //Efi: 
                  break;
             case 6:
                  strLine2 = "Hi:  kWh/m2";  //Hi
                  break;
              } //End of submenu2, case=1
              delay(1);
              
      case 2:  
          strLine1= "Mainmenu 2";
          break;
      case 3:
          strLine1= "Mainmenu 3";
          break;
      }   //End switch intMenu

Serial.print("Answer should be: Mainmenu 1, Tamb: °C"); Serial.println("");
Serial.print("Result: "); Serial.print(strLine1); Serial.print ('\t'); Serial.print(strLine2);
}

Who knows what's wrong?

Too many Strings?

What does wrong results mean?
don’t you want a break at the end of case 1: of the main menu, or did you want it to fall through and run case 2: ?

Jack

Here's a massively simplified version - no nested switch/case constructs, no Strings, massively reduced memory consumption, no memory corruption ...

char lcdDegrees =223 ;                // Setup a Degrees C LCD Symbol
int intMenu=1,  intSubmenu=3;

const char *strLine1, *strLine2;
const char *topLines[] = {
  "Main Menu 1",
  "Main Menu 2",
  "Main Menu 3"
};

const char *secondLines[] = {
  "Gi: W/m2",
  "Tm: °C",
  "Tamb: °C",
  "Pac:  W",
  "Efi: kWh",
  "Hi:  kWh/m2"
};

//=========================================================================================
void setup() {
  Serial.begin(9600);
  Mastercontroller();
}

//=========================================================================================
void loop() {
  // put your main code here, to run repeatedly: 
}
   
//=========================================================================================
void Mastercontroller() {
  strLine1 = topLines[intMenu-1];
  switch (intMenu){
    case 1:
      strLine2 = secondLines[intSubmenu-1];
      break;
    case 2:
      break;
    case 3:
      break;
  }              
  Serial.print("Answer should be: Mainmenu 1, Tamb: °C"); Serial.println("");
  Serial.print("Result: "); Serial.print(strLine1); Serial.print ('\t'); Serial.print(strLine2);
}

Hi JackWp,

Indeed I forgot a BREAK between Case 1 and Case 2 of the mainmenu.
Instructions between 'switch' and 'case' will be ignored.

Thank you for your help!

Hi Majenko,

I can't use your solution, because I have to make a lot of calculations of measured values before I offer the stringlines to my LCD.

ArduinoStarter1:
Hi Majenko,

I can't use your solution, because I have to make a lot of calculations of measured values before I offer the stringlines to my LCD.

Use those constant char * strings as formats to sprintf?

eg: "Gi: %dW/m2"

Or, if you need floats, and don't have the IDE tweaked to allow floats, use %s to embed a string that is generated using dtostrf().

Manipulating of String objects is not something to undertake lightly on a small embedded microcontroller. Yes, it's "easy", but it is one of the biggest causes of headaches from code sketches randomly crashing for no apparent reason.