Below is my code. I'm trying to make sub menus and for some reason the buttons always returns the main menu options.
I figured it'd be as easy as making a function called "readButtons();" and use it to call for the button states and use if statements to act on the button pressed. The issue is that when I select a menu it prints out the menu title and I want to use the readButtons(); there and do other things however when i press the buttons it just puts me back to the main menu. I thought when you did a function call it would go to the function code and execute it and I don't know what i'm doing wrong to keep the button reading to the current function.
this is the code for the main menu
#include <LiquidCrystal.h>
// Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
//States for the menu.
int currentMenuItem = 1;
int lastState = 0;
int state = 0;
int airTemp1 = 45;
void setup() {
//Set the characters and column numbers.
lcd.begin(16, 2);
//Print default title.
clearPrintTitle();
delay(2000);
displayMenu(currentMenuItem);
}
void loop() {
//Call the main menu.
mainMenu();
}
void mainMenu() {
buttonRead();
//If we are out of bounds it rolls over.
if (currentMenuItem < 1) {
currentMenuItem = 4;
displayMenu(currentMenuItem);
}
if (currentMenuItem > 4) {
currentMenuItem = 1;
displayMenu(currentMenuItem);
}
//If we have changed Index, saves re-draws.
if (state != lastState) {
if (state == 1) {
//If Up
currentMenuItem = currentMenuItem - 1;
displayMenu(currentMenuItem);
} else if (state == 2) {
//If Down
currentMenuItem = currentMenuItem + 1;
displayMenu(currentMenuItem);
} else if (state == 5) {
//If Selected
selectMenu(currentMenuItem);
}
//Save the last State to compare.
lastState = state;
}
//Small delay
delay(5);
}
void buttonRead() {
//State = 0 every loop cycle.
state = 0;
//Refresh the button pressed.
int x = analogRead (0);
//Set the Row 0, Col 0 position.
lcd.setCursor(0,0);
//Check analog values from LCD Keypad Shield
if (x < 80) {
//Right
state = 4;
} else if (x < 200) {
//Up
state = 1;
} else if (x < 400){
//Down
state = 2;
} else if (x < 600){
//Left
state = 3;
} else if (x < 800){
//Select
state = 5;
}
}
//Display Menu Option based on Index.
void displayMenu(int x) {
switch (x) {
case 1:
clearPrintTitle();
lcd.print ("-> Temperature");
break;
case 2:
clearPrintTitle();
lcd.print ("-> Menu Option 2");
break;
case 3:
clearPrintTitle();
lcd.print ("-> Menu Option 3");
break;
case 4:
clearPrintTitle();
lcd.print ("-> Menu Option 4");
break;
}
}
//Print a basic header on Row 1.
void clearPrintTitle() {
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" ReaperFarms ");
lcd.setCursor(0,1);
}
//Show the selection on Screen.
void selectMenu(int x) {
switch (x) {
case 1:
clearPrintTitle();
lcd.print ("Temp:");
lcd.setCursor(0,2);
lcd.print("Set -->");
lcd.setCursor(9,2);
lcd.print(airTemp1);
while (int a = 0) {
x = analogRead(0);
if (x < 80) {
//Right
}
else if (x < 200) {
//Up
airTemp1++;
lcd.setCursor(9,2);
lcd.print(airTemp1);
}
else if (x < 400){
//Down
airTemp1--;
lcd.setCursor(9,2);
lcd.print(airTemp1);
}
else if (x < 600){
//Left
}
else if (x < 800){
//Select
mainMenu();
}
}
break;
case 2:
clearPrintTitle();
lcd.print ("Selected Opt 2");
//Call the function that belongs to Option 2
break;
case 3:
clearPrintTitle();
lcd.print ("Selected Opt 3");
//Call the function that belongs to Option 3
break;
case 4:
clearPrintTitle();
lcd.print ("Selected Opt 4");
//Call the function that belongs to Option 4
break;
}
}