// Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 7, 6, 5, 4, 3);
//States for the menu.
int currentMenuItem = 0;
int lastState = 0;
void setup() {
//Set the characters and column numbers.
lcd.begin(16, 2);
//Print default title.
clearPrintTitle();
}
void loop() {
//Call the main menu.
mainMenu();
}
void mainMenu() {
//State = 0 every loop cycle.
int 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 < 100) {
//Right
} else if (x < 200) {
//Up
state = 1;
} else if (x < 400){
//Down
state = 2;
} else if (x < 600){
//Left
} else if (x < 800){
//Select
state = 3;
}
//If we are out of bounds on th menu then reset it.
if (currentMenuItem < 0 || currentMenuItem > 2) {
currentMenuItem = 0;
}
//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 Selected
selectMenu(currentMenuItem);
}
//Save the last State to compare.
lastState = state;
}
//Small delay
delay(5);
}
//Display Menu Option based on Index.
void displayMenu(int x) {
switch (x) {
case 1:
clearPrintTitle();
lcd.print ("-> Menu Option 1");
break;
case 2:
clearPrintTitle();
lcd.print ("-> Menu Option 2");
break;
}
}
//Print a basic header on Row 1.
void clearPrintTitle() {
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Version 1.0 ");
lcd.setCursor(0,1);
}
//Show the selection on Screen.
void selectMenu(int x) {
switch (x) {
case 1:
clearPrintTitle();
lcd.print ("Temperature Alarm= 20c");
//Call the function that belongs to Option 1
break;
case 2:
clearPrintTitle();
lcd.print ("Temperature Alarm= 18c");
//Call the function that belongs to Option 2
break;
}
}/quote]
At some point you push the "Up" button which makes the analog value be between 200 and 400, so state = 1.
This causes currentMenuItem to decrement from 0 to -1. You call displayMenu(-1) which should do nothing since there's no case for -1 in its switch.
The next time through mainMenu, currentMenuItem will get reset to 0 at line 48 since it's < 0.
At some point you push the "Select" button which makes the analog value be between 400 and 600, so that state = 2.
At this point you will call selectMenu(currentMenuItem) at line 59 with currentMenuItem = 0. This should also do nothing since there's no case for 0 in its switch.
While I'm not 100% sure about how you intend for this to work, it seems like it couldn't have wanted it to work like this. Maybe this is how you intend for it to be?
#include <LiquidCrystal.h>
// Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 7, 6, 5, 4, 3);
//States for the menu.
enum {menu_none, menu_20c, menu_18c, num_menus};
int currentMenuItem = menu_none;
int lastState = 0;
void setup() {
//Set the characters and column numbers.
lcd.begin(16, 2);
//Print default title.
clearPrintTitle();
}
void loop() {
//Call the main menu.
mainMenu();
}
void mainMenu() {
//State = 0 every loop cycle.
int 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 < 100) {
//Right
} else if (x < 200) {
//Up
state = 1;
} else if (x < 400){
//Down
state = 2;
} else if (x < 600){
//Left
} else if (x < 800){
//Select
state = 3;
}
//If we have changed Index, saves re-draws.
if (state != lastState) {
if (state == 1) {
//If Up ,increment menu, reset when out of bounds.
if(currentMenuItem < num_menus - 1) currentMenuItem++;
else currentMenuItem = menu_none;
displayMenu(currentMenuItem);
} else if (state == 2) {
//If Selected
selectMenu(currentMenuItem);
}
//Save the last State to compare.
lastState = state;
}
//Small delay
delay(5);
}
//Display Menu Option based on Index.
void displayMenu(int x) {
switch (x) {
default:
case menu_none:
break;
case menu_20c:
clearPrintTitle();
lcd.print ("-> Menu Option 1");
break;
case menu_18c:
clearPrintTitle();
lcd.print ("-> Menu Option 2");
break;
}
}
//Print a basic header on Row 1.
void clearPrintTitle() {
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Version 1.0 ");
lcd.setCursor(0,1);
}
//Show the selection on Screen.
void selectMenu(int x) {
switch (x) {
default:
case menu_none:
break;
case menu_20c:
clearPrintTitle();
lcd.print ("Temperature Alarm= 20c");
//Call the function that belongs to Option 1
break;
case menu_18c:
clearPrintTitle();
lcd.print ("Temperature Alarm= 18c");
//Call the function that belongs to Option 2
break;
}
}