0% found this document useful (0 votes)
8 views4 pages

Arduino Traffic Light Control Code

This document contains an Arduino program for a traffic light control system using an LCD display. It defines the LED pins for different directions and manages the traffic flow for East-West and North-South directions with specified timing for green, yellow, and red lights. The program includes functions to handle light transitions and display messages on the LCD to indicate traffic conditions.

Uploaded by

hukabar347
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Arduino Traffic Light Control Code

This document contains an Arduino program for a traffic light control system using an LCD display. It defines the LED pins for different directions and manages the traffic flow for East-West and North-South directions with specified timing for green, yellow, and red lights. The program includes functions to handle light transitions and display messages on the LCD to indicate traffic conditions.

Uploaded by

hukabar347
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#include <Wire.

h>
#include <LiquidCrystal_I2C.h>
// Initialize the LCD. COMMON ADDRESSES ARE 0x27 or 0x3F.
// If 0x27 doesn't work, try 0x3F.
LiquidCrystal_I2C lcd(0x27, 16, 2);
//--- LED Pin Definitions ---
// North Direction
#define N_RED 2
#define N_YELLOW 3
#define N_GREEN 4
// South Direction
#define S_RED 5
#define S_YELLOW 6
#define S_GREEN 7
// East Direction
#define E_RED 8
#define E_YELLOW 9
#define E_GREEN 10
// West Direction
#define W_RED 11
#define W_YELLOW 12
#define W_GREEN 13
//--- Timing Configuration (in milliseconds) ---
int greenTime = 8000; // Total time for green (will be split for 2 messages)
int yellowTime = 2000; // Total time for yellow
int allRedTime = 1000; // Safety delay where all lights are red
void setup() {
// Set all LED pins as OUTPUT
for (int i = 2; i <= 13; i++) {
pinMode(i, OUTPUT);
}
// Initialize the LCD
[Link]();
[Link]();
[Link]();
[Link]("Traffic System");
[Link](0, 1);
[Link]("Starting...");
delay(2000);
// Start with all lights red
setAllRed();
[Link]();
[Link]("ALL STOP");
delay(1000);
}
void loop() {
// --- Phase 1: East-West Flow ---
handle_EW_Flow();
// --- Phase 2: North-South Flow ---
handle_NS_Flow();
}
/**
* @brief Manages the East-West traffic flow (straight and turns).
*/
void handle_EW_Flow() {
// Set lights for E-W GREEN, N-S RED
digitalWrite(E_GREEN, HIGH);
digitalWrite(W_GREEN, HIGH);
digitalWrite(N_RED, HIGH);
digitalWrite(S_RED, HIGH);
// Turn off other lights
digitalWrite(E_RED, LOW);
digitalWrite(W_RED, LOW);
digitalWrite(N_GREEN, LOW);
digitalWrite(S_GREEN, LOW);
digitalWrite(E_YELLOW, LOW);
digitalWrite(W_YELLOW, LOW);
digitalWrite(N_YELLOW, LOW);
digitalWrite(S_YELLOW, LOW);
// Display Condition 1 on LCD
displayLCD("Cond 1: GO", "E-W & W-E");
delay(greenTime / 2); // Show for half the green time
// Display Condition 2 on LCD
displayLCD("Cond 2: GO", "E->N & W->S");
delay(greenTime / 2); // Show for the other half
// --- Transition to Yellow ---
digitalWrite(E_GREEN, LOW);
digitalWrite(W_GREEN, LOW);
digitalWrite(E_YELLOW, HIGH);
digitalWrite(W_YELLOW, HIGH);
displayLCD("E-W STOPPING", "");
delay(yellowTime);
// --- Transition to All Red ---
digitalWrite(E_YELLOW, LOW);
digitalWrite(W_YELLOW, LOW);
setAllRed();
displayLCD("ALL STOP", "");
delay(allRedTime);
}
/**
* @brief Manages the North-South traffic flow (straight and turns).
*/
void handle_NS_Flow() {
// Set lights for N-S GREEN, E-W RED
digitalWrite(N_GREEN, HIGH);
digitalWrite(S_GREEN, HIGH);
digitalWrite(E_RED, HIGH);
digitalWrite(W_RED, HIGH);
// Turn off other lights
digitalWrite(N_RED, LOW);
digitalWrite(S_RED, LOW);
digitalWrite(E_GREEN, LOW);
digitalWrite(W_GREEN, LOW);
digitalWrite(E_YELLOW, LOW);
digitalWrite(W_YELLOW, LOW);
digitalWrite(N_YELLOW, LOW);
digitalWrite(S_YELLOW, LOW);
// Display Condition 3 on LCD
displayLCD("Cond 3: GO", "N-S & S-N");
delay(greenTime / 2);
// Display Condition 4 on LCD
displayLCD("Cond 4: GO", "N->W & S->E");
delay(greenTime / 2);
// --- Transition to Yellow ---
digitalWrite(N_GREEN, LOW);
digitalWrite(S_GREEN, LOW);
digitalWrite(N_YELLOW, HIGH);
digitalWrite(S_YELLOW, HIGH);
displayLCD("N-S STOPPING", "");
delay(yellowTime);
// --- Transition to All Red ---
digitalWrite(N_YELLOW, LOW);
digitalWrite(S_YELLOW, LOW);
setAllRed();
displayLCD("ALL STOP", "");
delay(allRedTime);
}
/**
* @brief Helper function to set all lights to RED.
*/
void setAllRed() {
// Turn all Green and Yellow OFF
digitalWrite(N_GREEN, LOW);
digitalWrite(N_YELLOW, LOW);
digitalWrite(S_GREEN, LOW);
digitalWrite(S_YELLOW, LOW);
digitalWrite(E_GREEN, LOW);
digitalWrite(E_YELLOW, LOW);
digitalWrite(W_GREEN, LOW);
digitalWrite(W_YELLOW, LOW);
// Turn all Red ON
digitalWrite(N_RED, HIGH);
digitalWrite(S_RED, HIGH);
digitalWrite(E_RED, HIGH);
digitalWrite(W_RED, HIGH);
}
/**
* @brief Helper function to clear and print two lines to the LCD.
* @param line1 Text for the first line.
* @param line2 Text for the second line.
*/
void displayLCD(String line1, String line2) {
[Link]();
[Link](0, 0);
[Link](line1);
[Link](0, 1);
[Link](line2);
}

You might also like