Menu System- How to Return to my Main Menu?

OK, I'm new to all this so bear with me.

So I'm just playing around with a random menu system that opens other menu systems, which than have commands, and I'm just using the serial monitor as the display.

and my question is How to I return to my "Main Menu" once I open one of the other menus and test the commands?

for example, my Main Menu has an option is to press 'L', which than opens a 2nd menu screen, and ok I play around with the commands in that menu, now how do I go back to said Main Menu Screen.

I tried using adding a "return" in a case '3': which worked but it didn't re-display the main menu screen in the serial monitor and it also kept my LED2 on, which it's suppose to full reset(off).

Here is the the of what I have so far:

int Var;//Initializes Variable for options within Second Menu Screen POP-UP.
void setup() {
  Serial.begin(9600);//For communication to Serial Monitor.
  Serial.println("HELLO WELCOME TO THE MAIN MENU SCREEN FOR CHOOSING A MENU SCREEN");
  Serial.println(" ");
  Serial.println("L - LED Menu");
  Serial.println("B - Buzzer Menu");
  Serial.println("S - Light Sensor Menu"); 
  Serial.println(" ");
  Serial.println("Enter an option.");
}

void loop() {
   int Blink = 0;//Initialization for Blink of LED's.
    if(Serial.available())//send data only when you receive data:
    {
            int Menu = Serial.read();//Reads INPUT of MAIN Menu, In our case it's only INPUTS 'L', 'B', Or 'S'.
             switch(Menu)//Takes INPUT entered and performs taks according to the "case".
             {
                    case('L')://New Menu Screen POP-UP.
                     Serial.println(" ");
                     Serial.println("1 - Blink LEDs Alternately");
                     Serial.println("2 - Fade LEDs Alternately");
                     Serial.println(" ");
                     Serial.println("Enter an option.");    
    
                    while(1)//Loops
                    {   
                           if (Serial.available())//send data only when you receive data.
                           {
                                  Var = Serial.read();//Reads INPUT for 2nd Menu, in our case it's only '1' or '2'.
                                  switch(Var)//Takes INPUT entered and performs taks according to the "case".
                                  {
                                         case '1':
                                         Blink = 1;//Perform tasks based on when Blink is ==1.
                                         break;
                                         case '2':
                                         Blink = 2;//Perform tasks based on when Blink is ==2.
                                         break;                                                                        
                                  }
                           }
                           if (Blink==1)//When Blink is equal to 1 peform the tasks below.
                           {
                                  for(int LED = 5 ; LED < 7; LED++)//Initalizes Pins 5 and 6 for LEDs  
                                     //Alternates between each one continously.
                                  {
                                         pinMode(LED, OUTPUT);//Sets the LED that is currently in line to OUTPUT.
                                         digitalWrite(LED, HIGH);//Sets the 1st LED that is currently in line to HIGH.
                                         delay(150);//150ms delay.
                                         digitalWrite(LED, LOW);//Sets the 2nd LED that is currently in line to LOW.
                                  }
                           }
                           if (Blink==2)
                           { 
                                   int LED1 = 5;
                                   int LED2 = 6;
                                   for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) 
                                   { 
                                          analogWrite(LED1, fadeValue);// sets the value (range from 0 to 255).
                                          analogWrite(LED2, 255 - fadeValue);
                                          delay(30);// wait for 30 milliseconds to see the dimming effect.                            
                                   } 
                                   for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) 
// fade out from max to min in increments of 5 points.
                                   { 
                                          analogWrite(LED1, fadeValue);// sets the value (range from 0 to 255):
                                          analogWrite(LED2, 255 - fadeValue);         
                                          delay(30);// sets the value (range from 0 to 255):                            
                                   } 
                            }
                    }
                    case('B'):
                     Serial.println(" ");
                     Serial.println("1 - Sound an SOS Signal Repeatedly");
                     Serial.println("2 - Make a Continuous Low Tone");
                     Serial.println("3 - Make a High Pitched Beeping Sound");
                     Serial.println(" ");
                     Serial.println("Enter an option.");
                    break;
                    case('S'):
                     Serial.println(" ");
                     Serial.println("1 - Sound a Tone when it is Dark");
                     Serial.println("2 - Sound a Tone on Significant Light Changes");
                     Serial.println(" ");
                     Serial.println("Enter an option.");
                     break;     
             }
              
      }
     
  }

Hi,

First, displaying your main menu has to be part of the loop(). setup() is only executed once, before the loop().

Then, in all your submenus, you have to check for a special command '9' or 'M' or wheatever, that will break out of your while(1) loop.

                           if (Serial.available())//send data only when you receive data.
                           {
                                  Var = Serial.read();//Reads INPUT for 2nd Menu, in our case it's only '1' or '2'.
                                  switch(Var)//Takes INPUT entered and performs taks according to the "case".
                                  {
                                         case '1':
                                         Blink = 1;//Perform tasks based on when Blink is ==1.
                                         break;
                                         case '2':
                                         Blink = 2;//Perform tasks based on when Blink is ==2.
                                         break;                                                                        
                                  }
                                  if (Var == 'M') break;

Jacques

Yes, Delta_G,

You are right.

Jacques

Delta_G:
If you're careful about it, you can separate the code that chooses which menu from the code that runs the menu itself. If menu5 is a sub-menu of menu2 then that just means that there is a menu item in menu2 that sets the current menu to menu5 and a menu item in menu5 to exit that sets it back to menu2.

I don't really get the sentence(poor comprehension sorry).

If I were to illustrate it, would it look like this?

MAIN MENU
> MENU1
   > MENU1.1
      //other sub menu
      > RETURN TO MENU1
   > MENU1.2
      //other sub menu
      > RETURN TO MENU1
   > RETURN TO MAIN MENU
> MENU2
   > MENU2.1
   > MENU2.2
   > RETURN TO MAIN MENU
> MENU3
   > MENU3.1
   > MENU3.2
   > RETURN TO MAIN MENU

Create a variable to hold the current state each time a user switch menus and only displays what the current menu has so the highest level menu is not active?

so the highest level menu is not active?

Just activate the current menu. And the highest menu can be one of those.

Usually, you need a command to select the item in the current menu (or submenu) a command to go back to the parent menu. To make the menu universal, I suggest :

  • 'U' for Dp, the previous item in the current menu
  • 'D' for Down, for the next item in the current menu
  • 'R' for Right, to accempt the current menu item
  • 'L' for Left, Back-up to the parent menu.

Knowing the current menu item, you can easily navigate thru really complex menus easily.

Jacques