Skip to main content
edited title
Link
Michel Keijzers
  • 13k
  • 7
  • 43
  • 59

Nested "switch" statmentsstatements menu

code updated based on @jsotola recommendation
Source Link
MDChaara
  • 207
  • 1
  • 3
  • 13

My current code is as follows: [Note: Updated with a function to handle screen updates based on @jsotola]

/*
 * Buttons Menu Sketch
 * Three buttons with a 1602 i2c LCD Display
 * 2 buttons for up/down and one button for select
 * Variation of the code on https://www.instructables.com/id/Arduino-LCD-16x2-Turn-Single-Screen-Into-Multiple-/
 * copy right statement goes here
 */

 
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

#include <EEPROM.h>     //EEPROM Functions: EEPROM Module
#include <Arduino.h>    //EEPROM Functions: for type definitions

LiquidCrystal_I2C lcd(0x27,16,2);


//Globals:
int page_counter    =   1 ; //To move between pages
int page_counter_s  = 1;  //submenu

int up = 2;       //Up button
int down = 3;     //Down button
int selection = 4;  1;  //Select buttonsubmenu

int up = 2;             //Up button
int down = 3;           //Down button
int selection = 4;      //Select button
int LED =   13;

int pageCount   =   7;  //Main Menu

 
/*
Storage debounce function
*/
 
//Main Menu 
boolean last_up         =   LOW;            
boolean last_down       =   LOW;
boolean last_select     =   LOW;

boolean current_up      =   LOW;  
boolean current_down    =   LOW;
boolean current_select  =   LOW;

boolean last_up_s     =   LOW;            
boolean last_down_s     =   LOW;

boolean current_up_s    =   LOW;  
boolean current_down_s  =   LOW;

String line1;
String line2;


//---- De-bouncing function for all buttons----//
boolean debounce(boolean last, int pin){
  boolean current = digitalRead(pin);
  if (last != current){
    delay(5);
    current = digitalRead(pin);
  }
  return current;
}

//LCD Function

void printlcd(){
    lcd.setCursor(0,0);
    lcd.print(line1);
    lcd.setCursor(0,1);
    lcd.print(line2);
}

//Menu Function
void Menus(){                                                                                                                                                                                                                                                                           

    current_up = debounce(last_up, up);         //Debounce for Up button
    current_down = debounce(last_down, down);   //Debounce for Down button
    current_select = debounce(last_select, selection); //Debounce for Select button
    //----Page counter function to move pages----//
  
    //Page Up
    if (last_up == LOW && current_up == HIGH){  //When up button is pressed
      lcd.clear();                     //When page is changed, lcd clear to print new page      
      
      if(page_counter < pageCount){             //Page counter never higher than 3(total of pages)
        page_counter= page_counter +1;   //Page up
      }
      else{
        page_counter = pageCount;  
      }
    }
    
    last_up = current_up;

    //Page DownUp
        if (last_down==last_up == LOW && current_downcurrent_up == HIGH){  //When downup button is pressed
          lcd.clear();                     //When page is changed, lcd clear to print new page        
          
            if(page_counter >1< pageCount){              //Page counter never lowerhigher than 1 3(total of pages)
                page_counter= page_counter -1;+1;   //Page downup
            }
            else{
                page_counter = pageCount;  
            }
        }
      
      }
      else{
      page_counter= 1;  
     }
    }
    
    last_downlast_up = current_down;current_up;

        //Page Down
        if (last_down== LOW && current_down == HIGH){ //When down button is pressed
          lcd.clear();                     //When page is changed, lcd clear to print new page    
          if(page_counter >1){              //Page counter never lower than 1 (total of pages)
          page_counter= page_counter -1;   //Page down
          
          }
          else{
          page_counter= 1;  
         }
        }
        
        last_down = current_down;

    


//------- Switch function to write and show what you want---// 
  switch (page_counter) {

   
    case 1:{ //Each case is a menu option
     
  lcd.setCursor(0,0);
    line1 = lcd.print("Counter 01:");";
      lcd.setCursor(0,1);line2 = "100 Hrs";
      lcd.printprintlcd("100 Hrs");
    }
    break;

    case 2:{ //Each case is a menu option
      lcd.setCursor(0,0);
    line1 = lcd.print("Counter 02:");";
      lcd.setCursor(0,1);line2 = "70 Hrs";
      lcd.printprintlcd("70 Hrs");
    }
    break;
  
  case 3:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Reset C01");
      lcd.setCursor(0,1);
      lcd.print("Timer");
    
    //Action:
    if (last_select == LOW && current_select ==case HIGH)3:{ //When downEach buttoncase is pressed
    a menu lcd.clear();option
      lcd.setCursor(0,0);
line1 = "Reset 01:";
   lcd.print("C01 Timer reset");
    line2 = lcd.setCursor(0,1);"";
      lcd.printprintlcd("was successful");
      
      //Action:
      if (last_select == LOW && current_select == HIGH){ //When down button is pressed
            lcd.clear();
            line1 = "Success!";
            line2 = "C01 Reset";
            printlcd();
            
            digitalWrite(LED,HIGH);
            delay(500);
            digitalWrite(LED,LOW);
            
            delay(3000);
            lcd.clear(); 

        }
    }
    break;
  
  case 4:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Reset C02");
      lcd.setCursor(0,1);
      lcd.print("Timer");
    
    //Action:
    if (last_select == LOW && current_select ==case HIGH)4:{ //When downEach buttoncase is pressed
    a menu lcd.clear();option
      lcd.setCursor(0,0);
line1 = "Reset 02:";
   lcd.print("C02 Timer reset");
    line2 = lcd.setCursor(0,1);"";
      lcd.printprintlcd("was successful");
      
      //Action:
      if (last_select == LOW && current_select == HIGH){ //When down button is pressed
            lcd.clear();
            line1 = "Success!";
            line2 = "C02 Reset";
            printlcd();
            
            digitalWrite(LED,HIGH);
            delay(500);
            digitalWrite(LED,LOW);
            
            delay(3000);
            lcd.clear();

        }
    }
    break;
    
    
    case 5:{ //Each case is a menu option
     
  lcd.setCursor(0,0);
      lcd.print("SETline1 C01");= "Set 01";
      lcd.setCursor(0,1);  line2 = "Interval";
      lcd.print  printlcd("Interval"); 

      //Action:
        if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      
       lcd.clear();
            submenu();
        }
    }
  }
    break;
  
  case 6:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("SET C02");
      lcd.setCursor(0,1);
      lcd.print("Interval");
    
    case 6:{ //Each case is a menu option
        line1 = "Set 02";
        line2 = "Interval";
        printlcd();

      //Action:
        if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      
         submenulcd.clear();
            submenu();
        }
    }
    break;
  
  case 7:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Exit Service");
      lcd.setCursor(0,1);
      lcd.print("Mode");
    
    case 7:{ //Each case is a menu option
        line1 = "Exit service";
        line2 = "mode";
        printlcd();
        
      //Action:
        if (last_select == LOW && current_select == HIGH){ //When down button is pressed
            
            lcd.clear();
      lcd.setCursor(0,0);      line1 = "Exiting";
      lcd.print("Exiting");      line2 = "Please Wait";
      lcd.setCursor      printlcd(0,1);
      lcd.print("Please Wait");     
      
       digitalWrite(LED,HIGH);
            delay(500);
            digitalWrite(LED,LOW);
            delay(500);
            
            delay(3000);
            lcd.clear();
            }
    }
    break;
    
  }//switch end
 
}

void submenu(){
  
  current_up_s = debounce(last_up, up);         //Debounce for Up button
  current_down_s = debounce(last_down, down);   //Debounce for Down button
  current_select = debounce(last_select, selection); //Debounce for Select button
  //----Page counter function to move pages----//
  
    //Page Up
    if (last_up == LOW && current_up_s == HIGH){  //When up button is pressed
      lcd.clear();                     //When page is changed, lcd clear to print new page      
      
      if(page_counter_s < 8){             //Page counter never higher than 3(total of pages)
        page_counter_s = page_counter_s +1;   //Page up
      }
      else{
        page_counter_s = 8;  
      }
    }
    
    last_upcurrent_up_s = current_up_s;

    //Page Down
  debounce(last_up, up); if (last_down == LOW && current_down_s == HIGH){ //When downDebounce buttonfor isUp pressedbutton
    current_down_s = lcd.cleardebounce(last_down, down);                     //When page is changed, lcd clear to print new page Debounce for Down button
    current_select = ifdebounce(page_counterlast_select, >1selection){      ; //Debounce for Select button
    //----Page counter never lower than 1function (totalto ofmove pages)
      page_counter_s = page_counter_s -1;   ---//Page down
      
      }
      else{
      page_counter_s = 1;  
     }
    }
    
    last_down    //Page Up
        if (last_up == LOW && current_up_s == HIGH){  //When up button is pressed
          lcd.clear();                     //When page is changed, lcd clear to print new page        
          
            if(page_counter_s < 8){             //Page counter never higher than 3(total of pages)
                page_counter_s = current_down_s;page_counter_s +1;   //Page up
            }
            else{
                page_counter_s = 8;  
            }
        }
      
        last_up = current_up_s;

        //Page Down
        if (last_down == LOW && current_down_s == HIGH){ //When down button is pressed
          lcd.clear();                     //When page is changed, lcd clear to print new page    
          if(page_counter >1){              //Page counter never lower than 1 (total of pages)
          page_counter_s = page_counter_s -1;   //Page down
          
          }
          else{
          page_counter_s = 1;  
         }
        }
        
        last_down = current_down_s;

    


//------- Switch function to write and show what you want---// 
  switch (page_counter_s) {

   
  
  case 1:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Set dealy To:");
      lcd.setCursor(0,1);
      lcd.print("100 hrs");
    
    //Action:
    if (last_select == LOW && current_select ==case HIGH)1:{ //When downEach buttoncase is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("New Dealy:");
      lcd.setCursor(0,1);
     a lcd.print("100menu Hrs.");option
      
      line1 =   "Set dealy to:";
      line2 =   "100 hrs";
      printlcd();
      //Action:
      if (last_select == LOW && current_select == HIGH){ //When down button is pressed
            lcd.clear();
            line1 = "New Delay:";;
            line2 = "100 hrs";
            printlcd();
            
            digitalWrite(LED,HIGH);
            delay(500);
            digitalWrite(LED,LOW);
            delay(3000);
            lcd.clear();
        }
    }
    break;
  
  case 2:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Set dealy To:");
      lcd.setCursor(0,1);
      lcd.print("200 hrs");
    
    //Action:
    if (last_select == LOW && current_select ==case HIGH)2:{ //When downEach buttoncase is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("New Dealy:");
      lcd.setCursor(0,1);
     a lcd.print("200menu Hrs.");option
      
      line1 =   "Set dealy to:";
      line2 =   "200 hrs";
      printlcd();
      //Action:
      if (last_select == LOW && current_select == HIGH){ //When down button is pressed
            lcd.clear();
            line1 = "New Delay:";
            line2 = "200 hrs";
            printlcd();
            
            digitalWrite(LED,HIGH);
            delay(500);
            digitalWrite(LED,LOW);
            delay(3000);
            lcd.clear();
        }
    }
    break;
  
  case 3:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Set dealy To:");
      lcd.setCursor(0,1);
      lcd.print("300 hrs");
    
    //Action:
    if (last_select == LOW && current_select ==case HIGH)3:{ //When downEach buttoncase is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("New Dealy:");
      lcd.setCursor(0,1);
     a lcd.print("300menu Hrs.");option
      
      line1 =   "Set dealy to:";
      line2 =   "300 hrs";
      printlcd();
      //Action:
      if (last_select == LOW && current_select == HIGH){ //When down button is pressed
            lcd.clear();
            line1 = "New Delay:";
            line2 = "300 hrs";
            printlcd();
            
            digitalWrite(LED,HIGH);
            delay(500);
            digitalWrite(LED,LOW);
            delay(3000);
            lcd.clear();
        }
    }
    break;
  
  case 4:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Set dealy To:");
      lcd.setCursor(0,1);
      lcd.print("400 hrs");
    
    //Action:
    if (last_select == LOW && current_select ==case HIGH)4:{ //When downEach buttoncase is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("New Dealy:");
      lcd.setCursor(0,1);
     a lcd.print("400menu Hrs.");option
      
      line1 =   "Set dealy to:";
      line2 =   "400 hrs";
      printlcd();
      //Action:
      if (last_select == LOW && current_select == HIGH){ //When down button is pressed
            lcd.clear();
            line1 = "New Delay:";
            line2 = "400 hrs";
            printlcd();
            
            digitalWrite(LED,HIGH);
            delay(500);
            digitalWrite(LED,LOW);
            delay(3000);
            lcd.clear();
        }
    }
    break;
  
  case 5:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Set dealy To:");
      lcd.setCursor(0,1);
      lcd.print("500 hrs");
    
    //Action:
    if (last_select == LOW && current_select ==case HIGH)5:{ //When downEach buttoncase is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("New Dealy:");
      lcd.setCursor(0,1);
     a lcd.print("500menu Hrs.");option
      
      line1 =   "Set dealy to:";
      line2 =   "500 hrs";
      printlcd();
      //Action:
      if (last_select == LOW && current_select == HIGH){ //When down button is pressed
            lcd.clear();
            line1 = "New Delay:";
            line2 = "500 hrs";
            printlcd();
            
            digitalWrite(LED,HIGH);
            delay(500);
            digitalWrite(LED,LOW);
            delay(3000);
            lcd.clear();
        }
    }
    break;
  
  case 6:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Set dealy To:");
      lcd.setCursor(0,1);
      lcd.print("600 hrs");
    
    //Action:
    if (last_select == LOW && current_select ==case HIGH)6:{ //When downEach buttoncase is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("New Dealy:");
      lcd.setCursor(0,1);
     a lcd.print("600menu Hrs.");option
      
      line1 =   "Set dealy to:";
      line2 =   "600 hrs";
      printlcd();
      //Action:
      if (last_select == LOW && current_select == HIGH){ //When down button is pressed
            lcd.clear();
            line1 = "New Delay:";
            line2 = "600 hrs";
            printlcd();
            
            digitalWrite(LED,HIGH);
            delay(500);
            digitalWrite(LED,LOW);
            delay(3000);
            lcd.clear();
        }
    }
    break;
  
  case 7:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Set dealy To:");
      lcd.setCursor(0,1);
      lcd.print("700 hrs");
    
    //Action:
    if (last_select == LOW && current_select ==case HIGH)7:{ //When downEach buttoncase is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("New Dealy:");
      lcd.setCursor(0,1);
     a lcd.print("700menu Hrs.");option
      
      line1 =   "Set dealy to:";
      line2 =   "700 hrs";
      printlcd();
      //Action:
      if (last_select == LOW && current_select == HIGH){ //When down button is pressed
            lcd.clear();
            line1 = "New Delay:";
            line2 = "700 hrs";
            printlcd();
            
            digitalWrite(LED,HIGH);
            delay(500);
            digitalWrite(LED,LOW);
            delay(3000);
            lcd.clear();
        }
    }
    break;
  
  case 8:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Back");
      lcd.setCursor(0,1);
      lcd.print("200 hrs");
    
    //Action:
    if (last_select == LOW && current_select ==case HIGH)8:{ //When downEach buttoncase is presseda menu option
      
      line1 =   "Back";
      line2 =   "";
      printlcd();
      //ExitAction:
 function code    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
            lcd.clear();
            line1 = "Returning";
            line2 = "";
            printlcd();
            
            digitalWrite(LED,HIGH);
            delay(500);
            digitalWrite(LED,LOW);
            delay(3000);
            lcd.clear();
        }
    }
    break;
    
  }//switch end
    
}




void setup() {
  // put your setup code here, to run once:
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("Menu Test");
  delay(3000);
  lcd.clear();  
}

void loop() {
  

  Menus();
}

My current code is as follows:

/*
 * Buttons Menu Sketch
 * Three buttons with a 1602 i2c LCD Display
 * 2 buttons for up/down and one button for select
 * Variation of the code on https://www.instructables.com/id/Arduino-LCD-16x2-Turn-Single-Screen-Into-Multiple-/
 * copy right statement goes here
 */

 
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

#include <EEPROM.h>     //EEPROM Functions: EEPROM Module
#include <Arduino.h>    //EEPROM Functions: for type definitions

LiquidCrystal_I2C lcd(0x27,16,2);


//Globals:
int page_counter  = 1 ; //To move between pages
int page_counter_s  = 1;  //submenu

int up = 2;       //Up button
int down = 3;     //Down button
int selection = 4;    //Select button

int LED = 13;

int pageCount   =   7;  //Main Menu

 
/*
Storage debounce function
*/
 
//Main Menu 
boolean last_up     =   LOW;            
boolean last_down     =   LOW;
boolean last_select   =   LOW;

boolean current_up    =   LOW;  
boolean current_down  =   LOW;
boolean current_select  =   LOW;

boolean last_up_s     =   LOW;            
boolean last_down_s     =   LOW;

boolean current_up_s    =   LOW;  
boolean current_down_s  =   LOW;



//---- De-bouncing function for all buttons----//
boolean debounce(boolean last, int pin){
  boolean current = digitalRead(pin);
  if (last != current){
    delay(5);
    current = digitalRead(pin);
  }
  return current;
}

//Menu Function
void Menus(){                                                                                                                                     

  current_up = debounce(last_up, up);         //Debounce for Up button
  current_down = debounce(last_down, down);   //Debounce for Down button
  current_select = debounce(last_select, selection); //Debounce for Select button
  //----Page counter function to move pages----//
  
    //Page Up
    if (last_up == LOW && current_up == HIGH){  //When up button is pressed
      lcd.clear();                     //When page is changed, lcd clear to print new page      
      
      if(page_counter < pageCount){             //Page counter never higher than 3(total of pages)
        page_counter= page_counter +1;   //Page up
      }
      else{
        page_counter = pageCount;  
      }
    }
    
    last_up = current_up;

    //Page Down
    if (last_down== LOW && current_down == HIGH){ //When down button is pressed
      lcd.clear();                     //When page is changed, lcd clear to print new page    
      if(page_counter >1){              //Page counter never lower than 1 (total of pages)
      page_counter= page_counter -1;   //Page down
      
      }
      else{
      page_counter= 1;  
     }
    }
    
    last_down = current_down;

  


//------- Switch function to write and show what you want---// 
  switch (page_counter) {

   
    case 1:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Counter 01:");
      lcd.setCursor(0,1);
      lcd.print("100 Hrs");
    }
    break;

    case 2:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Counter 02:");
      lcd.setCursor(0,1);
      lcd.print("70 Hrs");
    }
    break;
  
  case 3:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Reset C01");
      lcd.setCursor(0,1);
      lcd.print("Timer");
    
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("C01 Timer reset");
      lcd.setCursor(0,1);
      lcd.print("was successful");
      
      digitalWrite(LED,HIGH);
      delay(500);
      digitalWrite(LED,LOW);
      delay(3000);
      lcd.clear();
    }
    }
    break;
  
  case 4:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Reset C02");
      lcd.setCursor(0,1);
      lcd.print("Timer");
    
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("C02 Timer reset");
      lcd.setCursor(0,1);
      lcd.print("was successful");
      
      digitalWrite(LED,HIGH);
      delay(500);
      digitalWrite(LED,LOW);
      delay(3000);
      lcd.clear();

    }
    }
    break;
  
  
  case 5:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("SET C01");
      lcd.setCursor(0,1);
      lcd.print("Interval");
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      
       
      submenu();
    }
  }
    break;
  
  case 6:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("SET C02");
      lcd.setCursor(0,1);
      lcd.print("Interval");
    
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      
         submenu();
        
    }
    }
    break;
  
  case 7:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Exit Service");
      lcd.setCursor(0,1);
      lcd.print("Mode");
    
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Exiting");
      lcd.setCursor(0,1);
      lcd.print("Please Wait");
      
       digitalWrite(LED,HIGH);
      delay(500);
      digitalWrite(LED,LOW);
      delay(500);
      
      delay(3000);
      lcd.clear();
      }
    }
    break;
    
  }//switch end
 
}

void submenu(){
  
  current_up_s = debounce(last_up, up);         //Debounce for Up button
  current_down_s = debounce(last_down, down);   //Debounce for Down button
  current_select = debounce(last_select, selection); //Debounce for Select button
  //----Page counter function to move pages----//
  
    //Page Up
    if (last_up == LOW && current_up_s == HIGH){  //When up button is pressed
      lcd.clear();                     //When page is changed, lcd clear to print new page      
      
      if(page_counter_s < 8){             //Page counter never higher than 3(total of pages)
        page_counter_s = page_counter_s +1;   //Page up
      }
      else{
        page_counter_s = 8;  
      }
    }
    
    last_up = current_up_s;

    //Page Down
    if (last_down == LOW && current_down_s == HIGH){ //When down button is pressed
      lcd.clear();                     //When page is changed, lcd clear to print new page    
      if(page_counter >1){              //Page counter never lower than 1 (total of pages)
      page_counter_s = page_counter_s -1;   //Page down
      
      }
      else{
      page_counter_s = 1;  
     }
    }
    
    last_down = current_down_s;

  


//------- Switch function to write and show what you want---// 
  switch (page_counter_s) {

   
  
  case 1:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Set dealy To:");
      lcd.setCursor(0,1);
      lcd.print("100 hrs");
    
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("New Dealy:");
      lcd.setCursor(0,1);
      lcd.print("100 Hrs.");
      
      digitalWrite(LED,HIGH);
      delay(500);
      digitalWrite(LED,LOW);
      delay(3000);
      lcd.clear();
    }
    }
    break;
  
  case 2:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Set dealy To:");
      lcd.setCursor(0,1);
      lcd.print("200 hrs");
    
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("New Dealy:");
      lcd.setCursor(0,1);
      lcd.print("200 Hrs.");
      
      digitalWrite(LED,HIGH);
      delay(500);
      digitalWrite(LED,LOW);
      delay(3000);
      lcd.clear();
    }
    }
    break;
  
  case 3:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Set dealy To:");
      lcd.setCursor(0,1);
      lcd.print("300 hrs");
    
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("New Dealy:");
      lcd.setCursor(0,1);
      lcd.print("300 Hrs.");
      
      digitalWrite(LED,HIGH);
      delay(500);
      digitalWrite(LED,LOW);
      delay(3000);
      lcd.clear();
    }
    }
    break;
  
  case 4:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Set dealy To:");
      lcd.setCursor(0,1);
      lcd.print("400 hrs");
    
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("New Dealy:");
      lcd.setCursor(0,1);
      lcd.print("400 Hrs.");
      
      digitalWrite(LED,HIGH);
      delay(500);
      digitalWrite(LED,LOW);
      delay(3000);
      lcd.clear();
    }
    }
    break;
  
  case 5:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Set dealy To:");
      lcd.setCursor(0,1);
      lcd.print("500 hrs");
    
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("New Dealy:");
      lcd.setCursor(0,1);
      lcd.print("500 Hrs.");
      
      digitalWrite(LED,HIGH);
      delay(500);
      digitalWrite(LED,LOW);
      delay(3000);
      lcd.clear();
    }
    }
    break;
  
  case 6:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Set dealy To:");
      lcd.setCursor(0,1);
      lcd.print("600 hrs");
    
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("New Dealy:");
      lcd.setCursor(0,1);
      lcd.print("600 Hrs.");
      
      digitalWrite(LED,HIGH);
      delay(500);
      digitalWrite(LED,LOW);
      delay(3000);
      lcd.clear();
    }
    }
    break;
  
  case 7:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Set dealy To:");
      lcd.setCursor(0,1);
      lcd.print("700 hrs");
    
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("New Dealy:");
      lcd.setCursor(0,1);
      lcd.print("700 Hrs.");
      
      digitalWrite(LED,HIGH);
      delay(500);
      digitalWrite(LED,LOW);
      delay(3000);
      lcd.clear();
    }
    }
    break;
  
  case 8:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Back");
      lcd.setCursor(0,1);
      lcd.print("200 hrs");
    
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      
      //Exit function code
    }
    }
    break;
    
  }//switch end
  
}




void setup() {
  // put your setup code here, to run once:
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("Menu Test");
  delay(3000);
  lcd.clear();  
}

void loop() {
  

  Menus();
}

My current code is as follows: [Note: Updated with a function to handle screen updates based on @jsotola]

#include <LiquidCrystal_I2C.h>
#include <Wire.h>

#include <EEPROM.h>     //EEPROM Functions: EEPROM Module
#include <Arduino.h>    //EEPROM Functions: for type definitions

LiquidCrystal_I2C lcd(0x27,16,2);


//Globals:
int page_counter    =   1 ; //To move between pages
int page_counter_s  =   1;  //submenu

int up = 2;             //Up button
int down = 3;           //Down button
int selection = 4;      //Select button
int LED =   13;

int pageCount   =   7;  //Main Menu

 
//Main Menu 
boolean last_up         =   LOW;            
boolean last_down       =   LOW;
boolean last_select     =   LOW;

boolean current_up      =   LOW;  
boolean current_down    =   LOW;
boolean current_select  =   LOW;

boolean last_up_s     =   LOW;            
boolean last_down_s     =   LOW;

boolean current_up_s    =   LOW;  
boolean current_down_s  =   LOW;

String line1;
String line2;


//---- De-bouncing function for all buttons----//
boolean debounce(boolean last, int pin){
  boolean current = digitalRead(pin);
  if (last != current){
    delay(5);
    current = digitalRead(pin);
  }
  return current;
}

//LCD Function

void printlcd(){
    lcd.setCursor(0,0);
    lcd.print(line1);
    lcd.setCursor(0,1);
    lcd.print(line2);
}

//Menu Function
void Menus(){                                                                                                                                                                                                                                                                           

    current_up = debounce(last_up, up);         //Debounce for Up button
    current_down = debounce(last_down, down);   //Debounce for Down button
    current_select = debounce(last_select, selection); //Debounce for Select button
    //----Page counter function to move pages----//
    
        //Page Up
        if (last_up == LOW && current_up == HIGH){  //When up button is pressed
          lcd.clear();                     //When page is changed, lcd clear to print new page        
          
            if(page_counter < pageCount){             //Page counter never higher than 3(total of pages)
                page_counter= page_counter +1;   //Page up
            }
            else{
                page_counter = pageCount;  
            }
        }
      
        last_up = current_up;

        //Page Down
        if (last_down== LOW && current_down == HIGH){ //When down button is pressed
          lcd.clear();                     //When page is changed, lcd clear to print new page    
          if(page_counter >1){              //Page counter never lower than 1 (total of pages)
          page_counter= page_counter -1;   //Page down
          
          }
          else{
          page_counter= 1;  
         }
        }
        
        last_down = current_down;

    


//------- Switch function to write and show what you want---// 
  switch (page_counter) {

   
    case 1:{ //Each case is a menu option
  
      line1 = "Counter 01:";
      line2 = "100 Hrs";
      printlcd();
    }
    break;

    case 2:{ //Each case is a menu option
      line1 = "Counter 02:";
      line2 = "70 Hrs";
      printlcd();
    }
    break;
    
    case 3:{ //Each case is a menu option
      line1 = "Reset 01:";
      line2 = "";
      printlcd();
      
      //Action:
      if (last_select == LOW && current_select == HIGH){ //When down button is pressed
            lcd.clear();
            line1 = "Success!";
            line2 = "C01 Reset";
            printlcd();
            
            digitalWrite(LED,HIGH);
            delay(500);
            digitalWrite(LED,LOW);
            
            delay(3000);
            lcd.clear(); 

        }
    }
    break;
    
    case 4:{ //Each case is a menu option
      line1 = "Reset 02:";
      line2 = "";
      printlcd();
      
      //Action:
      if (last_select == LOW && current_select == HIGH){ //When down button is pressed
            lcd.clear();
            line1 = "Success!";
            line2 = "C02 Reset";
            printlcd();
            
            digitalWrite(LED,HIGH);
            delay(500);
            digitalWrite(LED,LOW);
            
            delay(3000);
            lcd.clear();

        }
    }
    break;
    
    
    case 5:{ //Each case is a menu option
     
        line1 = "Set 01";
        line2 = "Interval";
        printlcd(); 

      //Action:
        if (last_select == LOW && current_select == HIGH){ //When down button is pressed
            lcd.clear();
            submenu();
        }
    }
    break;
    
    case 6:{ //Each case is a menu option
        line1 = "Set 02";
        line2 = "Interval";
        printlcd();

      //Action:
        if (last_select == LOW && current_select == HIGH){ //When down button is pressed
            lcd.clear();
            submenu();
        }
    }
    
    case 7:{ //Each case is a menu option
        line1 = "Exit service";
        line2 = "mode";
        printlcd();
        
      //Action:
        if (last_select == LOW && current_select == HIGH){ //When down button is pressed
            
            lcd.clear();
            line1 = "Exiting";
            line2 = "Please Wait";
            printlcd();
            
            digitalWrite(LED,HIGH);
            delay(500);
            digitalWrite(LED,LOW);
            delay(500);
            
            delay(3000);
            lcd.clear();
            }
    }
    break;
    
  }//switch end
 
}

void submenu(){
    
    current_up_s = debounce(last_up, up);         //Debounce for Up button
    current_down_s = debounce(last_down, down);   //Debounce for Down button
    current_select = debounce(last_select, selection); //Debounce for Select button
    //----Page counter function to move pages----//
    
        //Page Up
        if (last_up == LOW && current_up_s == HIGH){  //When up button is pressed
          lcd.clear();                     //When page is changed, lcd clear to print new page        
          
            if(page_counter_s < 8){             //Page counter never higher than 3(total of pages)
                page_counter_s = page_counter_s +1;   //Page up
            }
            else{
                page_counter_s = 8;  
            }
        }
      
        last_up = current_up_s;

        //Page Down
        if (last_down == LOW && current_down_s == HIGH){ //When down button is pressed
          lcd.clear();                     //When page is changed, lcd clear to print new page    
          if(page_counter >1){              //Page counter never lower than 1 (total of pages)
          page_counter_s = page_counter_s -1;   //Page down
          
          }
          else{
          page_counter_s = 1;  
         }
        }
        
        last_down = current_down_s;

    


//------- Switch function to write and show what you want---// 
  switch (page_counter_s) {

   
    
    case 1:{ //Each case is a menu option
      
      line1 =   "Set dealy to:";
      line2 =   "100 hrs";
      printlcd();
      //Action:
      if (last_select == LOW && current_select == HIGH){ //When down button is pressed
            lcd.clear();
            line1 = "New Delay:";;
            line2 = "100 hrs";
            printlcd();
            
            digitalWrite(LED,HIGH);
            delay(500);
            digitalWrite(LED,LOW);
            delay(3000);
            lcd.clear();
        }
    }
    break;
    
    case 2:{ //Each case is a menu option
      
      line1 =   "Set dealy to:";
      line2 =   "200 hrs";
      printlcd();
      //Action:
      if (last_select == LOW && current_select == HIGH){ //When down button is pressed
            lcd.clear();
            line1 = "New Delay:";
            line2 = "200 hrs";
            printlcd();
            
            digitalWrite(LED,HIGH);
            delay(500);
            digitalWrite(LED,LOW);
            delay(3000);
            lcd.clear();
        }
    }
    break;
    
    case 3:{ //Each case is a menu option
      
      line1 =   "Set dealy to:";
      line2 =   "300 hrs";
      printlcd();
      //Action:
      if (last_select == LOW && current_select == HIGH){ //When down button is pressed
            lcd.clear();
            line1 = "New Delay:";
            line2 = "300 hrs";
            printlcd();
            
            digitalWrite(LED,HIGH);
            delay(500);
            digitalWrite(LED,LOW);
            delay(3000);
            lcd.clear();
        }
    }
    break;
    
    case 4:{ //Each case is a menu option
      
      line1 =   "Set dealy to:";
      line2 =   "400 hrs";
      printlcd();
      //Action:
      if (last_select == LOW && current_select == HIGH){ //When down button is pressed
            lcd.clear();
            line1 = "New Delay:";
            line2 = "400 hrs";
            printlcd();
            
            digitalWrite(LED,HIGH);
            delay(500);
            digitalWrite(LED,LOW);
            delay(3000);
            lcd.clear();
        }
    }
    break;
    
    case 5:{ //Each case is a menu option
      
      line1 =   "Set dealy to:";
      line2 =   "500 hrs";
      printlcd();
      //Action:
      if (last_select == LOW && current_select == HIGH){ //When down button is pressed
            lcd.clear();
            line1 = "New Delay:";
            line2 = "500 hrs";
            printlcd();
            
            digitalWrite(LED,HIGH);
            delay(500);
            digitalWrite(LED,LOW);
            delay(3000);
            lcd.clear();
        }
    }
    break;
    
    case 6:{ //Each case is a menu option
      
      line1 =   "Set dealy to:";
      line2 =   "600 hrs";
      printlcd();
      //Action:
      if (last_select == LOW && current_select == HIGH){ //When down button is pressed
            lcd.clear();
            line1 = "New Delay:";
            line2 = "600 hrs";
            printlcd();
            
            digitalWrite(LED,HIGH);
            delay(500);
            digitalWrite(LED,LOW);
            delay(3000);
            lcd.clear();
        }
    }
    break;
    
    case 7:{ //Each case is a menu option
      
      line1 =   "Set dealy to:";
      line2 =   "700 hrs";
      printlcd();
      //Action:
      if (last_select == LOW && current_select == HIGH){ //When down button is pressed
            lcd.clear();
            line1 = "New Delay:";
            line2 = "700 hrs";
            printlcd();
            
            digitalWrite(LED,HIGH);
            delay(500);
            digitalWrite(LED,LOW);
            delay(3000);
            lcd.clear();
        }
    }
    break;
    
    case 8:{ //Each case is a menu option
      
      line1 =   "Back";
      line2 =   "";
      printlcd();
      //Action:
      if (last_select == LOW && current_select == HIGH){ //When down button is pressed
            lcd.clear();
            line1 = "Returning";
            line2 = "";
            printlcd();
            
            digitalWrite(LED,HIGH);
            delay(500);
            digitalWrite(LED,LOW);
            delay(3000);
            lcd.clear();
        }
    }
    break;
    
  }//switch end
    
}




void setup() {
  // put your setup code here, to run once:
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("Menu Test");
  delay(3000);
  lcd.clear();  
}

void loop() {
  

  Menus();
}
Source Link
MDChaara
  • 207
  • 1
  • 3
  • 13

Nested "switch" statments menu

I know that menus were discussed perhaps a thousand times here and everywhere. But using a ready solution is not something that I'm looking for because I know I won't learn the concept if I use them. Hence the question.

I am trying to build a simple timer with a setup menu. The timer code works so that part of the code is not a problem. The menu is an adaption of a code I found online which I understand, and for the most part, works as long as I am in level 1. My menu will have 2 levels. the structure of the menu is something like this:

Option 1: Display Timer 1

Option 2: Display Timer 2

Option 3: Reset Timer 1

Option 4: Reset Timer 2

Option 5: Set Timer 1 Action Delay

  • Option 5.1: Delay01
  • Option 5.2: Delay02
  • Option 5.3: Delay03
  • Option 5.4: Back

Option 6: Set Timer 2 Action Delay

  • Option 6.1: Delay01
  • Option 6.2: Delay02
  • Option 6.3: Delay03
  • Option 6.4: Back

The menu will be displayed with an I2C 1602 LCD Screen, and navigation will be by way of 3 push buttons: Up, Down, and Select.

I am using a nested "switch case" statement to navigate between the menus.

So as long as I'm in level 1, everything works. the times are displayed, reset works, and all is well. The moment I go to a submenu, the first option in the submenu is selected immediately and the display shows the parent menu.

I am putting the menu code in a function, the submenu code in another function. Once the menu with a submenu is selected, it calls the submenu function.

My current code is as follows:

/*
 * Buttons Menu Sketch
 * Three buttons with a 1602 i2c LCD Display
 * 2 buttons for up/down and one button for select
 * Variation of the code on https://www.instructables.com/id/Arduino-LCD-16x2-Turn-Single-Screen-Into-Multiple-/
 * copy right statement goes here
 */

 
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

#include <EEPROM.h>     //EEPROM Functions: EEPROM Module
#include <Arduino.h>    //EEPROM Functions: for type definitions

LiquidCrystal_I2C lcd(0x27,16,2);


//Globals:
int page_counter  = 1 ; //To move between pages
int page_counter_s  = 1;  //submenu

int up = 2;       //Up button
int down = 3;     //Down button
int selection = 4;    //Select button

int LED = 13;

int pageCount   =   7;  //Main Menu


/*
Storage debounce function
*/
 
//Main Menu 
boolean last_up     =   LOW;            
boolean last_down     =   LOW;
boolean last_select   =   LOW;

boolean current_up    =   LOW;  
boolean current_down  =   LOW;
boolean current_select  =   LOW;

boolean last_up_s     =   LOW;            
boolean last_down_s     =   LOW;

boolean current_up_s    =   LOW;  
boolean current_down_s  =   LOW;



//---- De-bouncing function for all buttons----//
boolean debounce(boolean last, int pin){
  boolean current = digitalRead(pin);
  if (last != current){
    delay(5);
    current = digitalRead(pin);
  }
  return current;
}

//Menu Function
void Menus(){                                                                                                                                     

  current_up = debounce(last_up, up);         //Debounce for Up button
  current_down = debounce(last_down, down);   //Debounce for Down button
  current_select = debounce(last_select, selection); //Debounce for Select button
  //----Page counter function to move pages----//
  
    //Page Up
    if (last_up == LOW && current_up == HIGH){  //When up button is pressed
      lcd.clear();                     //When page is changed, lcd clear to print new page      
      
      if(page_counter < pageCount){             //Page counter never higher than 3(total of pages)
        page_counter= page_counter +1;   //Page up
      }
      else{
        page_counter = pageCount;  
      }
    }
    
    last_up = current_up;

    //Page Down
    if (last_down== LOW && current_down == HIGH){ //When down button is pressed
      lcd.clear();                     //When page is changed, lcd clear to print new page    
      if(page_counter >1){              //Page counter never lower than 1 (total of pages)
      page_counter= page_counter -1;   //Page down
      
      }
      else{
      page_counter= 1;  
     }
    }
    
    last_down = current_down;

  


//------- Switch function to write and show what you want---// 
  switch (page_counter) {

   
    case 1:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Counter 01:");
      lcd.setCursor(0,1);
      lcd.print("100 Hrs");
    }
    break;

    case 2:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Counter 02:");
      lcd.setCursor(0,1);
      lcd.print("70 Hrs");
    }
    break;
  
  case 3:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Reset C01");
      lcd.setCursor(0,1);
      lcd.print("Timer");
    
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("C01 Timer reset");
      lcd.setCursor(0,1);
      lcd.print("was successful");
      
      digitalWrite(LED,HIGH);
      delay(500);
      digitalWrite(LED,LOW);
      delay(3000);
      lcd.clear();
    }
    }
    break;
  
  case 4:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Reset C02");
      lcd.setCursor(0,1);
      lcd.print("Timer");
    
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("C02 Timer reset");
      lcd.setCursor(0,1);
      lcd.print("was successful");
      
      digitalWrite(LED,HIGH);
      delay(500);
      digitalWrite(LED,LOW);
      delay(3000);
      lcd.clear();

    }
    }
    break;
  
  
  case 5:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("SET C01");
      lcd.setCursor(0,1);
      lcd.print("Interval");
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      
      
      submenu();
    }
  }
    break;
  
  case 6:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("SET C02");
      lcd.setCursor(0,1);
      lcd.print("Interval");
    
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      
        submenu();
        
    }
    }
    break;
  
  case 7:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Exit Service");
      lcd.setCursor(0,1);
      lcd.print("Mode");
    
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Exiting");
      lcd.setCursor(0,1);
      lcd.print("Please Wait");
      
      digitalWrite(LED,HIGH);
      delay(500);
      digitalWrite(LED,LOW);
      delay(500);
      
      delay(3000);
      lcd.clear();
      }
    }
    break;
    
  }//switch end
 
}

void submenu(){
  
  current_up_s = debounce(last_up, up);         //Debounce for Up button
  current_down_s = debounce(last_down, down);   //Debounce for Down button
  current_select = debounce(last_select, selection); //Debounce for Select button
  //----Page counter function to move pages----//
  
    //Page Up
    if (last_up == LOW && current_up_s == HIGH){  //When up button is pressed
      lcd.clear();                     //When page is changed, lcd clear to print new page      
      
      if(page_counter_s < 8){             //Page counter never higher than 3(total of pages)
        page_counter_s = page_counter_s +1;   //Page up
      }
      else{
        page_counter_s = 8;  
      }
    }
    
    last_up = current_up_s;

    //Page Down
    if (last_down == LOW && current_down_s == HIGH){ //When down button is pressed
      lcd.clear();                     //When page is changed, lcd clear to print new page    
      if(page_counter >1){              //Page counter never lower than 1 (total of pages)
      page_counter_s = page_counter_s -1;   //Page down
      
      }
      else{
      page_counter_s = 1;  
     }
    }
    
    last_down = current_down_s;

  


//------- Switch function to write and show what you want---// 
  switch (page_counter_s) {

   
  
  case 1:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Set dealy To:");
      lcd.setCursor(0,1);
      lcd.print("100 hrs");
    
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("New Dealy:");
      lcd.setCursor(0,1);
      lcd.print("100 Hrs.");
      
      digitalWrite(LED,HIGH);
      delay(500);
      digitalWrite(LED,LOW);
      delay(3000);
      lcd.clear();
    }
    }
    break;
  
  case 2:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Set dealy To:");
      lcd.setCursor(0,1);
      lcd.print("200 hrs");
    
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("New Dealy:");
      lcd.setCursor(0,1);
      lcd.print("200 Hrs.");
      
      digitalWrite(LED,HIGH);
      delay(500);
      digitalWrite(LED,LOW);
      delay(3000);
      lcd.clear();
    }
    }
    break;
  
  case 3:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Set dealy To:");
      lcd.setCursor(0,1);
      lcd.print("300 hrs");
    
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("New Dealy:");
      lcd.setCursor(0,1);
      lcd.print("300 Hrs.");
      
      digitalWrite(LED,HIGH);
      delay(500);
      digitalWrite(LED,LOW);
      delay(3000);
      lcd.clear();
    }
    }
    break;
  
  case 4:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Set dealy To:");
      lcd.setCursor(0,1);
      lcd.print("400 hrs");
    
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("New Dealy:");
      lcd.setCursor(0,1);
      lcd.print("400 Hrs.");
      
      digitalWrite(LED,HIGH);
      delay(500);
      digitalWrite(LED,LOW);
      delay(3000);
      lcd.clear();
    }
    }
    break;
  
  case 5:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Set dealy To:");
      lcd.setCursor(0,1);
      lcd.print("500 hrs");
    
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("New Dealy:");
      lcd.setCursor(0,1);
      lcd.print("500 Hrs.");
      
      digitalWrite(LED,HIGH);
      delay(500);
      digitalWrite(LED,LOW);
      delay(3000);
      lcd.clear();
    }
    }
    break;
  
  case 6:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Set dealy To:");
      lcd.setCursor(0,1);
      lcd.print("600 hrs");
    
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("New Dealy:");
      lcd.setCursor(0,1);
      lcd.print("600 Hrs.");
      
      digitalWrite(LED,HIGH);
      delay(500);
      digitalWrite(LED,LOW);
      delay(3000);
      lcd.clear();
    }
    }
    break;
  
  case 7:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Set dealy To:");
      lcd.setCursor(0,1);
      lcd.print("700 hrs");
    
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("New Dealy:");
      lcd.setCursor(0,1);
      lcd.print("700 Hrs.");
      
      digitalWrite(LED,HIGH);
      delay(500);
      digitalWrite(LED,LOW);
      delay(3000);
      lcd.clear();
    }
    }
    break;
  
  case 8:{ //Each case is a menu option
      lcd.setCursor(0,0);
      lcd.print("Back");
      lcd.setCursor(0,1);
      lcd.print("200 hrs");
    
    //Action:
    if (last_select == LOW && current_select == HIGH){ //When down button is pressed
      
      //Exit function code
    }
    }
    break;
    
  }//switch end
  
}




void setup() {
  // put your setup code here, to run once:
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("Menu Test");
  delay(3000);
  lcd.clear();  
}

void loop() {
  

  Menus();
}

So, my first question is: What is going wrong with the submenu? and my second question is: in the submenu, how do I exit back to main menu?

Any assistance will be much appreciated.

Thank You.