LCD Menu and Buttons

Hello everyone,

I am trying to make a simple menu using an LCD and buttons. So far I have been able to print the text on my LCD and put a cursor on the screen; however, as I try to control the cursor using my buttons it seems like the Arduino is not reading the signal from the buttons. I already make sure the buttons are working using a test program.

So far this is my code

#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"

Adafruit_LiquidCrystal lcd(0);

int S = 59; // count seconds 
int Control=1;// count minutes

void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
lcd.begin(20,4);

}

void loop() {
  int pushUp=digitalRead(2);
  int pushDown=digitalRead(3);
  int pushOK=digitalRead(4);
  int pushExit=digitalRead(5);
  int col=0;
  int row=1;
    
  screen1();
  delay (3000);
  lcd.clear();
  screen2();
  
  while(pushOK==HIGH){
    cursor(col,row);
    if(pushUp==LOW){
      col=0;
      row=1;
    } else if(pushDown==LOW){
      col=0;
      row=2;
    }
  }
}
void cursor(int col,int row){
 lcd.setCursor(col,row);
  lcd.noCursor();
  delay (500);
  lcd.cursor();
  delay(500);
}
void screen1 (){
  lcd.setCursor(5,1);
  lcd.print("Welcome!! :D");
}
void screen2(){
  lcd.setCursor(5,0);
  lcd.print("Main Menu");
  lcd.setCursor(1,1);
  lcd.print("Automatic Mode");
  lcd.setCursor(1,2);
  lcd.print("Manual Mode");
}

Any help on how I can solve this problem will be appreciated

If you want to have responsive buttons you can not use the delay() function since it blocks everything while the time ticks away... Have a look at Demonstration code for several things at the same time or check out the BlinkWithoutDelay example to see how to use millis() to time a delay without using delay().

Also, read the sticky post at the top of the forum about how to properly post your code using code tags. It helps people help you.

blh64:
If you want to have responsive buttons you can not use the delay() function since it blocks everything while the time ticks away... Have a look at Demonstration code for several things at the same time or check out the BlinkWithoutDelay example to see how to use millis() to time a delay without using delay().

Also, read the sticky post at the top of the forum about how to properly post your code using code tags. It helps people help you.

Hey, I read the article and corrected the code posting. The main problem here is that not even a simple if statement embedded in a while loop is working for me. I learned how to program in C and I never had a any problem putting an if statement inside a while loop. For example:

#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"

Adafruit_LiquidCrystal lcd(0);

void setup() { // put your setup code here, to run once:

pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(13, OUTPUT);
lcd.begin(20,4);
}

void loop() { // put your main code here, to run repeatedly:

int pushUp=digitalRead(2);
int pushDown=digitalRead(3);
int pushOK=digitalRead(4);
int pushExit=digitalRead(5);

while(1){
  if (pushUp==LOW){
  lcd.print("Hey");
    }else{
      lcd.print("No");
      }
    }
  }

For this code, the button LOW state is never read by the Arduino and it just prints "No" to the LCD. I would like to know why this is not working.

For this code, the button LOW state is never read by the Arduino and it just prints "No" to the LCD.

A while(1) is a never ending loop so anything outside the while loop never gets executed, see the problem!

You could move the digital reads into the while loop, OR since the loop() function is also a never ending loop you can just get rid of the while.

Hutkikz:
A while(1) is a never ending loop so anything outside the while loop never gets executed, see the problem!

You could move the digital reads into the while loop, OR since the loop() function is also a never ending loop you can just get rid of the while.

OMG!!! I moved the digitalRead inside the while and now it is working perfectly!!! Thank you!! After you suggested it, I understood why it was not working. Thanks!!!!

Glad I could help