#define BLYNK_TEMPLATE_ID "TMPL3E8Dvkbk2"
#define BLYNK_TEMPLATE_NAME "smart traffic"
#define BLYNK_AUTH_TOKEN "NnLmjH3lXWyrxLZGyH_tC8hLDjP3tXEM"
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// WiFi credentials
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Blynk authentication token
char auth[] ="NnLmjH3lXWyrxLZGyH_tC8hLDjP3tXEM";
// Define LED pins for Lane 1
const int greenLED_lane1 = 14;
const int yellowLED_lane1 = 25;
const int redLED_lane1 = 34;
// Define LED pins for Lane 2
const int greenLED_lane2 = 15;
const int yellowLED_lane2 = 16;
const int redLED_lane2 = 18;
// Define potentiometer pin
const int potPin = 0;
// Virtual pin for indication LED in Blynk
#define INDICATION_LED_VIRTUAL_PIN V0
void setup() {
// Initialize the LED pins as outputs for Lane 1
pinMode(greenLED_lane1, OUTPUT);
pinMode(yellowLED_lane1, OUTPUT);
pinMode(redLED_lane1, OUTPUT);
// Initialize the LED pins as outputs for Lane 2
pinMode(greenLED_lane2, OUTPUT);
pinMode(yellowLED_lane2, OUTPUT);
pinMode(redLED_lane2, OUTPUT);
// Initialize serial communication
Serial.begin(9600);
// Attempt to connect to WiFi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Initialize Blynk
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// Initial condition: Lane 1 is green, Lane 2 is red
digitalWrite(greenLED_lane1, HIGH);
digitalWrite(yellowLED_lane1, LOW);
digitalWrite(redLED_lane1, LOW);
digitalWrite(greenLED_lane2, LOW);
digitalWrite(yellowLED_lane2, LOW);
digitalWrite(redLED_lane2, HIGH);
}
void loop() {
// Read the value from the potentiometer
int potValue = analogRead(potPin);
// Map the potentiometer value to LED brightness (0-255)
int brightness = map(potValue, 0, 4095, 0, 255);
// Determine LED status for Lane 1 based on potentiometer value
if (potValue < 1365) { // Less than 1/3 of the potentiometer range
// Green light - No congestion for Lane 1
digitalWrite(greenLED_lane1, HIGH);
digitalWrite(yellowLED_lane1, LOW);
digitalWrite(redLED_lane1, LOW);
// Red light - Heavy congestion for Lane 2
digitalWrite(greenLED_lane2, LOW);
digitalWrite(yellowLED_lane2, LOW);
digitalWrite(redLED_lane2, HIGH);
// Indication LED ON in Blynk when Lane 1 is red
Blynk.virtualWrite(INDICATION_LED_VIRTUAL_PIN, HIGH);
} else if (potValue >= 1365 && potValue < 2730) { // Between 1/3 and 2/3 of the potentiometer range
// Yellow light - Moderate congestion for both lanes
digitalWrite(greenLED_lane1, LOW);
digitalWrite(yellowLED_lane1, HIGH);
digitalWrite(redLED_lane1, LOW);
digitalWrite(greenLED_lane2, LOW);
digitalWrite(yellowLED_lane2, HIGH);
digitalWrite(redLED_lane2, LOW);
// Indication LED OFF in Blynk when Lane 1 is not red
Blynk.virtualWrite(INDICATION_LED_VIRTUAL_PIN, LOW);
} else { // Greater than 2/3 of the potentiometer range
// Red light - Heavy congestion for Lane 1
digitalWrite(greenLED_lane1, LOW);
digitalWrite(yellowLED_lane1, LOW);
digitalWrite(redLED_lane1, HIGH);
// Green light - No congestion for Lane 2
digitalWrite(greenLED_lane2, HIGH);
digitalWrite(yellowLED_lane2, LOW);
digitalWrite(redLED_lane2, LOW);
// Indication LED OFF in Blynk when Lane 1 is not red
Blynk.virtualWrite(INDICATION_LED_VIRTUAL_PIN, LOW);
}
// Run Blynk
Blynk.run();
// Delay for smoother transitions (adjust as needed)
delay(100);
}