**HELP** changing pages on a 16x2 LCD with a button

My arduino is calculating multiple things ..
for example..
A, B, C, D, E

Now I have a 16x2 CLD connected to the arduino and I want to make some pages where only few of the things will be displayed
Like.

PAGE 1
A C
B D

PAGE 2
A E
B D

PAGE 3
Only E

Now I want to changes these pages one by one with a button..

How do I start this..??

I tried something like this..
Can this be made more easier ..??

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int switchPin = 8;   // momentary switch on 8, other side connected to ground
int Display = 0;

void setup()
{
  lcd.begin(16, 2);
  pinMode(switchPin, INPUT);
  digitalWrite(switchPin, HIGH);      // turn on pullup resistor
}

void loop()
{
  if (digitalRead(switchPin) == LOW){  
    delay(500);                        // delay to debounce switch

    Display = Display + 1;
    if(Display > 3){
      lcd.clear();
      Display = 1;
    }
    
    switch (Display) {
      case 1: {
        lcd.setCursor(0, 0);
        lcd.print("joy");
        
        break;
      }
        
      case 2: {
        lcd.clear();
        lcd.setCursor(1, 0);
        lcd.print("raha");
        break;
      }
        
       case 3: {
         lcd.clear();
         lcd.setCursor(1, 1);
         lcd.print("hkfl");
         break;
       }
  }
}
}

Yes you can. My question for you: are the A-E going to dynamically change as you switch screens or are they static once you start displaying them.

If they are static, use my phi_prompt library's scrollable long message and do something like:

simple_long_msg("A B \nC D \nE");

This will display all 5 things in a scrollable long message with a scroll bar on the right. Using up and down buttons you can scroll through them and press enter to dismiss the long message. Let me know if that is what you want. As I said, this can't handle dynamic updates as you scroll so A-E have to be determined by the time you call the function. Yes, the long message recognizes new line. :wink:

All values A, B, C, D, E are continuously calculated by arduino and so they will change dynamically...

And please help me removing that delay for the button debounce... :frowning: as it will effct the other loops... :stuck_out_tongue:

Joy:
And please help me removing that delay for the button debounce... :frowning: as it will effct the other loops... :stuck_out_tongue:

You don't need a 500 millisecond delay. Most switches are "done" bouncing within 50 to 100ms.

You can debounce a switch with a small capacitor, but you don't say how the circuit is built, so I won't recommend a value. Changing that delay(500) to delay(50) should work. Just remember that if you do that, you have to let go of the switch REALLY FAST or else the program will rapidly scroll through all of the screens :wink:

ok i removed the delay and used a similar program like the one used in the example of "Blink without delay"

But I have kept the debounce rate 250..

But I dont want the thing that when the button is kept pressed the screens will scroll over..

I want it like if the display is showing screen 1, pressing the button shifts display to screen 2 but until and unless the button is released and pressed again there will be no change in the scree...

How do I do this...??

Add a variable which says "the last time I checked, the button was pressed". When that button is "true", don't change the page.

Then check to see if the button has been released and set that new variable to "false". Don't forget to debounced the "button released" test as well :wink:

Joy:
All values A, B, C, D, E are continuously calculated by arduino and so they will change dynamically...

That was what I feared. Then my code won't help you. To make the buttons work the way you describe, you should record the button state in a variable and compare its value with reading to capture NOT the button in the down state, BUT capture the button changing from up to down TRANSITION.

can someone please show that to me with a small example...??

int prevState = HIGH;
int currState;

void loop()
{
  currState = digitalRead(somePin);
  if(currState != prevState)
  {
    // A transition occurred (assuming that HIGH is not pressed and LOW is pressed).
  }
  prevState = currState;
}

I tried your way..

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 10, 9);

int switchPin = 19;   // momentary switch on 8, other side connected to ground
int Display = 0;
int prevstate = HIGH;
int currstate;

void setup()
{
  lcd.begin(16, 2);
  pinMode(switchPin, INPUT);
  digitalWrite(switchPin, HIGH);      // turn on pullup resistor
}

void loop()
{
  currstate = digitalRead(switchPin);
  if (currstate != prevstate){  
                

    Display = Display + 1;
    if(Display > 3){
      lcd.clear();
      Display = 1;
    }
  }
 
    switch (Display) {
      case 1: {
        lcd.setCursor(0, 0);
        lcd.print("joy");
        break;
      }
        
      case 2: {
        lcd.setCursor(5, 0);
        lcd.print("raha");
        break;
      }
        
       case 3: {
         lcd.setCursor(1, 1);
         lcd.print("hkfl");
         break;
       }
  }
   prevstate = currstate;
}

But this does not seems to work properly...
Please make corrections if I am making mistakes somewhere... :frowning:

What pin do you have the switch connected to?

int switchPin = 19;   // momentary switch on 8, other side connected to ground

cyclegadget:
What pin do you have the switch connected to?

int switchPin = 19;   // momentary switch on 8, other side connected to ground

Sorry...
Actually I have the button connected to A5..
So it will be actually

int switchPin = 19;   // momentary switch on A5, other side connected to ground

And how are you connecting the button on the breadboard? Makes a big difference between connecting it the right way and the wrong way. Need diagram or description.

I am connecting button in the right way...

Its isnt that the code is not working with the button...
What is happening is that when I press the button once from Case 1 it changes to Case 2 then when I release the button again it changes from Case 2 to Case 3

  currstate = digitalRead(switchPin);
  if (currstate != prevstate){

Your code responds to a transition in either direction, LOW -> HIGH and HIGH -> LOW, so reacts when you press the switch and when you release it. You only want to respond one or the other. So, try something like this:

  currstate = digitalRead(switchPin);
  if ((currstate != prevstate) && (currstate == HIGH)){

Assuming currstate goes HIGH when the switch is pressed.

Hi
how to, clean display between the case 1 and 2, 2 and 3?
can somebody help me with some tips?

Clean the display ?
How about lcd.clear() - is that clean enough ?

Thanks for the tip, I'm perhaps a newbie, but I know what clear (), is, I've tried it in slightly different locations in the code, without quite get it to work.

This is the modified code I've tried.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);

int switchPin = 13; // momentary switch on 8, other side connected to ground
int Display = 0;
int prevstate = HIGH;
int currstate;

void setup()
{
lcd.begin(16, 2);
pinMode(switchPin, INPUT);
digitalWrite(switchPin, HIGH); // turn on pullup resistor
}

void loop()
{
currstate = digitalRead(switchPin);
if (currstate != prevstate){

Display = Display + 1;
if(Display > 3){
lcd.clear();

Display = 1;
}
}

switch (Display) {
case 1: {
lcd.setCursor(0, 0);
lcd.print("joy");
break;

}

case 2: {
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("raha");
break;

}

case 3: {
lcd.clear();
lcd.setCursor(1, 1);
lcd.print("hkfl");
break;

}
}
prevstate = currstate;
}