#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int switchPin = 7; // Pin connected to the switch
bool lcdState = true; // Current state of the LCD (on or off)
void setup() {
lcd.begin(16, 2); // set up the LCD's number of columns and rows
lcd.print("Hello, World!"); // Print a message to the LCD
pinMode(switchPin, INPUT_PULLUP); // Set the switch pin as input with an internal pull-up resistor
}
void loop() {
if (digitalRead(switchPin) == LOW) { // If the switch is pressed
delay(50); // Debounce delay
if (digitalRead(switchPin) == LOW) { // Confirm the switch is still pressed
lcdState = !lcdState; // Toggle the state of the LCD
if (lcdState) {
lcd.display(); // Turn the display on
} else {
lcd.noDisplay(); // Turn the display off
}
while (digitalRead(switchPin) == LOW); // Wait for the switch to be released
}
}
}