Dear Users,
I am new to Arduino and coding and just starting to understand some basic functions.
What I am currently working on is a menu interface displayed on a LCD screen. I have to buttons, one intended for choosing the menu and the other for selecting it.
I have managed to display all the text I need, yet for some reason if I choose a menu with button2, and want to choose an option in the next window, button1 rewrites all the options from the former window. (very confusing, hope the following helps to explain)
Button 1 moves the ‘>’ down and button 2 selects it.
Page 1 Page 2 Page 2 (what i want) Page 2 (what I really get)
‘interface’ menu 0 menu 0 menu 0
menu 0 >red red red
menu 1 green >green >menu1
menu 2 blue blue blue
The problem is that when 1 change to page 2 button 1 doesn’t change function the way I want it to, it just writes over with its initial code. This is probably a very simple programming issue but I have spent hours looking and working on a solution, I am just stuck.
I have attached my code that I have come up with so far. It might be that I am not aligning my codes properly so if someone can give me a heads up on that matter also it would be much appreciated.
#include <LiquidCrystal.h>
#include <Wire.h>LiquidCrystal lcd(0);
int menuFlag = 0;
int menuFlagMax = 2;int Button1 = 4;
int Button1Value = 0;
int lastButton1Value = 0;int Button2 = 5;
int Button2Value = 0;
int lastButton2Value = 0;void setup()
{
pinMode(Button1, INPUT);
pinMode(Button2, INPUT);lcd.begin(20, 4);
lcd.setBacklight(HIGH);
lcd.print(“INTERFACE”);
lcd.setCursor(0,2);
lcd.print(" Menu 1");
lcd.setCursor(0,1);
lcd.print(">Menu 0");
lcd.setCursor(0,3);
lcd.print(" Menu 2");
delay (500);
}void loop ()
{Button1Value = digitalRead(Button1);
if(Button1Value == 0)
{
lastButton1Value = 0;
}
if(Button1Value == 1 && lastButton1Value == 0)
{
lastButton1Value = 1;if (menuFlag == menuFlagMax)
{
menuFlag = 0;
}
else
{
menuFlag = menuFlag + 1;
}if(menuFlag == 0 && Button2Value == 0)
{
lcd.setCursor(0, 3);
lcd.print(" ");lcd.setCursor(0, 1);
lcd.print(" “);
lcd.setCursor(0, 1);
lcd.print(”>Menu 0");
}
if(menuFlag == 1 && Button2Value == 0)
{
lcd.setCursor(0, 1);
lcd.print(" ");lcd.setCursor(0, 2);
lcd.print(" “);
lcd.setCursor(0, 2);
lcd.print(”>Menu 1");
}
if(menuFlag == 2 && Button2Value == 0)
{
lcd.setCursor(0, 2);
lcd.print(" ");lcd.setCursor(0, 3);
lcd.print(" “);
lcd.setCursor(0, 3);
lcd.print(”>Menu 2");
}
}Button2Value = digitalRead(Button2);
if(Button2Value == 0)
{
lastButton2Value = 0;
}
if(Button2Value == 1 && lastButton2Value == 0)
{
lastButton2Value = 1;
}if(menuFlag == 0 && Button2Value == 1)
{lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,0);
lcd.print(“MENU 0”);lcd.setCursor(0,1);
lcd.print(" “);
lcd.setCursor(0,1);
lcd.print(”>RED");lcd.setCursor(0,2);
lcd.print(" “);
lcd.setCursor(0,2);
lcd.print(” GREEN");lcd.setCursor(0,3);
lcd.print(" “);
lcd.setCursor(0,3);
lcd.print(” BLUE");if(Button2Value == 1 && Button1Value == 1)
{lcd.setCursor(0,1);
lcd.print(" “);
lcd.setCursor(0,1);
lcd.print(” RED");lcd.setCursor(0,2);
lcd.print(" “);
lcd.setCursor(0,2);
lcd.print(”>GREEN");lcd.setCursor(0,3);
lcd.print(" “);
lcd.setCursor(0,3);
lcd.print(” BLUE");
}
if(Button2Value == 1 &&Button1Value == 2)
{lcd.setCursor(0,1);
lcd.print(" “);
lcd.setCursor(0,1);
lcd.print(” RED");lcd.setCursor(0,2);
lcd.print(" “);
lcd.setCursor(0,2);
lcd.print(” GREEN");lcd.setCursor(0,3);
lcd.print(" “);
lcd.setCursor(0,3);
lcd.print(”>BLUE");
}}
if(menuFlag == 1 && Button2Value == 1)
{lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,0);
lcd.print(“MENU 1”);lcd.setCursor(0,1);
lcd.print(" “);
lcd.setCursor(0,1);
lcd.print(”>RED");lcd.setCursor(0,2);
lcd.print(" “);
lcd.setCursor(0,2);
lcd.print(” GREEN");lcd.setCursor(0,3);
lcd.print(" “);
lcd.setCursor(0,3);
lcd.print(” BLUE");if(Button1Value == 1)
{lcd.setCursor(0,1);
lcd.print(" “);
lcd.setCursor(0,1);
lcd.print(” RED");lcd.setCursor(0,2);
lcd.print(" “);
lcd.setCursor(0,2);
lcd.print(”>GREEN");lcd.setCursor(0,3);
lcd.print(" “);
lcd.setCursor(0,3);
lcd.print(” BLUE");
}
if(Button1Value == 2)
{lcd.setCursor(0,1);
lcd.print(" “);
lcd.setCursor(0,1);
lcd.print(” RED");lcd.setCursor(0,2);
lcd.print(" “);
lcd.setCursor(0,2);
lcd.print(” GREEN");lcd.setCursor(0,3);
lcd.print(" “);
lcd.setCursor(0,3);
lcd.print(”>BLUE");
}
}
if(menuFlag == 2 && Button2Value == 1)
{lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,0);
lcd.print(“MENU 2”);lcd.setCursor(0,1);
lcd.print(" “);
lcd.setCursor(0,1);
lcd.print(”>RED");lcd.setCursor(0,2);
lcd.print(" “);
lcd.setCursor(0,2);
lcd.print(” GREEN");lcd.setCursor(0,3);
lcd.print(" “);
lcd.setCursor(0,3);
lcd.print(” BLUE");if(Button1Value == 1)
{lcd.setCursor(0,1);
lcd.print(" “);
lcd.setCursor(0,1);
lcd.print(” RED");lcd.setCursor(0,2);
lcd.print(" “);
lcd.setCursor(0,2);
lcd.print(”>GREEN");lcd.setCursor(0,3);
lcd.print(" “);
lcd.setCursor(0,3);
lcd.print(” BLUE");
}
if(Button1Value == 2)
{lcd.setCursor(0,1);
lcd.print(" “);
lcd.setCursor(0,1);
lcd.print(” RED");lcd.setCursor(0,2);
lcd.print(" “);
lcd.setCursor(0,2);
lcd.print(” GREEN");lcd.setCursor(0,3);
lcd.print(" “);
lcd.setCursor(0,3);
lcd.print(”>BLUE");
}
}delay (50);
}
Thank you very much in advance for helping me out!