#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define pin numbers for the LEDs
const int green1Pin = 8; // Green light for direction 1
const int yellow1Pin = 9; // Yellow light for direction 1
const int red1Pin = 10; // Red light for direction 1
const int green2Pin = 2; // Green light for direction 2
const int yellow2Pin = 1; // Yellow light for direction 2
const int red2Pin = 6; // Red light for direction 2
// Define timing constants
const unsigned long greenDuration = 10000; // Green light duration in milliseconds
const unsigned long redDuration = 3000; // Yellow light duration in milliseconds
const unsigned long yellowDuration = 2000; // Red light duration in milliseconds
// Initialize LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 columns, 2 rows
// Function declaration
void updateLCD(const char* direction1, const char* direction2);
void setup() {
// Initialize pin modes
pinMode(green1Pin, OUTPUT);
pinMode(yellow1Pin, OUTPUT);
pinMode(red1Pin, OUTPUT);
pinMode(green2Pin, OUTPUT);
pinMode(yellow2Pin, OUTPUT);
pinMode(red2Pin, OUTPUT);
// Initialize LCD
lcd.begin(16, 2); // 16 columns, 2 rows
lcd.backlight();
lcd.print("Traffic Lights");
}
void loop() {
// Direction 1: Red, Direction 2: Green
digitalWrite(red1Pin, HIGH);
digitalWrite(green1Pin, LOW);
digitalWrite(yellow1Pin, LOW);
digitalWrite(red2Pin, LOW);
digitalWrite(green2Pin, HIGH);
digitalWrite(yellow2Pin, LOW);
updateLCD("D1: Stop", "D2: Go");
delay(yellowDuration);
// Direction 1: Yellow, Direction 2: Yellow
digitalWrite(red1Pin, LOW);
digitalWrite(green1Pin, LOW);
digitalWrite(yellow1Pin, HIGH);
digitalWrite(red2Pin, LOW);
digitalWrite(green2Pin, LOW);
digitalWrite(yellow2Pin, HIGH);
updateLCD("D1: Prepare to go", "D2: Prepare to stop");
delay(yellowDuration);
// Direction 1: Green, Direction 2: Red
digitalWrite(red1Pin, LOW);
digitalWrite(green1Pin, HIGH);
digitalWrite(yellow1Pin, LOW);
digitalWrite(red2Pin, HIGH);
digitalWrite(green2Pin, LOW);
digitalWrite(yellow2Pin, LOW);
updateLCD("D1: Go", "D2: Stop");
delay(greenDuration);
// Direction 1: Yellow, Direction 2: Yellow
digitalWrite(red1Pin, LOW);
digitalWrite(green1Pin, LOW);
digitalWrite(yellow1Pin, HIGH);
digitalWrite(red2Pin, LOW);
digitalWrite(green2Pin, LOW);
digitalWrite(yellow2Pin, HIGH);
updateLCD("D1: Prepare to stop", "D2: Prepare to go");
delay(yellowDuration);
}
// Function definition
void updateLCD(const char* direction1, const char* direction2) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(direction1);
lcd.setCursor(0, 1);
lcd.print(direction2);
}