#define BLYNK_TEMPLATE_ID "TMPL3VZc8r7oy"
#define BLYNK_TEMPLATE_NAME "Traffic light"
#define BLYNK_AUTH_TOKEN "eEypNKZ6HVWFiRnAWErVz2DOgBa17pLx"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
// Your WiFi credentials.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Define pins for LEDs
const int redPinX = 33;
const int yellowPinX = 25;
const int greenPinX = 26;
const int redPinY = 23;
const int yellowPinY = 22;
const int greenPinY = 21;
const int redPinZ = 2;
const int yellowPinZ = 0;
const int greenPinZ = 4;
// Define pins for potentiometers
const int potPinX = 34; // Potentiometer for lane X
const int potPinY = 35; // Potentiometer for lane Y
const int potPinZ = 32; // Potentiometer for lane Z
// Global values of potentiometer
int potValueX = 0;
int potValueY = 0;
int potValueZ = 0;
// Threshold of traffic
const int FULL_TRAFFIC_THRESHOLD = 4095; // Full traffic
const int QUARTER_TRAFFIC_THRESHOLD = 3071; // Moderate traffic
// Traffic available lanes
const int size = 3;
int TrafficLanesArray[size] = {0, 0, 0}; // Available lanes with no traffic
int CurrentLane = 1; // Start with lane X
int NextLane = -1; // -1 represents no available lanes
bool LaneChanged = true; // If true then only it will check for next lane
bool Intimate=true;
void setup() {
Serial.begin(115200);
analogReadResolution(12); // Analog reading bit
// Set potentiometer pin modes
pinMode(potPinX, INPUT);
pinMode(potPinY, INPUT);
pinMode(potPinZ, INPUT);
// Set LED pin modes
pinMode(redPinX, OUTPUT);
pinMode(yellowPinX, OUTPUT);
pinMode(greenPinX, OUTPUT);
pinMode(redPinY, OUTPUT);
pinMode(yellowPinY, OUTPUT);
pinMode(greenPinY, OUTPUT);
pinMode(redPinZ, OUTPUT);
pinMode(yellowPinZ, OUTPUT);
pinMode(greenPinZ, OUTPUT);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
Serial.println("**************************");
Serial.println("* Traffic Management System*");
Serial.println("LANE STATUS");
Serial.println("* Lane X: " + String(potValueX) + ", Lane Y: " + String(potValueY) + ", Lane Z: " + String(potValueZ) + " *");
Serial.println("**************************");
// Initialize traffic lights
controlTrafficLights();
}
// Updating traffic of lanes
void updateLanes() {
potValueX = analogRead(potPinX);
potValueY = analogRead(potPinY);
potValueZ = analogRead(potPinZ);
// Check for lane X
TrafficLanesArray[0] = (potValueX < FULL_TRAFFIC_THRESHOLD) ? 1 : 0;
// Check for lane Y
TrafficLanesArray[1] = (potValueY < FULL_TRAFFIC_THRESHOLD) ? 1 : 0;
// Check for lane Z
TrafficLanesArray[2] = (potValueZ < FULL_TRAFFIC_THRESHOLD) ? 1 : 0;
}
void controlTrafficLights() {
// Turn off all lights first
digitalWrite(redPinX, LOW);
digitalWrite(yellowPinX, LOW);
digitalWrite(greenPinX, LOW);
digitalWrite(redPinY, LOW);
digitalWrite(yellowPinY, LOW);
digitalWrite(greenPinY, LOW);
digitalWrite(redPinZ, LOW);
digitalWrite(yellowPinZ, LOW);
digitalWrite(greenPinZ, LOW);
bool allLanesBlocked = (TrafficLanesArray[0] == 0 && TrafficLanesArray[1] == 0 && TrafficLanesArray[2] == 0);
if (allLanesBlocked) { // All lanes are blocked, set all lights to red
digitalWrite(redPinX, HIGH);
digitalWrite(redPinY, HIGH);
digitalWrite(redPinZ, HIGH);
} else {
// Set the green light for the current lane
switch (CurrentLane) {
case 1:
digitalWrite(greenPinX, HIGH);
Blynk.virtualWrite(V3, "Lane X: 🟢 Lane Y: 🔴 Lane Z: 🔴 ");
if (potValueX >= QUARTER_TRAFFIC_THRESHOLD && potValueX < FULL_TRAFFIC_THRESHOLD) {
digitalWrite(yellowPinX, HIGH);
if (NextLane == 2) {
digitalWrite(yellowPinY, HIGH);
} else if (NextLane == 3) {
digitalWrite(yellowPinZ, HIGH);
}
}
break;
case 2:
digitalWrite(greenPinY, HIGH);
Blynk.virtualWrite(V3, "Lane X: 🔴 Lane Y: 🟢 Lane Z: 🔴 ");
if (potValueY >= QUARTER_TRAFFIC_THRESHOLD && potValueY < FULL_TRAFFIC_THRESHOLD) {
digitalWrite(yellowPinY, HIGH);
if (NextLane == 1) {
digitalWrite(yellowPinX, HIGH);
} else if (NextLane == 3) {
digitalWrite(yellowPinZ, HIGH);
}
}
break;
case 3:
digitalWrite(greenPinZ, HIGH);
Blynk.virtualWrite(V3, "Lane X: 🔴 Lane Y: 🔴 Lane Z: 🟢 ");
if (potValueZ >= QUARTER_TRAFFIC_THRESHOLD && potValueZ < FULL_TRAFFIC_THRESHOLD) {
digitalWrite(yellowPinZ, HIGH);
if (NextLane == 1) {
digitalWrite(yellowPinX, HIGH);
} else if (NextLane == 2) {
digitalWrite(yellowPinY, HIGH);
}
}
break;
}
// Set red lights for other lanes
for (int i = 1; i <= size; i++) {
if (i != CurrentLane) {
switch (i) {
case 1:
digitalWrite(redPinX, HIGH);
break;
case 2:
digitalWrite(redPinY, HIGH);
break;
case 3:
digitalWrite(redPinZ, HIGH);
break;
}
}
}
}
}
void loop() {
Blynk.run();
// Read potentiometer values for each lane
updateLanes();
// Updating values to Blynk
// Traffic counts
Blynk.virtualWrite(V0, potValueX);
Blynk.virtualWrite(V1, potValueY);
Blynk.virtualWrite(V2, potValueZ);
getLane();
// Define a string to represent the next lane
String nextLaneString;
if (TrafficLanesArray[0] == 0 && TrafficLanesArray[1] == 0 && TrafficLanesArray[2] == 0) {
Serial.println("**************************");
Serial.println("* Lane Status *");
Serial.println("* All Lanes Are Blocked *");
Serial.println("**************************");
Blynk.virtualWrite(V3, "🚫 All Lanes Blocked 🚫");
Intimate = false;
LaneChanged = true;
} else {
if (NextLane == -1) {
nextLaneString = "No Lanes";
} else {
switch (NextLane) {
case 1:
nextLaneString = "Lane X";
break;
case 2:
nextLaneString = "Lane Y";
break;
case 3:
nextLaneString = "Lane Z";
break;
}
}
}
// Check if any lanes are available after all lanes were blocked
if (Intimate == false) {
getLane();
if (NextLane != -1) {
CurrentLane = NextLane;
LaneChanged = true;
Intimate = true;
Serial.println("**************************");
Serial.println("LANE STATUS");
Serial.println("* Switching to available lane: " + nextLaneString + " *");
Serial.println("* Lane X: " + String(potValueX) + ", Lane Y: " + String(potValueY) + ", Lane Z: " + String(potValueZ) + " *");
Serial.println("**************************");
Blynk.virtualWrite(V3, "🚫 All Lanes Blocked 🚫");
}
}
// Switching lanes
if (CurrentLane == 1 && potValueX == FULL_TRAFFIC_THRESHOLD) {
Serial.println("**************************");
Serial.println("* Lane X has reached full traffic threshold. Switching to lane: " + nextLaneString + " *");
Serial.println("LANE STATUS");
Serial.println("* Lane X: " + String(potValueX) + ", Lane Y: " + String(potValueY) + ", Lane Z: " + String(potValueZ) + " *");
Serial.println("**************************");
CurrentLane = NextLane;
LaneChanged = true;
} else if (CurrentLane == 2 && potValueY == FULL_TRAFFIC_THRESHOLD) {
Serial.println("**************************");
Serial.println("LANE STATUS");
Serial.println("* Lane Y has reached full traffic threshold. Switching to lane: " + nextLaneString + " *");
Serial.println("* Lane X: " + String(potValueX) + ", Lane Y: " + String(potValueY) + ", Lane Z: " + String(potValueZ) + " *");
Serial.println("**************************");
CurrentLane = NextLane;
LaneChanged = true;
} else if (CurrentLane == 3 && potValueZ == FULL_TRAFFIC_THRESHOLD) {
Serial.println("**************************");
Serial.println("LANE STATUS");
Serial.println("* Lane Z has reached full traffic threshold. Switching to lane: " + nextLaneString + " *");
Serial.println("* Lane X: " + String(potValueX) + ", Lane Y: " + String(potValueY) + ", Lane Z: " + String(potValueZ) + " *");
Serial.println("**************************");
CurrentLane = NextLane;
LaneChanged = true;
} else {
if (CurrentLane == 1 && potValueX >= QUARTER_TRAFFIC_THRESHOLD && potValueX < FULL_TRAFFIC_THRESHOLD) {
Serial.println("**************************");
Serial.println("LANE STATUS");
Serial.println("* Lane X has reached quarter traffic threshold. Next lane: " + nextLaneString + " *");
Serial.println("* Lane X: " + String(potValueX) + ", Lane Y: " + String(potValueY) + ", Lane Z: " + String(potValueZ) + " *");
Serial.println("**************************");
} else if (CurrentLane == 2 && potValueY >= QUARTER_TRAFFIC_THRESHOLD && potValueY < FULL_TRAFFIC_THRESHOLD) {
Serial.println("**************************");
Serial.println("LANE STATUS");
Serial.println("* Lane Y has reached quarter traffic threshold. Next lane: " + nextLaneString + " *");
Serial.println("* Lane X: " + String(potValueX) + ", Lane Y: " + String(potValueY) + ", Lane Z: " + String(potValueZ) + " *");
Serial.println("**************************");
} else if (CurrentLane == 3 && potValueZ >= QUARTER_TRAFFIC_THRESHOLD && potValueZ < FULL_TRAFFIC_THRESHOLD) {
Serial.println("**************************");
Serial.println("LANE STATUS");
Serial.println("* Lane Z has reached quarter traffic threshold. Next lane: " + nextLaneString + " *");
Serial.println("* Lane X: " + String(potValueX) + ", Lane Y: " + String(potValueY) + ", Lane Z: " + String(potValueZ) + " *");
Serial.println("**************************");
}
}
controlTrafficLights();
delay(500);
}
void getLane() {
NextLane = -1; // Assuming as no next lanes
// Check if any lanes are available
for (int i = 0; i < size; i++) {
if (TrafficLanesArray[i] == 1 && (i + 1) != CurrentLane) {
NextLane = i + 1; // Lanes are 1-indexed
break;
}
}
}