How do I change lcd display

Hi, I want to change the lcd display by using bush buttons like when I push the button it will display twinky-wtinky and when push it again it will show something else like (good morning) how do I do this

this is my code below
best regards

Images of code are less than ideal way to present code here. Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.

Hallo
At least you have to debounce the button and an implementation of a small FSM is been highly recommended.

Here is how I would do it. I do not use the LiquidCrystal libraries. There are several by that name and i don’t know which exact one that you are using. I use the hd44780 library for my LCDs. The hd44780 library is available through the library manager.

#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip

const byte  buttonPin = 7;
int buttonPushCounter = 0;   // counter for the number of button presses
bool buttonState = 0;         // current state of the button
bool lastButtonState = 0;     // previous state of the button


// LCD geometry
const int LCD_COLS = 16;
const int LCD_ROWS = 2;

void setup()
{
   Serial.begin(115200);
   pinMode(buttonPin, INPUT_PULLUP);
   lcd.begin(LCD_COLS, LCD_ROWS);
   lcd.print("Hello World");
}

void loop()
{
   static unsigned long lcdTimer = 0;
   unsigned long lcdInterval = 50;
   if (millis() - lcdTimer >= lcdInterval)
   {
      lcdTimer = millis();
      buttonState = digitalRead(buttonPin);
      if (buttonState != lastButtonState)
      {
         if (buttonState == LOW)
         {
            if (buttonPushCounter == 0)
            {
               lcd.clear();
               lcd.print("Tinky Winky");
            }
            else if (buttonPushCounter == 1)
            {
               lcd.clear();
               lcd.print("Good Morning");
            }
            else
            {
               lcd.clear();
               lcd.print("Sun Shines");
            }
            buttonPushCounter ++;
            if (buttonPushCounter > 2)
            {
               buttonPushCounter = 0;
            }
         }
      }
      // save the current state as the last state      
      lastButtonState = buttonState;
   }
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.