LCD Keypad Menu

Hi,

I have a 16 x 2 keypad shield and I am trying to have the buttons select a message from left to right. I am just testing so it starts with a string of ones. If you press right, there will be a string of two’s. if you press right again, it goes to a string of three’s. You could also go back by pressing left. Currently, I have an integer called page. When it displays the string of ones, page == 1 same goes with the string of two’s and also the string of three’s.

This is my code:

// include the library code:
#include <LiquidCrystal.h>

int Page = 1;//make the page variable
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.setCursor(0,0);//set the cursor to top left corner
  lcd.print("1111111111");
}

void loop() {
  int x = analogRead(0);
  
  // put the if statements in for the pages
  
  
  if (x < 60 && Page == 1) {
    lcd.clear();
    lcd.print ("22222222");
    Page = 2;
  }
  
  
  
  else if (x < 600 && x > 400 && Page == 2){
    lcd.clear();
    lcd.print("11111111");
    Page = 1;
  }
  
 else if (x < 60 && Page == 2){
    lcd.clear();
    lcd.print("33333333");
    Page = 3;
  }
  
 else if (x <600 && x >400 && Page == 3){
    lcd.clear();
    lcd.print("22222222");
    Page = 2;
  }
}
}

Whenever I try this, instead of going from page == 1 to page == 2 then to page == 3, it goes directly from page == 1 to page == 2. I was wondering why it does this. So my question is, how can I do something like this without the skip?

What happens here?

if (x < 60 && Page == 1) {
    lcd.clear();
    lcd.print ("22222222");
    Page = 2;
  }

. . . 

if (x < 60 && Page == 2){
    lcd.clear();
    lcd.print("33333333");
    Page = 3;
  }

Have you read about:
else if

  int x;// call an integer x and be the input of analog 0
  x = analogRead (0);

There really isn't any reason to declare a local variable and not assign it an initial value, and then, on the very next line, after the useless comment, assign it a value. Make it look like you know what you are doing:

  int x = analogRead (0);

Anyone can see that this declares a variable called x and assigns it the value returned from the analog pin.

The relationship between the pot value and the text to be displayed appears entirely random. The text to be displayed should be a function of JUST the pot value or it should be a function of what is displayed and the change between two pot values. As it is, you're making things entirely too difficult.

Hi,

Thank you LarryD and PaulS for making my code simpler, but the program still jumps from page == 1 to page == 3. Do you have any idea why it does this?

If you analog reading returns a value of less than 60 and page currently holds 1, This section will update it to page=2;

  if (x < 60 && Page == 1) {
    lcd.clear();
    lcd.print ("22222222");
    Page = 2;
  }

But then, just microseconds later, when the loop runs again, This bit will set page = 3

else if (x < 60 && Page == 2){
    lcd.clear();
    lcd.print("33333333");
    Page = 3;
  }

I guess I was too subtle for the OP ;)

LarryD: I guess I was too subtle for the OP ;)

;)

Usually a hammer works better..

...

Hi,

Thanks KenF, I think that is my problem! I am sorry I couldn't reply fast enough but I am glad you told me what was going wrong.