Code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
// IR Sensor pins
int sensorsLane1[] = {2, 3, 4, 5, 6, 7};
int sensorsLane2[] = {8, 9, 10, 11, 12, 13};
int sensorsLane3[] = {22, 23, 24, 25, 26, 27};
int sensorsLane4[] = {28, 29, 30, 31, 32, 33};
// LED pins [R, Y, G]
int lane1LED[] = {34, 35, 36};
int lane2LED[] = {37, 38, 39};
int lane3LED[] = {40, 41, 42};
int lane4LED[] = {43, 44, 45};
int lastGreenLane = -1;
void setup() {
[Link](9600);
[Link]();
[Link]();
[Link](0, 0);
[Link]("Traffic System");
delay(2000);
[Link]();
// Set sensors as input
for (int i = 0; i < 6; i++) {
pinMode(sensorsLane1[i], INPUT);
pinMode(sensorsLane2[i], INPUT);
pinMode(sensorsLane3[i], INPUT);
pinMode(sensorsLane4[i], INPUT);
}
// Set LEDs as output
for (int i = 0; i < 3; i++) {
pinMode(lane1LED[i], OUTPUT);
pinMode(lane2LED[i], OUTPUT);
pinMode(lane3LED[i], OUTPUT);
pinMode(lane4LED[i], OUTPUT);
}
}
void loop() {
int count1 = countCars(sensorsLane1);
int count2 = countCars(sensorsLane2);
int count3 = countCars(sensorsLane3);
int count4 = countCars(sensorsLane4);
[Link]("Lane 1: "); [Link](count1);
[Link](" | Lane 2: "); [Link](count2);
[Link](" | Lane 3: "); [Link](count3);
[Link](" | Lane 4: "); [Link](count4);
// Check if all lanes are empty
if (count1 == 0 && count2 == 0 && count3 == 0 && count4 == 0) {
allGreenMode();
[Link]();
[Link](0, 0);
[Link]("ALL CLEAR");
delay(2000);
return; // Skip the rest of the loop
}
// Find max lane
int maxLane = getMaxLane(count1, count2, count3, count4);
// Avoid same lane twice in a row
if (maxLane == lastGreenLane) {
maxLane = getNextBestLane(count1, count2, count3, count4, lastGreenLane);
}
if (maxLane == -1) maxLane = 1; // Fallback
lastGreenLane = maxLane;
// Determine green time dynamically
int greenTime = getGreenTime(maxLane, count1, count2, count3, count4);
activateLane(maxLane);
// Countdown on LCD
for (int i = greenTime; i >= 0; i--) {
[Link]();
[Link](0, 0);
[Link]("Lane ");
[Link](maxLane);
[Link](" GO");
[Link](0, 1);
[Link]("Time: ");
[Link](i);
[Link]("s");
delay(1000);
}
}
int countCars(int sensors[]) {
int count = 0;
for (int i = 0; i < 6; i++) {
if (digitalRead(sensors[i]) == LOW) count++; // Active LOW
}
return count;
}
int getMaxLane(int c1, int c2, int c3, int c4) {
int maxLane = 1;
int maxCount = c1;
if (c2 > maxCount) { maxLane = 2; maxCount = c2; }
if (c3 > maxCount) { maxLane = 3; maxCount = c3; }
if (c4 > maxCount) { maxLane = 4; maxCount = c4; }
return maxLane;
}
int getNextBestLane(int c1, int c2, int c3, int c4, int exclude) {
int counts[4] = {c1, c2, c3, c4};
int bestLane = -1;
int bestCount = -1;
for (int i = 0; i < 4; i++) {
if ((i+1) != exclude && counts[i] > bestCount) {
bestCount = counts[i];
bestLane = i+1;
}
}
return bestLane;
}
int getGreenTime(int lane, int c1, int c2, int c3, int c4) {
int cars = 0;
if (lane == 1) cars = c1;
if (lane == 2) cars = c2;
if (lane == 3) cars = c3;
if (lane == 4) cars = c4;
return map(cars, 0, 6, 5, 12); // Dynamic timing
}
void activateLane(int lane) {
// All RED first
digitalWrite(lane1LED[0], HIGH);
digitalWrite(lane2LED[0], HIGH);
digitalWrite(lane3LED[0], HIGH);
digitalWrite(lane4LED[0], HIGH);
// Turn all GREEN OFF first
digitalWrite(lane1LED[2], LOW);
digitalWrite(lane2LED[2], LOW);
digitalWrite(lane3LED[2], LOW);
digitalWrite(lane4LED[2], LOW);
// Selected lane GREEN, others RED
if (lane == 1) { digitalWrite(lane1LED[2], HIGH); digitalWrite(lane1LED[0], LOW); }
if (lane == 2) { digitalWrite(lane2LED[2], HIGH); digitalWrite(lane2LED[0], LOW); }
if (lane == 3) { digitalWrite(lane3LED[2], HIGH); digitalWrite(lane3LED[0], LOW); }
if (lane == 4) { digitalWrite(lane4LED[2], HIGH); digitalWrite(lane4LED[0], LOW); }
}
void allGreenMode() {
// All lanes GREEN, all RED off
digitalWrite(lane1LED[0], LOW);
digitalWrite(lane2LED[0], LOW);
digitalWrite(lane3LED[0], LOW);
digitalWrite(lane4LED[0], LOW);
digitalWrite(lane1LED[2], HIGH);
digitalWrite(lane2LED[2], HIGH);
digitalWrite(lane3LED[2], HIGH);
digitalWrite(lane4LED[2], HIGH);
}
Detailed Code Explanation for Chapter 3 (Methodology)
Introduction
This program implements an intelligent traffic control system using an Arduino
microcontroller, IR sensors for vehicle detection, LEDs for traffic lights, and an LCD
display for real-time status updates.
Key Components of the Code
1. Library Inclusions
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
Wire.h enables I²C communication for the LCD.
LiquidCrystal_I2C.h manages the 16x2 LCD via I²C.
2. LCD Initialization
LiquidCrystal_I2C lcd(0x27, 16, 2);
Initializes LCD with address 0x27 and size 16x2.
3. Sensor and LED Pin Setup
IR Sensors: Four arrays represent six sensors for each lane.
LED Arrays: Each lane has three LEDs (Red, Yellow, Green).
4. Setup Function
void setup() {
[Link](9600);
[Link]();
[Link]();
[Link]("Traffic System");
Starts serial communication for debugging.
Initializes the LCD and displays a welcome message.
5. Loop Function
Reads car counts for each lane using:
int count1 = countCars(sensorsLane1);
Displays counts on Serial Monitor for debugging.
If all lanes are empty, it activates allGreenMode().
Otherwise:
o Determines the lane with the highest traffic using getMaxLane().
o Prevents repeating the same lane consecutively with getNextBestLane().
o Calculates dynamic green time using map() based on the number of cars.
o Activates LEDs for the selected lane and displays countdown on LCD.
6. Helper Functions
countCars(): Counts active IR sensors (car presence).
getMaxLane(): Finds lane with maximum vehicles.
getNextBestLane(): Picks next lane if previous lane had green signal.
getGreenTime(): Dynamically maps car count to green light duration (5–12
seconds).
activateLane(): Turns on green for the selected lane and red for others.
allGreenMode(): Turns all green when traffic is zero.
Algorithm Flow
1. Initialize LCD and configure pins.
2. Continuously monitor IR sensors to count cars.
3. Decide which lane gets the green signal based on traffic density.
4. Set traffic lights dynamically and display status on LCD.
5. Repeat the process continuously.
Why This Approach?
Dynamic Timing: Avoids fixed delays, optimizing flow during peak hours.
Fair Lane Distribution: Prevents starvation of any lane.
LCD Feedback: Provides real-time updates for users.