Hi everyone! I am trying to carry out a project in which, using an esp32 wroom 32, I do the following: first, while the pushbutton is not activated, it shows "hello world" on the screen (Display LCD I2C 16x2), and second, if the pushbutton is activated (only by pressing it once, not holding it down) to change the text on the display and show "goodbye world" (and maybe in the future add some other actions like turning on a led than only show a text).
The problem I am having is that if you notice here Prueba 1 - Wokwi ESP32, STM32, Arduino Simulator , in the wokwi simulator the program works correctly, however, when I take it to the real thing what happens is that: it shows the first message "hello world" on the display, it loops , it shows "hello world" again but then in the fourth loop it shows itself, without pressing the button, "goodbye world". It is a programming problem since I checked my button.
My theory is that I have an error when I try to make the pushbutton work as a switch. I would appreciate it if you could help me.
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int buttonPin = 18;
bool buttonState = LOW;
bool messageDisplayed = false;
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
lcd.clear();
pinMode(buttonPin, INPUT);
}
void loop() {
delay(200);
// Mostrar mensaje inicial - Show the first text
lcd.setCursor(1, 0);
lcd.print("hello world");
delay(900);
lcd.clear();
// Leer el estado del botón - Pushbuttom state read
int currentButtonState = digitalRead(buttonPin);
// Verificar si el botón fue presionado - Verify if the pushbuttom was activated
if (currentButtonState == HIGH && !messageDisplayed) {
delay(10); // Desdebujado temporal para evitar el rebote del botón - Temporary undrawing to prevent button bounce
lcd.setCursor(2, 0);
lcd.print("goodaby world");
messageDisplayed = true; // Marcar que el mensaje ya fue mostrado - Mark that the message was showed
delay(1000);
lcd.clear();
}
// Reiniciar el estado del botón si se ha liberado - If it´s been liberated, reboot the pushbuttom state
if (currentButtonState == LOW) {
messageDisplayed = false;
}
}