#include <Adafruit BusIO.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* auth = "btoNyR6HPdyAlID_AWIfSv26idxDShQ4";
const int motionPin = 4; // GPIO pin connected to the motion sensor's OUT pin
const int ledPin1 = 2; // GPIO pin for LED 1
const int ledPin2 = 15; // GPIO pin for LED 2
const int ledPin3 = 5; // GPIO pin for LED 3
const unsigned long led1Interval = 20000; // Time interval for LED 1 (in milliseconds)
const unsigned long led2Interval = 3000; // Time interval for LED 2 (in milliseconds)
const unsigned long led3Interval = 20000; // Time interval for LED 3 (in milliseconds)
Adafruit_SSD1306 display(128, 64, &Wire, -1);
int currentLED = 1; // Variable to track the current LED in the sequence
unsigned long previousMillis = 0; // Variable to store the previous time in milliseconds
int motionThreshold = 400; // Initial value for motion sensor threshold
BLYNK_WRITE(V1) { // Blynk slider widget on V1
motionThreshold = param.asInt(); // Update motion threshold value from the Blynk slider
}
void setup() {
Serial.begin(115200);
Blynk.begin(auth, ssid, password);
pinMode(motionPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
digitalWrite(ledPin1, HIGH); // Turn on the first LED initially
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Initialize OLED display
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
}
void loop() {
Blynk.run();
int motionState = digitalRead(motionPin);
unsigned long currentMillis = millis(); // Get the current time in milliseconds
// If motion is detected while the first LED is glowing and motion threshold is increased above 200,
// print specific information on the OLED display
if (currentLED == 1 && motionState == HIGH && motionThreshold > 200) {
display.clearDisplay();
display.setCursor(0, 10);
display.println("Name: ABC");
display.display();
delay(4000);
display.clearDisplay();
display.println("Car Number: *****");
display.display();
delay(4000);
display.clearDisplay();
display.println("Fine: Rs. 1000");
display.display();
delay(4000);
display.clearDisplay();
display.println("Reason: Jumping Red Light");
display.display();
delay(4000);
display.clearDisplay();
}
// Toggle the current LED in the sequence based on the time intervals
if (currentLED == 1 && currentMillis - previousMillis >= led1Interval) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
previousMillis = currentMillis;
currentLED = 2;
} else if (currentLED == 2 && currentMillis - previousMillis >= led2Interval) {
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
previousMillis = currentMillis;
currentLED = 3;
} else if (currentLED == 3 && currentMillis - previousMillis >= led3Interval) {
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin1, HIGH);
previousMillis = currentMillis;
currentLED = 1;
}
// Check if motion sensor value is above the threshold and print motion state on Blynk terminal
if (motionState > motionThreshold) {
Blynk.virtualWrite(V1, "Motion Detected");
} else {
Blynk.virtualWrite(V1, "No Motion");
}
delay(100); // Small delay for stability
}