I have a 20X4 LCD wired up and a button with a pull down resistor and I'm trying to programme a simple one-button test menu. The code below works until case 2 where it just repeats and doesn't go forward. Any pointers as to where I'm going wrong would be greatly appreciated!
Oh, I'm sure the code could be somewhat more elegant
Code: Select all
#include <Bounce.h>
#include <LiquidCrystal.h>
#define button 8
Bounce bounced = Bounce(button, 5);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
byte menu = 1;
void buttonPress(){
while (menu == 1){
bounced.update();
byte value = bounced.read();
if (value == HIGH) {
condition1();
}
}
}
void buttonPress1(){
while (menu == 2){
bounced.update();
byte value = bounced.read();
if (value == HIGH) {
condition2();
}
}
}
void buttonPress2(){
while (menu == 3){
bounced.update();
byte value = bounced.read();
if (value == HIGH) {
conditionFinish();
}
}
}
void mainMenu(){
lcd.clear();
lcd.setCursor(6,0);
lcd.print("WELCOME!");
lcd.setCursor(3,2);
lcd.print("Press button to");
lcd.setCursor(4,3);
lcd.print("go forward");
}
void condition1(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Hello World");
}
void condition2(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Goodbye!");
}
void conditionFinish(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Finished!!!");
}
void setup() {
lcd.begin(20,4);
pinMode (button, INPUT);
}
void loop(){
switch (menu) {
case 1:
mainMenu();
buttonPress();
break;
case 2:
condition1();
buttonPress1();
break;
case 3:
condition2();
buttonPress2();
break;
case 4:
conditionFinish();
break;
}
}
Jamie