Change the text after pressing the button

Hello, I would like to ask for help. I need a "widget" that has 4 buttons and a different text is displayed after pressing each of the buttons. Eg: button.1 - Your answer is YES, button.2 - Your answer is NO, etc. - Is this possible? I use Arduino UNO and this display: https://www.aliexpress.com/item/Blue-Display-IIC-I2C-TWI-SP-I-Serial-Interface2004-20X4-Character-LCD-Module/32816049153.html?spm=2114.10010108.1000014.6.45f21ebfZLiwhz&traffic_analysisId=recommend_3035_null_null_null&scm=1007.13338.80878.000000000000000&pvid=88572b51-5b8a-4671-8dc7-1a2a51d167f5&tpp=1.
Many thanks in advance

Your link is not working. The Arduino can read buttons and depending on which one is pressed it can send different text to a display device. since the link does not work it is not clear how easy it would be to attach that particular display device to the Arduino.

Your link does not work so we don't know which display that you are using.
You posted no code so we don't know which library that you are using.
You posted no schematic so we don't know how the buttons are wired.

That said, a way to do that is:

if(digitalRead(button1) == LOW)
  {
    display.print("Your answer is YES");
  }
else if((digitalRead(button2)== LOW)
  {
    display.print("Your answer is NO");
  }

and so on. Assuming the button is wired to ground with a pullup resistor and the display library has the print method.

I tried to do this:

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);

const int buttonOne = 2;
const int buttonTwo = 3;

int prevSwitchState = 0;
int switchState = 0;
int prevSwitchStateTwo = 0;
int switchStateTwo = 0;

void setup()
{
  pinMode(buttonOne, INPUT);
  pinMode(buttonTwo, INPUT);

  lcd.begin();
  lcd.backlight();
  lcd.setCursor ( 7, 1 );
  lcd.print("Welcome");
  delay (3000);
  lcd.clear();
}

void loop()
{
  lcd.setCursor(0, 0);
  switchState = digitalRead(buttonOne);
  if (prevSwitchState != switchState)
  {
    if (digitalRead(buttonOne) == HIGH)
    {
      lcd.print("YES");
    }
  }

  lcd.setCursor(0, 1);
  switchStateTwo = digitalRead(buttonTwo);
  if (prevSwitchStateTwo != switchStateTwo)
  {
    if (digitalRead(buttonTwo) == HIGH)
    {
      lcd.print("NO");
    }
  }
}

But it does not work :confused: . Can I ask for help? Thank you very much!

What does it do? Were you able to run the example program on the aliexpress page for your LCD?

Answers YES, NO - Appears after "Welcome" without pressing the button. The program from aliexpress works fine. I am sorry for my bad English.

Can I ask for help?

Sure that is what we are here for.

What value pull down resistors are on the push button switch inputs?

At some point in each switch reading code block you need to save the current switch state variables to the previous switch state variables so the previous state is available for the next check.

void loop()
{
  lcd.setCursor(0, 0);
  prevSwitchState = switchState;           // ****** save the switch state to previous ******
  switchState = digitalRead(buttonOne);
  if (prevSwitchState != switchState)      // now you can compare what was last loop() to what is now
  {
    if (digitalRead(buttonOne) == HIGH)
    {
      lcd.print("YES");
    }
  }

How are those buttons wired?

Buttons are connected to pins 2, 3 and GND

Do you have external pullup resistors for the switch inputs? I ask because the internal pull ups are not enabled with

pinMode(pin, INPUT_PULLUP);

.

Buttons are connected according to this diagram

Problem solved :slight_smile: . Here is the function code:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
 
 #define buttonone 2  
 #define buttontwo 3  
 #define buttonthree 4  
 int state = 1;  
 void setup()   
 {  
  Serial.begin(9600);  
  pinMode(buttonone, INPUT_PULLUP);  
  pinMode(buttontwo, INPUT_PULLUP);  
  pinMode(buttonthree, INPUT_PULLUP);
  
  lcd.begin();
  lcd.backlight();
  lcd.setCursor ( 7, 1 );
  lcd.print("WELCOME");
  delay (3000);
  lcd.clear();    
  
  digitalWrite(buttonone, 1);   
  digitalWrite(buttontwo, 1);   
  digitalWrite(buttonthree, 1);   
  delay(200);  
 }  
 void loop()   
 { 
  lcd.setCursor(0, 0); 
  state = digitalRead(buttonone);  
  if (state != 1) {  
    lcd.print("YES");  
  }
  
  lcd.setCursor(0, 1);   
  state = digitalRead(buttontwo);  
  if (state != 1) {  
    lcd.print("NO");  
  }

  lcd.setCursor(0, 2);
  state = digitalRead(buttonthree);  
  if (state != 1) {  
    lcd.print("HELLO");  
  }   
 }

Thank you for your help

No pull-ups meaning your pins are floating when the buttons are not pressed, and will randomly read high or low. Set you pinMode to INPUT_PULLUP to enable the internal pull-up resistors and likely the problem goes away.