#include <Wire.h>
#include <WiFi.h>
#include <LiquidCrystal_I2C.h>
#define redPin 14
#define yellowPin 12
#define greenPin 13
const int sdlPin = 21; // I2C data line
const int sclPin = 22; // I2C clock line
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int redDuration = 4000;
const int yellowDuration = 2000;
const int greenDuration = 4000;
void setup() {
Serial.begin(9600);
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
Wire.begin(sdlPin, sclPin);
lcd.begin(16, 2);
lcd.backlight();
}
void loop() {
// Red Light
digitalWrite(yellowPin, LOW);
digitalWrite(redPin, HIGH);
displayCountdown(redDuration);
// Yellow Light
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, HIGH);
displayCountdown(yellowDuration);
// Green Light
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, HIGH);
displayCountdown(greenDuration);
// Turn off all lights
digitalWrite(greenPin, LOW);
}
void displayCountdown(int duration) {
for (int i = duration / 1000; i > 0; i--) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.print(i);
lcd.print("s");
delay(1000);
}
}