Hi
I am using a LCD Keypad Shield with Mega 2560. I am trying to write some code that moves between different menus when Select Button is pressed. While on each menu the Up, Down, Left and Right buttons perform menu specific actions.
The problem I am having is that when the LCD is suppose to show the first menu it shows the second menu too and there is ghosting effect on the LCD screen.
I am using a variable called menu_stage. The value of this variable changes when the Select button is pressed. The Loop function uses switch case statement to call functions depending on the value of menu_stage.
Here is my code so far:
#include <LiquidCrystal.h>
int timer_default_mins = 30;
int menu_stage = 0;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
//values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0);
if (adc_key_in > 1000) return btnNONE;
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 650) return btnLEFT;
if (adc_key_in < 850) return btnSELECT;
return btnNONE;
}
void setup()
{
lcd.begin(16, 2);
}
void loop()
{
switch (menu_stage)
{
case 0:
{
first_menu();
}
case 1:
{
second_menu();
}
}
}
void first_menu()
{
lcd.setCursor(0,0);
lcd.print(" TIMER SETUP ");
lcd.setCursor(0,1);
lcd.print("Mins = ");
lcd.setCursor(7,1);
lcd_key = read_LCD_buttons();
switch (lcd_key)
{
case btnUP:
{
timer_default_mins = timer_default_mins + 1;
lcd.print(timer_default_mins);
lcd.print(" ");
delay(500); //debounce
break;
}
case btnDOWN:
{
timer_default_mins = timer_default_mins - 1;
lcd.print(timer_default_mins);
lcd.print(" ");
delay(500); //debounce
break;
}
case btnSELECT:
{
menu_stage = 1;
delay(500); //debounce
break;
}
case btnNONE:
{
lcd.print(timer_default_mins);
break;
}
}
}
void second_menu()
{
lcd.setCursor(0,0);
lcd.print(" SECOND MENU ");
lcd.setCursor(0,1);
lcd.print(" TEST ");
lcd_key = read_LCD_buttons();
switch (lcd_key)
{
case btnUP:
{
//code for UP button will go here
break;
}
case btnDOWN:
{
//code for DOWN button will go here
break;
}
case btnSELECT:
{
//code for SELECT button will go here
break;
}
case btnNONE:
{
break;
}
}
}
Could someone please let me know where I am going wrong with this?