#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// LCD I2C address (default is 0x27, but this can vary)
LiquidCrystal_I2C lcd(0x27, 16, 2); // 16 columns and 2 rows
// Timer interval in milliseconds (1000 ms = 1 second)
unsigned long lastUpdateTime = 0;
unsigned long updateInterval = 2000; // Update every 2 seconds
void setup() {
// Initialize the LCD
lcd.begin(16, 2);
lcd.setBacklight(1); // Turn on the backlight
// Display an initial message
lcd.setCursor(0, 0);
lcd.print("Hello, ESP32!");
lcd.setCursor(0, 1);
lcd.print("Updating...");
}
void loop() {
// Get the current time
unsigned long currentMillis = millis();
// Check if it's time to update the display
if (currentMillis - lastUpdateTime >= updateInterval) {
// Update the last time the display was updated
lastUpdateTime = currentMillis;
// Clear the previous content
lcd.clear();
// Display a new message
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.print(currentMillis / 1000); // Display seconds elapsed
lcd.setCursor(0, 1);
lcd.print("Updated every 2s");
}
}