/*************************************************************
This sketch shows the Traffic Light Signalling System sequence Model
for double lane roads (i.e., 4 way road crossing, North-South Lane
& East-West Lane) using ESP32 Micro-controller
App project setup:
Switch widget (0...1) on Virtual Pin V0
Slider widget (0...60000) on Virtual Pin V1
Slider widget (0...60000) on Virtual Pin V2
*************************************************************/
// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID "TMPLajpZoRpU"
#define BLYNK_DEVICE_NAME "Traffic Lights for 2 lane road"
#define BLYNK_AUTH_TOKEN "BlXeim8sLj6VMHoFa6LUI-cdt31hvGHK"
// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
//Defining the ESP32 pin numbers for each LED connection
#define EWR 21 // Connect Red LED of East-West Lane to pin 21
#define NSR 14 // Connect Red LED of North-South Lane to pin 14
#define EWY 19 // Connect Yellow LED of East-West Lane to pin 19
#define NSY 12 // Connect Yellow LED of North-South Lane to pin 12
#define EWG 18 // Connect Green LED of East-West Lane to pin 18
#define NSG 13 // Connect Green LED of North-South Lane to pin 13
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Declaring the switch and slider variables globally
int redgreentime;
int yellowtime;
int power;
// This function will be called every time Switch Widget
// in Blynk app writes values to the Virtual Pin V0
BLYNK_WRITE(V0)
{
power = param.asInt(); // assigning incoming value from pin V0 to a variable
// process received value
}
// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin V1
BLYNK_WRITE(V1)
{
redgreentime = param.asInt(); // assigning incoming value from pin V1 to a variable
// process received value
}
// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin V2
BLYNK_WRITE(V2)
{
yellowtime = param.asInt(); // assigning incoming value from pin V2 to a variable
// process received value
}
void setup()
{
// Debug console
Serial.begin(115200);
pinMode(NSR, OUTPUT);
pinMode(NSY, OUTPUT);
pinMode(NSG, OUTPUT);
pinMode(EWR, OUTPUT);
pinMode(EWY, OUTPUT);
pinMode(EWG, OUTPUT);
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
}
void loop()
{
Blynk.run();
if(power==1) // LED states for Traffic Lights System Power ON condition
{
{
digitalWrite(NSR, HIGH);
digitalWrite(EWG, HIGH);
delay(redgreentime);
}
{
digitalWrite(EWG, LOW);
digitalWrite(EWY, HIGH);
delay(yellowtime);
}
{
digitalWrite(EWY, LOW);
digitalWrite(EWR, HIGH);
delay(yellowtime);
}
{
digitalWrite(NSR, LOW);
digitalWrite(NSG, HIGH);
delay(redgreentime);
}
{
digitalWrite(NSG, LOW);
digitalWrite(NSY, HIGH);
delay(yellowtime);
}
{
digitalWrite(NSY, LOW);
digitalWrite(NSR, HIGH);
delay(yellowtime);
}
{
digitalWrite(EWR, LOW);
}
}
else // LED states for Traffic Lights System Power OFF condition
{
digitalWrite(NSR, LOW);
digitalWrite(NSY, LOW);
digitalWrite(NSG, LOW);
digitalWrite(EWR, LOW);
digitalWrite(EWY, LOW);
digitalWrite(EWG, LOW);
}
}