#include <LiquidCrystal.h>
// Define pins for ultrasonic sensors
#define TRIG_PIN_1 9
#define ECHO_PIN_1 10
#define TRIG_PIN_2 11
#define ECHO_PIN_2 12
// Define pins for the LEDs
#define GREEN_LED_1 3
#define RED_LED_1 4
#define GREEN_LED_2 5
#define RED_LED_2 6
// Define the LCD pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
// Variables to store distance
long duration1, duration2;
int distance1, distance2;
// Threshold distance to consider a vehicle detected
const int thresholdDistance = 20;
void setup() {
// Initialize serial communication
Serial.begin(500);
Serial.println("Smart Traffic Diversion System");
// Initialize ultrasonic sensor pins
pinMode(TRIG_PIN_1, OUTPUT);
pinMode(ECHO_PIN_1, INPUT);
pinMode(TRIG_PIN_2, OUTPUT);
pinMode(ECHO_PIN_2, INPUT);
// Initialize LED pins
pinMode(GREEN_LED_1, OUTPUT);
pinMode(RED_LED_1, OUTPUT);
pinMode(GREEN_LED_2, OUTPUT);
pinMode(RED_LED_2, OUTPUT);
// Initialize LCD
lcd.begin(16, 2);
lcd.print("Traffic System");
}
void loop() {
// Read distance from the first ultrasonic sensor
distance1 = readDistance(TRIG_PIN_1, ECHO_PIN_1);
// Read distance from the second ultrasonic sensor
distance2 = readDistance(TRIG_PIN_2, ECHO_PIN_2);
// Process sensor data and manage traffic lights
manageTraffic(distance1, distance2);
// Provide diversion recommendations on the LCD
displayRecommendations(distance1, distance2);
// Simulate mobile notifications
sendMobileNotifications(distance1, distance2);
// Delay before the next loop
delay(500); // 1 second delay
}
// Function to read distance from ultrasonic sensor
int readDistance(int trigPin, int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
int distance = duration * 0.034 / 2;
return distance;
}
// Function to manage traffic lights based on sensor data
void manageTraffic(int distance1, int distance2) {
if (distance1 < thresholdDistance) {
digitalWrite(GREEN_LED_1, LOW);
digitalWrite(RED_LED_1, HIGH);
Serial.println("Vehicle detected on Route A, RED LED ON");
} else {
digitalWrite(GREEN_LED_1, HIGH);
digitalWrite(RED_LED_1, LOW);
Serial.println("No vehicle on Route A, GREEN LED ON");
}
if (distance2 < thresholdDistance) {
digitalWrite(GREEN_LED_2, LOW);
digitalWrite(RED_LED_2, HIGH);
Serial.println("Vehicle detected on Route B, RED LED ON");
} else {
digitalWrite(GREEN_LED_2, HIGH);
digitalWrite(RED_LED_2, LOW);
Serial.println("No vehicle on Route B, GREEN LED ON");
}
}
// Function to display recommendations on the LCD
void displayRecommendations(int distance1, int distance2) {
lcd.clear();
if (distance1 < thresholdDistance && distance2 < thresholdDistance) {
lcd.setCursor(0,0);
lcd.print("Divert to Route C");
} else if (distance1 < thresholdDistance) {
lcd.print("Route A busy");
} else if (distance2 < thresholdDistance) {
lcd.print("Route B busy");
} else {
lcd.print("Routes A & B clear");
}
}
// Function to simulate mobile notifications
void sendMobileNotifications(int distance1, int distance2) {
if (distance1 < thresholdDistance) {
Serial.println("Notification: Heavy traffic on Route A");
}
if (distance2 < thresholdDistance) {
Serial.println("Notification: Heavy traffic on Route B");
}
if (distance1 >= thresholdDistance && distance2 >= thresholdDistance) {
Serial.println("Notification: Routes A & B clear");
}
}