LCD menu and submenu with push buttons

Hello guys
I'm working on a project where I use an LCD to display menu and submenu with 3 push buttons (up, down and select) the buttons work on the menu so well but they are not functional on submenu (can't scroll up and down)
I hope someone could help me on this because I really need that and thank u.

Did you forget something?

Yeah it must be something
Would u be able to help me if I shared the code with you?

It'll be impossible if you don't.

Can I get ur email so I can share it with you

Certainly not.

Then can u tell me how to share it here
Cuz I'm new and don't know how to do that here

Are you for real?

Start here


#include <LiquidCrystal.h>
#include<Wire.h>
LiquidCrystal  lcd(12, 11, 5, 4, 3, 2);

int page_counter=1 ;       //To move beetwen items of main menu
int subpage_counter=0;    //To move between items of submenu 1
int subpage2_counter=0;   //To move between items of submenu 2
int upButton = 8;                
int downButton = 9;             
int selectButton = 10;          
boolean current_up = LOW;          
boolean last_up=LOW;            
boolean current_sel = LOW;
boolean last_sel = LOW;
boolean last_down = LOW;
boolean current_down = LOW;

byte heart[8] = {
  0b00000,
  0b00000,
  0b01010,
  0b11111,
  0b01110,
  0b00100,
  0b00000,
  0b00000
};

byte arrow[8] = {
  0b00000,
  0b00100,
  0b00110,
  0b11111,
  0b00110,
  0b00100,
  0b00000,
  0b00000
};
void setup() {
Serial.begin(9600);
pinMode(upButton,INPUT_PULLUP);
pinMode(downButton,INPUT_PULLUP);
pinMode(selectButton,INPUT_PULLUP);

lcd.begin(16,2);
lcd.createChar(1, heart);
lcd.createChar(2, arrow);   
}

 //---- 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;
}

void loop() {
current_up = debounce(last_up, upButton);         //Debounce for Up button
current_sel = debounce(last_sel, selectButton);      //Debounce for Select  button
current_down = debounce(last_down, downButton);   //Debounce for Down button

if(subpage_counter==0 && subpage2_counter==0){               
    if (last_down== LOW && current_down == HIGH){ 
      lcd.clear();                            
      if(page_counter <2){              
      page_counter= page_counter +1;   
      }
      else{
      page_counter= 1;                 
      }
}
last_down = current_down;                 //Save down button last state 

    if (last_up== LOW && current_up == HIGH){
      lcd.clear();                       
      if(page_counter >1){              
      page_counter= page_counter -1;    
      }
      else{
      page_counter= 2;                 
      }
  } 
    last_up = current_up;         
}
  switch (page_counter) {  
   case 1: { //Design of page 1
      lcd.setCursor(0,0);
      lcd.write(byte(2));
      lcd.setCursor(1,0);
      lcd.print("heart");
      lcd.setCursor(1,1);
      lcd.print("lung");
  
 if (last_sel== LOW && current_sel == HIGH){  //select button pressed
  subpage_counter=1;

     if(subpage_counter==1){
      page_counter=0;
     lcd.setCursor(0,0);
     lcd.write(byte(2));                           
     lcd.setCursor(1,0);
     lcd.print("disease1");
     lcd.setCursor(1,1);
     lcd.print("disease2"); 
     Serial.println(page_counter);
        }
      
    if (last_down== LOW && current_down == HIGH){ 
      lcd.clear();                            
      if(subpage_counter <2){              
      subpage_counter= subpage_counter +1;   
      }
      else{
      subpage_counter= 1;                 
      }
       Serial.println(subpage_counter);
}   
     if(subpage_counter==2){                               
    lcd.setCursor(1,0);
     lcd.print("disease1");
     lcd.setCursor(0,1);
     lcd.write(byte(2));                           
     lcd.setCursor(1,1);
     lcd.print("disease2"); 
        }
      }
      last_sel=current_sel;                      //Save last state of select button  
 }
   break;  

            
    case 2: { //Design of page 2 
     lcd.setCursor(1,0);
      lcd.print("heart");
      lcd.setCursor(0,1);
      lcd.write(byte(2));
      lcd.setCursor(1,1);
      lcd.print("lung");  
    }
    break; 
  }//switch end
}



Someone help please

Post the sketch in code tags "</>".

Thank you but this is not the problem I have problem with the code can u help me with that?

You just set it to one . . . and then you test to see if it is one?

Do u suggest any solution
Cuz all I want is to pass to the submenu with all the buttons being functional
The select button should pass me to the submenu and eliminate the main menu but if I set the page_counter to 0 the buttons will be disabled.
I couldn't find any solution
And thanks for ur time I really appreciate that

Hello amal18
I did a little code review.
Do not use magic numbers. C++ provides the enum statement to create names for cases easily. The swich/case statement can be used nested.
All in all, take some time, study the IPO model, take a piece of paper and a pencil and make a re-design. There is no other way to untie the knot.
You may design objects for the LCD and for other common things.
Have a nice day and enjoy coding in C++.

I'd start by using the IDE's auto-format tool to make the structure clearer.
Maybe some comments...
Maybe revise your understanding of the values a bool can take.

a few suggestions

  • separate recognizing a button press rom the code using and determining what to do
  • four buttons are commonly used with a menu: up and down buttons to scroll thru a set of menus at some level, select button to select the menu item or sub-menu and a 4th to go up to the next higher menu level (enum { Bnul, Bup, Bdown, Bsel, Bmenu };)
  • use a switch statement to determine what to do for each of the four buttons
  • use a tables and sub-tables to manage you menus, possibly identifying a menu item by type: maintaining some value, variable, or performing some action, function pointer
  • maintain a stack (i.e. array) to track menu levels

...or if you structure your code to match your menu structure, you get your stack for free.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.