// 1. Include the necessary libraries
#include <Wire.h> // Essential for I2C communication
#include <LiquidCrystal_I2C.h>
// 2. Configure your LCD
// !!! CHANGE 0x27 to YOUR LCD's I2C address found by the scanner !!!
const int LCD_ADDRESS = 0x27;
const int LCD_COLUMNS = 20; // For a 20-character wide display
const int LCD_ROWS = 4; // For a 4-line display
// Create an LCD object
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLUMNS, LCD_ROWS);
// 3. Define pins for button and LED
const int buttonPin = 2; // Button to toggle status and LED
const int rotLed = 13; // LED to indicate status
// 4. Define String variables for display messages
String Elektronik_JJ = "Elektronik 2025";
String Name = "CHAUDHARY";
String Status1 = "Status: EIN"; // Status when LED is ON
String Status2 = "Status: AUS"; // Status when LED is OFF
// 5. Variables for toggle logic
boolean isLedOn = false; // Tracks the current state of the LED (ON or OFF)
int currentButtonState; // Current reading from the button
int previousButtonState = HIGH; // Previous reading from the button (INPUT_PULLUP starts HIGH)
void setup() {
// 6. Initialize the LCD
lcd.init();
lcd.backlight();
// 7. Set up the button pin with INPUT_PULLUP
pinMode(buttonPin, INPUT_PULLUP);
// 8. Set up the LED pin as an OUTPUT
pinMode(rotLed, OUTPUT);
digitalWrite(rotLed, LOW); // Start with LED OFF
// 9. Display initial messages on the LCD
lcd.setCursor(0, 0); // Column 0, Row 0
lcd.print(Elektronik_JJ);
lcd.setCursor(0, 1); // Column 0, Row 1
lcd.print(Name);
delay(2000); // Show initial messages for 2 seconds
lcd.clear(); // Clear the LCD for status messages
}
void loop() {
// 10. Read the current state of the button
currentButtonState = digitalRead(buttonPin);
// 11. Check for a button press (state change from HIGH to LOW)
if (previousButtonState == HIGH && currentButtonState == LOW) {
// Button was just pressed, toggle the LED state
isLedOn = !isLedOn; // Flip the state (true to false, or false to true)
// Short delay for basic debouncing
delay(50);
}
// 12. Update the previous button state for the next loop iteration
previousButtonState = currentButtonState;
// 13. Update LED and LCD based on the 'isLedOn' state
lcd.setCursor(0, 0); // Set cursor for status message
if (isLedOn) {
digitalWrite(rotLed, HIGH); // Turn LED ON
lcd.print(Status1); // Display "Status: EIN"
} else {
digitalWrite(rotLed, LOW); // Turn LED OFF
lcd.print(Status2); // Display "Status: AUS"
}
// 14. Small delay at the end of the loop
// This delay was 100ms. If the LCD update seems slow or flickery,
// you might adjust this or the debouncing delay.
// For now, let's remove the main loop delay if the debouncing delay is sufficient.
// Consider if you need a delay here or if the 50ms debounce is enough.
// For faster responsiveness, a smaller or no delay here is better if debouncing is handled.
// Let's try with a smaller delay or rely on the debounce delay.
delay(50); // A small delay to make LCD updates readable and prevent too rapid looping.
}