Multiple functions for same button

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!

Welcome to the forum!

Let me tell you what your mistake is: you should totally separate key inputs and menu print-outs. Also, I suspect that once you get your code working, you will attempt to merge your code with some project you intend to control with your menu, making it into a spaghetti monster.

To solve your problem, you should use existing libraries such as my phi_prompt library or libraries by others. If you want to make your own, you have a very long way to go. You need to first write libraries to handle button push correctly. You are NOT handling them correctly. Then you need to write functions that render menus and read buttons from the libraries that handle buttons. Finally the project code can go in, hopefully mixed with menu code in a minimal amount. Your project should work without the menu, which should not be the focus of your project.

Thanks a lot for that fast response!

I'm not sure I understand what you mean by separating key inputs and menu print outs. Can you show me an example?

I'm just trying to learn how to use basic functions to control a screen.

Thank you for the library suggestion, will give it a try!

In your code, you try to sense a button and then decide what to print on the LCD. That is mixing the two together. You will have a hard time adding a new button, replacing some menu content etc.

That makes sense. Alright I will try and work with your library and see if I can get the hang of it!

Thanks a lot for you help, really appreciate it!

This Thread about Planning and Implementing a Program illustrates how to separate things into different functions and, as its name implies, how to think about how a program is organized.

Another thing you need to think about is keeping track of the state your program is in. When you select an option and then want to show a different set of options you need a variable to record the fact that you have changed state from the first set of options to the second set. The demo also includes variable that keep track of the states of things - for example LED on/off.

...R

wow this is perfect and extremely helpful! thank you so much!