Switch/Case Improper Use?

I have spent about a week on this, no luck. I am nesting Switch/Case in another Switch/Case. Everything works fine until you get to Case 3 which is my "quit to main menu" option. In other words, quit to the OTHER case. Maybe there is no way to do this? Case 1 and 2 work perfectly. 3 is there to quit to the topMenu. What am I doing wrong before I go to Case B, C, D and E on the topMenu with about 3 cases per.

void loop() {  
 if (runOnceMain == true) {
   mainMenu();
   runOnceMain = false;   
 }  
 if (Serial.available()> 0 ) {  
   if (optionAtop == true) {
     topMenu = Serial.read();
     optionAtop = false;
     optionAsub = true;
   }
   else if (optionAsub == true) {
     subMenuA = Serial.read();
   }    
    switch (topMenu) { 
      case 'A' : case 'a' : {        
        topCaseA();          
        switch (subMenuA) {         
          case '1' : { 
            runOnceA = true;            
            while (subMenuA == '1') {
              if (runOnceA == true) {
                Serial.println("Where would you like the Pan Servo? Q to Quit\n");
                runOnceA = false;
              }
              if (Serial.available() > 0) {                 
                setPosAngle = Serial.parseFloat();         /* read user input into pos */      
                ServoPan.write(setPosAngle);
                Serial.print(" Pan Servo Position Angle in Degrees (0-180): ");
                Serial.print("[ ");
                Serial.print(setPosAngle); 
                Serial.println(" ] Degrees");                        
              }           
              while (Serial.available()==0) {}
              if (Serial.peek() == 'q' || Serial.peek() == 'Q') {
                Serial.println("\nExiting . . . .\n");
                runOnceA = true;
                optionAsub = false;
                topCaseA();                                
                break;      
              }              
            }
                                
            break;            
          }        
          case '2' : { 
            runOnceA = true;            
            while (subMenuA == '2') {
              if (runOnceA == true) {
                Serial.println("Where would you like the Tilt Servo? Q to Quit\n");
                runOnceA = false;
              }
              if (Serial.available() > 0) {                 
                setPosAngle = Serial.parseFloat();  
                ServoTilt.write(setPosAngle);
                Serial.print(" Tilt Servo Position Angle in Degrees (0-180): ");
                Serial.print("[ ");
                Serial.print(setPosAngle); 
                Serial.println(" ] Degrees");                        
              }           
              while (Serial.available()==0) {}
              if (Serial.peek() == 'q' || Serial.peek() == 'Q') {
                Serial.println("\nExiting . . . .\n");
                runOnceA = true;
                optionAsub = false;
                topCaseA();               
                break;      
              }
            }                         
            break;            
          }
          case '3' : {
          //optionAtop = true ;
          //optionAsub = false;
          runOnceMain = true;
          //runOnceA = false;
          //Serial.println("WTF?");
          topMenu;
          break; 
          
           
          
          }  
          //goto loopTop;        
        } /* End subMenu Switch */ 
                
         break;                 
      } /* End Case A */  
    } /* End topMenu Switch */
  } /* End Serial.available */
} /* End Void Loop */

maybe there is no way to get OUT of the inner case? even goto doesn't work. Thanks!

Start by indenting the code properly (press ctrl-T)

Not sure what is your problem.

byte x=3, y=30;

switch (x) {
   case 1:
   break;

   case 2:
   break;

   case 3: // we will enter that case as x is 3
      switch (y) {
         case 10:
         break;

         case 20:
         break;

         case 30:
            // we will get here as y is 30 and then 
            ....
          break; // this exits the second switch/case
   }
    //so we arrive here 
   break; //  this exits the first switch/case
}
... // and we keep going from here

Didn't know about Crl-T. Thanks!

I cant seem to get the mainMenu() to actually work. mainMenu is merely this:

void mainMenu() {
Serial.println("\n -- Super ServoMaster Top Menu -- ");
Serial.println(" A) Calibrate B) Auto-Sweep C) Pot Control D) Self Center\n");
}
I can get it to display, I just cant get it to WORK. Pressing A just brings the menu back up. Its not jiving with the rest of the code. Here is the whole code so there are no questions about the fact that I do not know what I am doing!

#include <Servo.h>

Servo ServoPan;  int servoPin1 = 18; /* to hard code these: ex. int servoPin1 = 8 */
Servo ServoTilt; int servoPin2 = 19; /* to hard code these: ex. int servoPin1 = 9 */


int panLowLimit  =   0;       /* Set this variable for your Pan Servo's Low Limit */
int panHighLimit = 175;      /* Set this variable for your Pan Servo's High Limit */

int tiltLowLimit  =   0;     /* Set this variable for your Tilt Servo's Low Limit */
int tiltHighLimit = 176;    /* Set this variable for your Tilt Servo's High Limit */

char topMenu;
char subMenuA;


bool optionAtop = true;
bool optionAsub = true;


bool runOnceMain = true;
bool runOnceA = true;

int setPosAngle;

void setup() {
  Serial.begin (115200);
  pinMode (servoPin1, OUTPUT);  pinMode (servoPin2, OUTPUT);
  pinMode (analogPin1, INPUT);  pinMode (analogPin2, INPUT);
  /* Attach, Write and Center both Servos on Startup */
  ServoPan.attach(servoPin1); ServoPan.write((panHighLimit - panLowLimit) / 2);
  ServoTilt.attach(servoPin2); ServoTilt.write((tiltHighLimit - tiltLowLimit) / 2);
}

void mainMenu() {
  Serial.println("\n                     -- Super ServoMaster Top Menu --                       ");
  Serial.println(" A) Calibrate      B) Auto-Sweep       C) Pot Control      D) Self Center\n");
}

void subMenuA1() {
  Serial.println("\n               -- Super ServoMaster Calibration Menu A --                   ");
  Serial.println(" 1)  Set PanServo        2)  Set TiltServo           Q) Exit to Main Menu\n");
}

void topCaseA() {
  optionAsub = true;
  if (runOnceA == true) {
    subMenuA1();
    runOnceA = false;
  }
}

void loop() {
  if (runOnceMain == true) {
    mainMenu();
    runOnceMain = false;
  }
  if (Serial.available() > 0 ) {
    if (optionAtop == true) {
      topMenu = Serial.read();
      optionAtop = false;
      optionAsub = true;
    }
    else if (optionAsub == true) {
      subMenuA = Serial.read();
    }
    switch (topMenu) {
      case 'A' : case 'a' : {
          topCaseA();
          switch (subMenuA) {
            case '1' : {
                runOnceA = true;
                while (subMenuA == '1') {
                  if (runOnceA == true) {
                    Serial.println("Where would you like the Pan Servo? Q to Quit\n");
                    runOnceA = false;
                  }
                  if (Serial.available() > 0) {
                    setPosAngle = Serial.parseFloat();         /* read user input into pos */
                    ServoPan.write(setPosAngle);
                    Serial.print(" Pan Servo Position Angle in Degrees (0-180): ");
                    Serial.print("[ ");
                    Serial.print(setPosAngle);
                    Serial.println(" ] Degrees");
                  }
                  while (Serial.available() == 0) {}
                  if (Serial.peek() == 'q' || Serial.peek() == 'Q') {
                    Serial.println("\nExiting . . . .\n");
                    runOnceA = true;
                    optionAsub = false;
                    topCaseA();
                    break;
                  }
                }

                break;
              }
            case '2' : {
                runOnceA = true;
                while (subMenuA == '2') {
                  if (runOnceA == true) {
                    Serial.println("Where would you like the Tilt Servo? Q to Quit\n");
                    runOnceA = false;
                  }
                  if (Serial.available() > 0) {
                    setPosAngle = Serial.parseFloat();
                    ServoTilt.write(setPosAngle);
                    Serial.print(" Tilt Servo Position Angle in Degrees (0-180): ");
                    Serial.print("[ ");
                    Serial.print(setPosAngle);
                    Serial.println(" ] Degrees");
                  }
                  while (Serial.available() == 0) {}
                  if (Serial.peek() == 'q' || Serial.peek() == 'Q') {
                    Serial.println("\nExiting . . . .\n");
                    runOnceA = true;
                    optionAsub = false;
                    topCaseA();
                    break;
                  }
                }
                break;
              }
            case '3' : {
                //optionAtop = true ;
                //optionAsub = false;
                runOnceMain = true;
                //runOnceA = false;
                //Serial.println("WTF?");
                topMenu;
                break;



              }
              //goto loopTop;
          } /* End subMenu Switch */

          break;
        } /* End Case A */
    } /* End topMenu Switch */
  } /* End Serial.available */
} /* End Void Loop */

more to it that I have omitted to get to the problem. Thanks again!

                topMenu;

What is this statement supposed to do?

Pete

It's me losing it. I am at my wits end after days of staring at this code trying to get something... ANYthing to work. I cannot for the life of me figure out how to get out of the submenu and back to the top menu properly. There is no function to point to, no variable to point to... inexperience is my best defense!

How about:

   case 3:
    runOnceMain = true;
    return;   // return from loop(), which will then be run again.

Thank You! I learned about Control + T and the return statement. Can't beat progress. That solved it! Thanks Again!

Deve:
Thank You! I learned about Control + T and the return statement. Can't beat progress. That solved it! Thanks Again!

ctrl-T is a shortcut for a menu item in the IDE. You can select it there if you want.