#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define potPin A0 // Potentiometer pin
#define switchPin 2 // Digital pin for the switch
bool switchState = HIGH; // Assuming HIGH when not pressed
bool lastSwitchState = HIGH;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight(); // Turn on the backlight
lcd.clear();
pinMode(switchPin, INPUT_PULLUP); // Enable internal pull-up resistor for the switch
Serial.println("LCD initialized");
}
void loop() {
// Read switch state with debouncing
int reading = digitalRead(switchPin);
if (reading != lastSwitchState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != switchState) {
switchState = reading;
// Clear the LCD before updating
lcd.clear();
// Display switch state on LCD
lcd.setCursor(0, 0);
lcd.print("Switch: ");
lcd.print(switchState == HIGH ? "OFF" : "ON");
// Display potentiometer value on LCD
int potValue = analogRead(potPin);
lcd.setCursor(0, 1);
lcd.print("Pot Value: ");
lcd.print(potValue);
}
}
lastSwitchState = reading;
delay(50);
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4