Help with very simple menu code

For the discussion of Arduino related topics.

Moderator: phalanx

Post Reply
redrain1345
Posts: 1
Joined: Fri Oct 06, 2017 9:13 pm

Help with very simple menu code

Post by redrain1345 » Fri Oct 06, 2017 9:16 pm

Hey Guys,

Long time user, but new to the forums :)

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 :(

Code: Select all

#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
          }
        }
      }
    }
  }
==========================================================

Valen
Support Volunteer
Posts: 1743
Joined: Wed Dec 22, 2010 2:05 am

Re: Help with very simple menu code

Post by Valen » Fri Nov 03, 2017 11:09 am

What voltage ADC value is measured when no button is pressed? (Or after Enter is released) How are those switches arranged electrically anyway?

Menu_flow won't stay in the while loop if the condition for key-state 'c' applies. Which is basically whatever ADC values are left when none of the three other buttons are pressed. So nothing should be shown.

The WaitBtnRelease function implies that the every-key-unpressed value is higher than 800. Why is that not a possible output-state of ReadKeypad?

For debugging purposes return the value of the ADC, the output of ReadKeypad function, and a statement what it has decided in a certain part of a function to the screen or to serial monitor. That should immediately show where things go wrong.

Post Reply