#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Wi-Fi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Relay and button pins
const int relayPin = 4;
const int buttonPin = 16;
// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Timer variables
unsigned long startTime;
bool timerActive = false;
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize relay and button
pinMode(relayPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.clear();
}
void loop() {
// Check if button is pressed
if (digitalRead(buttonPin) == LOW && !timerActive) {
// Activate relay and start timer
digitalWrite(relayPin, HIGH);
startTime = millis();
timerActive = true;
// Display message on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Garage Light On");
}
// Update timer and LCD
if (timerActive) {
unsigned long elapsedTime = millis() - startTime;
unsigned long remainingTime = 300000 - elapsedTime; // 5 minutes in milliseconds
if (remainingTime > 0) {
// Display countdown on LCD
lcd.setCursor(0, 1);
lcd.print("Time left: ");
lcd.print(remainingTime / 1000);
lcd.print("s");
} else {
// Turn off relay and stop timer
digitalWrite(relayPin, LOW);
timerActive = false;
// Clear LCD message
lcd.clear();
}
}
}