#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define LED pins for traffic lights (Arduino)
#define RED_LED 2
#define YELLOW_LED 3
#define GREEN_LED 4
// IR sensor for vehicle detection
#define IR_SENSOR 7
// Seven-segment display pins (common cathode type)
const int segmentPins[] = {8, 9, 10, 11, 12, 13, A0}; // Seven-segment pins
// Traffic light timings in milliseconds
const long greenLightDuration = 9000; // 9 seconds for green light
const long yellowLightDuration = 3000; // 3 seconds for yellow light
const long redLightDuration = 9000; // 9 seconds for red light
// Initialize the LCD display with I2C address 0x27, 16 columns and 2 rows
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// Initialize the LCD
lcd.init();
lcd.backlight();
// Setup the traffic light LEDs as output
pinMode(RED_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
// Setup the IR sensor as input
pinMode(IR_SENSOR, INPUT);
// Setup seven-segment display pins as output
for (int i = 0; i < 7; i++) {
pinMode(segmentPins[i], OUTPUT);
}
// Initialize all LEDs and seven-segment display off
digitalWrite(RED_LED, LOW);
digitalWrite(YELLOW_LED, LOW);
digitalWrite(GREEN_LED, LOW);
clearSevenSegment();
}
void loop() {
// Green Light - GO
digitalWrite(GREEN_LED, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("GO"); // Display GO message
displayCountdown(greenLightDuration / 1000); // Display countdown
digitalWrite(GREEN_LED, LOW);
// Yellow Light - GET READY
digitalWrite(YELLOW_LED, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("GET READY"); // Display GET READY message
displayCountdown(yellowLightDuration / 1000); // Display countdown
digitalWrite(YELLOW_LED, LOW);
// Red Light - STOP
digitalWrite(RED_LED, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("STOP"); // Display STOP message
displayCountdown(redLightDuration / 1000); // Display countdown
digitalWrite(RED_LED, LOW);
}
// Function to clear the seven-segment display
void clearSevenSegment() {
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], LOW);
}
}
// Function to display the countdown on seven-segment display
void displayCountdown(int seconds) {
for (int i = seconds; i >= 0; i--) { // Display 0 at the end
showNumberOnSevenSegment(i);
delay(1000); // 1-second delay for countdown
}
clearSevenSegment();
}
// Function to show a number on the seven-segment display
void showNumberOnSevenSegment(int num) {
static const byte digits[10][7] = {
{HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW}, // 0
{LOW, HIGH, HIGH, LOW, LOW, LOW, LOW}, // 1
{HIGH, HIGH, LOW, HIGH, HIGH, LOW, HIGH}, // 2
{HIGH, HIGH, HIGH, HIGH, LOW, LOW, HIGH}, // 3
{LOW, HIGH, HIGH, LOW, LOW, HIGH, HIGH}, // 4
{HIGH, LOW, HIGH, HIGH, LOW, HIGH, HIGH}, // 5
{HIGH, LOW, HIGH, HIGH, HIGH, HIGH, HIGH}, // 6
{HIGH, HIGH, HIGH, LOW, LOW, LOW, LOW}, // 7
{HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH}, // 8
{HIGH, HIGH, HIGH, HIGH, LOW, HIGH, HIGH} // 9
};
// Show the digit on the seven-segment display
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], digits[num][i]);
}
}