// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID "TMPL2ZCVRlnsY"
#define BLYNK_TEMPLATE_NAME "EMERGENCY TRAFFIC"
#define BLYNK_AUTH_TOKEN "1WFB3uCb3YJqPg05e73PaM1MXoDQkeh6"
// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
#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[] = "";
BlynkTimer timer;
#define ONE_TRIGGER_PIN 26
#define ECHO_PIN_1 22
#define ECHO_PIN_2 34
#define ECHO_PIN_3 35
#define ECHO_PIN_4 27
unsigned int signal_pins[2][3] = {
{18, 4, 19}, // RED // YELLOW // GREEN *way_1
{32, 33, 25} // RED // YELLOW // GREEN *way_2
} ;
int A = 0; // Variable A
int B = 0; // Variable B
int C = 0; // Variable C
int D = 0; // Variable D
int emergency_call = 0 ;
int current_way =0 ;
unsigned int count = 0 ;
// ultrason function
int detectObject(int triggerPin, int echoPin) {
// Clear the trigger pin
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Send a 10 microsecond pulse to trigger the sensor
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
// Measure the duration of the echo pulse
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance based on the speed of sound
// Distance in centimeters = (duration * speed of sound) / 2
// Speed of sound at sea level = 343 meters/second = 0.0343 centimeters/microsecond
long distance_cm = (duration * 0.0343) / 2;
Serial.print(distance_cm);
Serial.print("\t");
// Check if an object is within 10 centimeters
if (distance_cm < 10) {
return 1; // Object detected
} else {
return 0; // No object detected
}
}
// a function that's open the desire way
int switch_way(unsigned int a){
current_way = a - 1 ;
// previous way
digitalWrite(signal_pins[!current_way][2], LOW); // GREEN OFF
digitalWrite(signal_pins[!current_way][1], HIGH);// YELLOW ON
delay(3000); // wait 1.5s for orange light
digitalWrite(signal_pins[!current_way][1], LOW); // YELLOW OFF
digitalWrite(signal_pins[!current_way][0], HIGH); // RED ON
// next way
digitalWrite(signal_pins[current_way][0], LOW); // RED OFF
digitalWrite(signal_pins[current_way][2], HIGH); // GREEN ON
return current_way ; // last state
}
void alert_light(void){
Serial.print("\nEMERGENCY !!!\n");
digitalWrite(signal_pins[0][2], LOW); // GREEN OFF
digitalWrite(signal_pins[0][1], LOW); // YELLOW OFF
digitalWrite(signal_pins[1][1], LOW); // YELLOW OFF
digitalWrite(signal_pins[1][2], LOW); // GREEN OFF
while(emergency_call){
Blynk.run();
digitalWrite(signal_pins[0][0], HIGH); // RED ON
digitalWrite(signal_pins[1][0], HIGH); // RED ON
delay(200);
digitalWrite(signal_pins[0][0], LOW); // RED ON
digitalWrite(signal_pins[1][0], LOW); // RED ON
delay(200);
}
switch_way(current_way+1);
delay(50);
}
void classic_routine(){
if(current_way == 1){
switch_way(1);
while( !(count > 10) ){
Blynk.run();
if(emergency_call == 1){
return ;
}else{
count ++;
delay(1000);
}
}
count = 0 ;
} else{
switch_way(2);
while( !(count > 10) ){
Blynk.run();
if(emergency_call == 1){
return ;
}else{
count ++;
delay(1000);
}
}
count = 0 ;
}
}
// function that display the stats of ABCD
void ABCD_stat(void){
A = detectObject(ONE_TRIGGER_PIN, ECHO_PIN_1);
delay(50);
B = detectObject(ONE_TRIGGER_PIN, ECHO_PIN_2);
delay(50);
C = detectObject(ONE_TRIGGER_PIN, ECHO_PIN_3);
delay(50);
D = detectObject(ONE_TRIGGER_PIN, ECHO_PIN_4);
delay(50);
Serial.print("\n");
Serial.print("A: ");
Serial.print(A);
Serial.print("\tB: ");
Serial.print(B);
Serial.print("\tC: ");
Serial.print(C);
Serial.print("\tD: ");
Serial.print(D);
Serial.print("\n");
delay(1000);
}
BLYNK_WRITE(V0)
{
int pinValue=param.asInt();
emergency_call = pinValue;
}
void setup() {
pinMode(signal_pins[0][0], OUTPUT);
pinMode(signal_pins[0][1], OUTPUT);
pinMode(signal_pins[0][2], OUTPUT);
pinMode(signal_pins[1][0], OUTPUT);
pinMode(signal_pins[1][1], OUTPUT);
pinMode(signal_pins[1][2], OUTPUT);
Serial.begin(115200);
pinMode(ONE_TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN_1, INPUT);
pinMode(ECHO_PIN_2, INPUT);
pinMode(ECHO_PIN_3, INPUT);
pinMode(ECHO_PIN_4, INPUT);
Blynk.begin(auth, ssid, pass);
}
void loop() {
ABCD_stat();
Blynk.run();
if(emergency_call){
alert_light();
} else{
switch(A*8 + B*4 + C*2 + D){
case 10 :
case 8 :
case 2 :
if(current_way == 1){
switch_way(1); // way 1
}else{}
delay(3000);
while(!((B || D) || (A+B+C+D == 0))){
Blynk.run();
if(emergency_call){
alert_light();
}else{
delay(1000) ;
ABCD_stat();
}}
delay(1500);
break;
case 5 :
case 4 :
case 1 :
if(current_way == 0){
switch_way(2); // way 2
}else{}
delay(3000);
while(!((A || C) || (A+B+C+D == 0))){
Blynk.run();
if(emergency_call){
alert_light();
}else{
delay(1000) ;
ABCD_stat();
}}
delay(3000);
break;
default:
classic_routine(); // classic
break;
}
Serial.print("\nthe currnet way is ");
Serial.print(1+current_way);
Serial.print("\n");
}
}