Hi, I am struggling trying to make a simple LCD Menu. I Have 3 tact switches: Next, Back (previous) and Enter. The code is totally my idea and I am a beginner so it may look very messy and stupid for you. Switching Menu positions works fine for me but I don't know how to make the Enter button trigger function depending on menu position. E.g. You click next button, land on menu position "2. Two", and when you click Enter now it runs "Function2 ();". When the function ends you're back in the menu, press "back" button, then enter --> it runs "Function1 ();"
This is my code without reading enter button state:
#include <LiquidCrystal.h>
//Menu button pins
#define next 13
#define back 3
#define enter 11
// LED pins
#define green 2
#define red 12
// Variable for Menu position
int a=1;
LiquidCrystal lcd(8, 9, 10, 5, 6, 7);
void setup() {
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Menu:");
pinMode(next, INPUT_PULLUP);
pinMode(back, INPUT_PULLUP);
pinMode(enter, INPUT_PULLUP);
pinMode(green, OUTPUT);
pinMode(red, OUTPUT);
}
void loop() {
// Printing Menu position to LCD depending on 'a' variable value
if (a==1) {
lcd.setCursor(0, 1);
lcd.print("1. One ");
};
if (a==2) {
lcd.setCursor(0, 1);
lcd.print("2. Two ");
};
if (a==3) {
lcd.setCursor(0, 1);
lcd.print("3. Three ");
};
if (a==4) {
lcd.setCursor(0, 1);
lcd.print("4. Four ");
};
if (a==5) {
lcd.setCursor(0, 1);
lcd.print("5. Five ");
};
// Changing 'a' value by pressing 'next' or 'back' buttons
while (digitalRead(next) == LOW) {
if (a<5) {
a=a+1;
delay(300);
}
else {
a=1;
delay(300);
};
};
while (digitalRead(back) == LOW) {
if (a>1) {
a=a-1;
delay(300);
}
else {
a=5;
delay(300);
};
};
}
// Functions
void function1 () { // function triggered by pressing Enter when Menu option One is chosen
digitalWrite(red, HIGH);
delay(1000);
digitalWrite(red, LOW);
}
void function2 () { // function triggered by pressing Enter when Menu option Two is chosen
digitalWrite(green, HIGH);
delay(1000);
digitalWrite(green, LOW);
}
I was thinking of adding something like:
while (digitalRead(enter) == LOW) {
if (a==1) {
function1();
}
if (a==2) {
function2();
}
}
But am not sure it is correct. I tried adding this in void loop but it did not work ;/
I would appreciate any help with that