Help with very simple menu code

Hey Guys,

Long time user, but new to the forums :slight_smile:

I’m really stumpped on a simple menu im trying to implement.

Basically its a splash screen, you press ENTER and it goes to main 1
Pressing enter again goes to Menu 2
Pressing enter again goes to Menu 3
Anytime you press the cancel button in menu 1-3 it goes to splash screen.

If anyone can help with this code, i’m just not good at looping and button pressing logic :frowning:

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);

int keypad_pin = A1;
int keypad_value = 0;
char btn_push;
==========================================================
char ReadKeypad()
{
  /* Keypad button analog Values
  up       0
  down   178
  enter  305
  cancel 398 
  */
  keypad_value = analogRead(keypad_pin);
  
  if(keypad_value < 10)
    return 'u';
  else if(keypad_value > 10 && keypad_value < 200)
    return 'd';
  else if(keypad_value > 290 && keypad_value < 320)
    return 'e';
  else 
    return 'c';

}
==========================================================
void WaitBtnRelease()
{
    while( analogRead(keypad_pin) < 800){} 
}
==========================================================
void setup()
{
  lcd.begin();
  lcd.backlight();
  Splashscreen();
  delay(1000);
}
==========================================================
void loop() {
  btn_push = ReadKeypad();
  
  if(btn_push == 'e')
    {
     WaitBtnRelease();
     Menu_flow();
    }
    Splashscreen();

delay(100);
}
==========================================================
void Splashscreen()
{
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Press ENTER to Begin");
}
==========================================================
void Menu_flow()
{  
    while(ReadKeypad()!= 'c') //if you press cancel button anytime (even inside nested menus), it will go back to very beginning
    {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Menu 1");
      
      if(btn_push == 'e'){
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Menu 2");
        
        if(btn_push == 'e'){
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("Menu 3");
          
          if(btn_push == 'e'){
            lcd.clear();
            lcd.setCursor(0,0);
            lcd.print("Menu 4");
            delay(5000); // wait 5 seconds then go back to main menu
          }
        }
      }
    }
  }
==========================================================

Have a look at the switch/case construct. You can use the 'enter' key to increment a counter. This counter is then used as the argument for a switch/case. If counter equals '1' you do stuff associated with *state * #1. Next button press counter increments to '2' and you do 'two stuff'. When the counter reaches some arbitrary limit you reset it to some beginning value.

With only a little more code you can decrement the counter and thus move back and forth through your menu.

Let's have a look at Menu_flow()

void Menu_flow()
{
  while (ReadKeypad() != 'c') //if you press cancel button anytime (even inside nested menus), it will go back to very beginning
  {
    ...
    ...

    if (btn_push == 'e')  // 1
    {
      ...
      ...
      if (btn_push == 'e') // 2
      {
        ...
        ...

        if (btn_push == 'e') // 3
        {
          ...
          ...
        }
      }
    }
  }
}

What do you think that the chances are that btn_push is NOT 'e' after you have read the button and the first if evaluated to true? Your code will do what I marked with (1) immediately followed by what I marked with (2) immediately followed by what I marked with (3). The visible effect is that you will basically only see what happens in (3).

dougp: Have a look at the switch/case construct. You can use the 'enter' key to increment a counter. This counter is then used as the argument for a switch/case. If counter equals '1' you do stuff associated with *state * #1. Next button press counter increments to '2' and you do 'two stuff'. When the counter reaches some arbitrary limit you reset it to some beginning value.

With only a little more code you can decrement the counter and thus move back and forth through your menu.

That is a great idea! But i just don't know how the code structure will look when its always readying the button and at the same time going into menus, while also having the while not cancel button is pressed.