#include <Arduino.h>
#include <WiFi.h>
// Pin Definitions
#define GREEN_LED 12
#define YELLOW_LED 13
#define MOTION_SENSOR_1 14
#define MOTION_SENSOR_2 15
// WiFi Credentials (for simulation)
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Traffic Detection Variables
unsigned long lastDetectionTime1 = 0;
unsigned long lastDetectionTime2 = 0;
bool vehicleDetected1 = false;
bool vehicleDetected2 = false;
// Configuration Parameters
const unsigned long DETECTION_TIMEOUT = 5000; // 5 seconds
const unsigned long LIGHT_CYCLE_DURATION = 3000; // 3 seconds
void setupWiFi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi Connected");
Serial.println("IP Address: " + WiFi.localIP().toString());
}
void setupPins() {
// Motion Sensor Pins (Simulated Vehicle Detection)
pinMode(MOTION_SENSOR_1, INPUT);
pinMode(MOTION_SENSOR_2, INPUT);
// Traffic Light LEDs
pinMode(GREEN_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT);
// Initial State
digitalWrite(GREEN_LED, LOW);
digitalWrite(YELLOW_LED, LOW);
}
void checkVehicleDetection() {
// Simulate vehicle detection using motion sensors
bool currentDetection1 = digitalRead(MOTION_SENSOR_1);
bool currentDetection2 = digitalRead(MOTION_SENSOR_2);
// Update detection states
if (currentDetection1) {
lastDetectionTime1 = millis();
vehicleDetected1 = true;
}
if (currentDetection2) {
lastDetectionTime2 = millis();
vehicleDetected2 = true;
}
// Reset detection if timeout occurs
if (millis() - lastDetectionTime1 > DETECTION_TIMEOUT) {
vehicleDetected1 = false;
}
if (millis() - lastDetectionTime2 > DETECTION_TIMEOUT) {
vehicleDetected2 = false;
}
}
void controlTrafficLights() {
// Logic for traffic light control
if (vehicleDetected1 && vehicleDetected2) {
// Vehicles detected in both directions - YELLOW
digitalWrite(GREEN_LED, LOW);
digitalWrite(YELLOW_LED, HIGH);
Serial.println("YELLOW ALERT: Vehicles in both directions");
}
else if (vehicleDetected1 || vehicleDetected2) {
// Vehicle detected in one direction - GREEN
digitalWrite(GREEN_LED, HIGH);
digitalWrite(YELLOW_LED, LOW);
Serial.println("GREEN: Single direction traffic");
}
else {
// No vehicles - Both lights OFF
digitalWrite(GREEN_LED, LOW);
digitalWrite(YELLOW_LED, LOW);
Serial.println("CLEAR: No vehicles detected");
}
}
void debugPrintStatus() {
Serial.println("--------------------");
Serial.print("Direction 1 Vehicle: ");
Serial.println(vehicleDetected1 ? "DETECTED" : "CLEAR");
Serial.print("Direction 2 Vehicle: ");
Serial.println(vehicleDetected2 ? "DETECTED" : "CLEAR");
Serial.println("--------------------");
}
void setup() {
Serial.begin(115200);
// Initialize components
setupPins();
setupWiFi(); // Optional for network features
Serial.println("Traffic Mirror Control System Initialized");
}
void loop() {
// Core system logic
checkVehicleDetection();
controlTrafficLights();
debugPrintStatus();
// Delay to prevent rapid cycling
delay(1000);
}
// Wokwi Simulation Hint:
// Use digital pins 14 and 15 as simulated motion sensors
// Toggle these pins to simulate vehicle detection