#define BLYNK_TEMPLATE_ID "TMPL3usutPXek"
#define BLYNK_TEMPLATE_NAME "smart traffic"
#define BLYNK_AUTH_TOKEN "5XG1WnX6KXlm3IDeYZzlz2Fix5mc_MwD"
#include <Arduino.h>
#include <Wire.h>
#include <NewPing.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
// Define Blynk Auth Token, WiFi SSID, and WiFi Password
const char auth[] = "5XG1WnX6KXlm3IDeYZzlz2Fix5mc_MwD";
const char *ssid = "Wokwi-GUEST";
const char *password = "";
// Define pins for Traffic Lights
const int redLight1 = 15;
const int yellowLight1 = 2;
const int greenLight1 = 4;
const int redLight2 = 14;
const int yellowLight2 = 12;
const int greenLight2 = 13;
const int redLight3 = 5;
const int yellowLight3 = 17;
const int greenLight3 = 16;
const int redLight4 = 18;
const int yellowLight4 = 19;
const int greenLight4 = 21;
// Define pins for Ultrasonic Sensors
const int trigPin1 = 32;
const int echoPin1 = 35;
// Define maximum distance for sensor
#define MAX_DISTANCE 200
NewPing sensor1(trigPin1, echoPin1, MAX_DISTANCE);
unsigned long lastSensorReadTime = 0; // Variable to store the last sensor read time
const unsigned long sensorReadInterval = 500; // Interval between sensor readings (0.5 second)
// Function declarations
void allLightsOff();
void controlTrafficLights(int d1);
void setTrafficLight(int distance, int redPin, int yellowPin, int greenPin);
void setup() {
Serial.begin(115200);
// Initialize Blynk
Blynk.begin(auth, ssid, password);
// Initialize traffic light pins as outputs
pinMode(redLight1, OUTPUT);
pinMode(yellowLight1, OUTPUT);
pinMode(greenLight1, OUTPUT);
pinMode(redLight2, OUTPUT);
pinMode(yellowLight2, OUTPUT);
pinMode(greenLight2, OUTPUT);
pinMode(redLight3, OUTPUT);
pinMode(yellowLight3, OUTPUT);
pinMode(greenLight3, OUTPUT);
pinMode(redLight4, OUTPUT);
pinMode(yellowLight4, OUTPUT);
pinMode(greenLight4, OUTPUT);
// Turn off all lights initially
allLightsOff();
}
void loop() {
Blynk.run();
// Read distance from sensors at intervals
if (millis() - lastSensorReadTime >= sensorReadInterval) {
int Distance1 = sensor1.ping_cm();
// Print sensor values to the Serial Monitor (for debugging)
Serial.print("Distance1: "); Serial.print(distance1); Serial.print(" cm, ");
// Send sensor values to Blynk app
Blynk.virtualWrite(V1, Distance1);
// Implement your logic here based on the sensor values
controlTrafficLights(Distance1);
// Update the last sensor read time
lastSensorReadTime = millis();
}
}
void controlTrafficLights(int d1) {
// Reset all lights
allLightsOff();
// Logic for controlling traffic lights based on sensor distance
setTrafficLight(d1, redLight1, yellowLight1, greenLight1);
setTrafficLight(d2, redLight2, yellowLight2, greenLight2);
setTrafficLight(d3, redLight3, yellowLight3, greenLight3);
setTrafficLight(d4, redLight4, yellowLight4, greenLight4);
}
void setTrafficLight(int distance, int redPin, int yellowPin, int greenPin) {
if (distance > 0 && distance < 20) {
digitalWrite(greenPin, HIGH);
delay(5000); // Keep the green light on for 5 seconds
} else if (distance >= 20 && distance < 40) {
digitalWrite(yellowPin, HIGH);
delay(5000); // Keep the yellow light on for 5 seconds
} else {
digitalWrite(redPin, HIGH);
delay(5000); // Keep the red light on for 5 seconds
}
}
void allLightsOff() {
digitalWrite(redLight1, LOW);
digitalWrite(yellowLight1, LOW);
digitalWrite(greenLight1, LOW);
digitalWrite(redLight2, LOW);
digitalWrite(yellowLight2, LOW);
digitalWrite(greenLight2, LOW);
digitalWrite(redLight3, LOW);
digitalWrite(yellowLight3, LOW);
digitalWrite(greenLight3, LOW);
digitalWrite(redLight4, LOW);
digitalWrite(yellowLight4, LOW);
digitalWrite(greenLight4, LOW);
}