For the past month and a half i am facing this programming problem with my menus at my arduino. The concept is that the display will show some information (temperature, humidity) and if i press a button (the ''Menu'' button) then the menus that i have built will appear. Once i press the option Exit menu then i go back at the information displayed at the LCD. Here comes the problems that i face and i am sure i am thick enough not to see the solution even if it stares me in the face (you can tell i am ticked off by this situation).
I press the menu button and i enter the menus. The "Settings" text appears, i press the "Enter" button and i am at the submenus. I do all the operations i want and when i press the "Exit menu" then i exit the menus.
When i try to enter the menus again by pressing the "Menu" button the "Settings" text appears and if i press "Enter" then the program returns me back at watching the temperature/ humidity information. If i do the whole process again and i press either the "ESC", "Left" or "Right" buttons then i gain access to the submenus.
The buttons are all placed at an analog pin and i checked if there is a conflict with the values that the pin reads and there is no conflict or voltage leak, there is no option to move them to separate digital pins.
On a side note i would like to know if the atmel studio 6.1 (with the visual micro installed) has the option for the user to follow the code step by step during execution for debugging, i can't find any information about it no matter how hard i looked.
#include <LiquidCrystal.h> //LCD Library
#include <MenuBackend.h> //MenuBackend library - copyright by Alexander Brevig
//---cases for buttons pushed---
#define buttonEnter 1
#define buttonEsc 2
#define buttonRight 3
#define buttonLeft 4
LiquidCrystal lcd(4, 5, 6, 7, 8, 9); //LCD pins.
boolean ButtonPushed = 0;
int read_button = 0;
boolean menus = 0; //flag for the menus
int lcd_brightness=200; //starting amaount of brightness for lcd
int reading = 0; //initial reading for analog buttons
const int backlightPin = 13; //LCD backlight dim pin
//---Menu variables and structure creation---
MenuBackend menu = MenuBackend(menuUsed,menuChanged);
//---initialize menuitems---
MenuItem brightness = MenuItem("Brightness");
MenuItem Increase = MenuItem("Increase");
MenuItem Decrease = MenuItem("Decrease");
MenuItem calibration = MenuItem("Calibration");
MenuItem Sensor_1 = MenuItem("Sensor 1");
MenuItem Temperature = MenuItem("Temperature");
MenuItem Humidity = MenuItem("Humidity");
MenuItem Sensor_2 = MenuItem("Sensor 2 Temp");
MenuItem Dew_enable = MenuItem("Dew Enable");
MenuItem Alarms = MenuItem("Alarms");
MenuItem L_Alarm = MenuItem("Low Alarm");
MenuItem H_Alarm = MenuItem("High Alarm");
MenuItem Exit = MenuItem("Exit");
void setup(){
//---menu configuration---
menu.getRoot().add(brightness);
brightness.addRight(calibration).addRight(Dew_enable).addRight(Alarms).addRight(Exit);
brightness.add(Increase).addRight(Decrease).addRight(Increase);
calibration.add(Sensor_1).addRight(Sensor_2).addRight(Sensor_1);
Sensor_1.add(Temperature).addRight(Humidity).addRight(Temperature);
Alarms.add(L_Alarm).addRight(H_Alarm).addRight(L_Alarm);
menu.toRoot();
lcd.begin(20, 4); //initialize 20 char 4 line lcd
lcd.setCursor(0,0);
analogWrite(backlightPin, lcd_brightness); //set brightness of LCD display
lcd.clear();
lcd.setCursor(0,0);
}
void loop(){
lcd.setCursor(0,0);
lcd.print("Temp:");
lcd.print("C");
lcd.setCursor(13, 0);
lcd.print("RH:");
lcd.print("%");
lcd.setCursor(0,1);
lcd.setCursor(0, 2);
lcd.print("Dew: ");
lcd.setCursor(10,2);
lcd.setCursor(11, 2);
lcd.print("C");
//---condition to enable menus---
read_button = analogRead(0);
if (read_button > 300 && read_button < 340){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Settings");
menus = 1; //flag to enable the menus
}
while (menus == 1){
readButtons(); //I splitted button reading and navigation in two procedures because
navigateMenus(); //in some situations I want to use the button for other purpose (eg. to change some settings)
}
}
//-------------------------------------Functions-------------------------------------------------------
//---menu operations when change event happens---
void menuChanged(MenuChangeEvent changed){
MenuItem newMenuItem = changed.to; //get the destination menu
lcd.setCursor(0,1); //set the start position for lcd printing to the second row
if (newMenuItem.getName() == menu.getRoot()){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Settings");
}
else
if (newMenuItem.getName() == "Brightness"){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Brightness");
}
else if (newMenuItem.getName() == "Increase"){
lcd.print("Increase Brightness");
}
else if (newMenuItem.getName() == "Decrease"){
lcd.print("Decrease Brightness");
}
else if (newMenuItem.getName() == "Calibration"){
lcd.print("Calibration");
}
else if (newMenuItem.getName() == "Sensor 1"){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Sensor 1 ");
}
else if (newMenuItem.getName() == "Temperature"){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Temperature ");
}
else if (newMenuItem.getName() == "Humidity"){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Humidity");
}
else if (newMenuItem.getName() == "Sensor 2 Temp"){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Sensor 2 Temp");
}
else if (newMenuItem.getName() == "Dew Enable"){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Dew Enable");
}
else if (newMenuItem.getName() == "Alarms"){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Alarms ");
}
else if (newMenuItem.getName() == "Low Alarm"){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Low Alarm");
}
else if (newMenuItem.getName() == "High Alarm"){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("High Alarm");
}
else if (newMenuItem.getName() == "Exit"){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Exit Menu");
}
}
//---individual menu operations---
void menuUsed(MenuUseEvent used){
if (used.item.getName() == "Exit"){
reading = 0;
lcd.clear();
menus = 0;
}
}
//---read buttons operations---
void readButtons(){ //read buttons status
boolean ButtonEnterState = LOW; //the state of the Enter button
boolean ButtonEscState = LOW; //the state of the Esc button
boolean ButtonLeftState = LOW; //the state of the Left button
boolean ButtonRightState = LOW; //the state of the Right button
// read the state of the switch into a local variable:
reading = analogRead(0);
delay(500);
//Enter button
// check to see if you just pressed the enter button
// (i.e. the input is at 820 and 900):
if (reading >850 && reading < 900) {
ButtonEnterState = HIGH;
ButtonEscState = LOW;
ButtonRightState = LOW;
ButtonLeftState = LOW;
}
//Esc button
// check to see if you just pressed the Down button
// (i.e. the input is 750 and 800):
if (reading >750 && reading < 800) {
ButtonEscState = HIGH;
ButtonEnterState = LOW;
ButtonRightState = LOW;
ButtonLeftState = LOW;
}
//Right button
// check to see if you just pressed the Down button
// (i.e. the input is 600 and 710)
if (reading > 650 && reading <700) {
ButtonRightState = HIGH;
ButtonEnterState = LOW;
ButtonEscState = LOW;
ButtonLeftState = LOW;
}
//Left button
// check to see if you just pressed the Down button
// (i.e. the input is 300 and 500
if (reading > 350 && reading < 450) {
ButtonLeftState = HIGH;
ButtonEnterState = LOW;
ButtonEscState = LOW;
ButtonRightState = LOW;
}
//records which button has been pressed
if (ButtonEnterState == HIGH){
ButtonPushed = buttonEnter;
}
else if (ButtonEscState == HIGH){
ButtonPushed = buttonEsc;
}
else if (ButtonRightState == HIGH){
ButtonPushed = buttonRight;
}
else if (ButtonLeftState == HIGH){
ButtonPushed = buttonLeft;
}
else{
ButtonPushed = 0;
}
}
//---menu navigation---
void navigateMenus() {
MenuItem currentMenu = menu.getCurrent();
switch (ButtonPushed){
case buttonEnter:
if(!(currentMenu.moveDown())){ //if the current menu has a child and has been pressed enter then menu navigate to item below
menu.use();
}
else{ //otherwise, if menu has no child and has been pressed enter the current menu is used
menu.moveDown();
}
break;
case buttonEsc:
menu.toRoot(); //back to main
break;
case buttonRight: //move right
menu.moveRight();
break;
case buttonLeft: //move left
menu.moveLeft();
break;
}
ButtonPushed = 0; //reset the ButtonPushed variable
}