#define BLYNK_TEMPLATE_ID "TMPL3iiBvCoH8"
#define BLYNK_TEMPLATE_NAME "Blynk Smart Dustbins"
#define BLYNK_AUTH_TOKEN "j39sWLDZbLeIl4nmIPb7RcIXxczl1EYr"

#define BLYNK_PRINT Serial //println functions ku use pannalaam for eg serial.println("hello") but ithu wokwi la message print pannum

#include <WiFi.h> //pothumaana functions ah kudukkum for to connect the ESP32 to a WiFi network.
#include <WiFiClient.h> //TCP connections over WiFi. ithu wifi oda subclass
#include <BlynkSimpleEsp32.h> //connect the ESP32 to the Blynk platform.

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST"; //ithu namma connect panna try panra SSID (network name) of the WiFi network ah store pannum.
char pass[] = ""; //ithu Stores the password for the WiFi network.

BlynkTimer timer; //timer function use pannalaam

// just connections ah define panrom. for eg sensor-->board
#define echoPin1 32
#define trigPin1 33
#define echoPin2 25
#define trigPin2 26
#define echoPin3 14
#define trigPin3 12
#define echoPin4 27
#define trigPin4 13
#define echoPin5 4
#define trigPin5 15

long duration1, duration2, duration3, duration4, duration5;
int distance1, distance2, distance3, distance4, distance5;

void ultrasonic()
{
    // Maximum distance for each bin
    int maxDistance = 400;

    // Ultrasonic sensor 1
    digitalWrite(trigPin1, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin1, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin1, LOW);
    duration1 = pulseIn(echoPin1, HIGH);
    distance1 = duration1 * 0.034 / 2;
    int percentage1 = (distance1 * 100) / maxDistance;
    Blynk.virtualWrite(V0, percentage1);

    // Ultrasonic sensor 2
    digitalWrite(trigPin2, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin2, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin2, LOW);
    duration2 = pulseIn(echoPin2, HIGH);
    distance2 = duration2 * 0.034 / 2;
    int percentage2 = (distance2 * 100) / maxDistance;
    Blynk.virtualWrite(V1, percentage2);

    // Ultrasonic sensor 3
    digitalWrite(trigPin3, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin3, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin3, LOW);
    duration3 = pulseIn(echoPin3, HIGH);
    distance3 = duration3 * 0.034 / 2;
    int percentage3 = (distance3 * 100) / maxDistance;
    Blynk.virtualWrite(V2, percentage3);

    // Ultrasonic sensor 4
    digitalWrite(trigPin4, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin4, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin4, LOW);
    duration4 = pulseIn(echoPin4, HIGH);
    distance4 = duration4 * 0.034 / 2;
    int percentage4 = (distance4 * 100) / maxDistance;
    Blynk.virtualWrite(V3, percentage4);

    // Ultrasonic sensor 5
    digitalWrite(trigPin5, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin5, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin5, LOW);
    duration5 = pulseIn(echoPin5, HIGH);
    distance5 = duration5 * 0.034 / 2;
    int percentage5 = (distance5 * 100) / maxDistance;
    Blynk.virtualWrite(V4, percentage5);
}

void setup() //ithu just initializes the system. for eg sets up pin modes, establishes connections,etc...
{
    Serial.begin(9600); //Starts serial communication with the computer (inga number represents "bits per second")
    pinMode(trigPin1, OUTPUT);
    pinMode(echoPin1, INPUT);
    pinMode(trigPin2, OUTPUT);
    pinMode(echoPin2, INPUT);
    pinMode(trigPin3, OUTPUT);
    pinMode(echoPin3, INPUT);
    pinMode(trigPin4, OUTPUT);
    pinMode(echoPin4, INPUT);
    pinMode(trigPin5, OUTPUT);
    pinMode(echoPin5, INPUT);
    Blynk.begin(auth, ssid, pass); //The Blynk.begin function initializes the connection to the Blynk cloud, allowing you to send and receive data from the Blynk app.
    delay(2000);
    timer.setInterval(1000L, ultrasonic);
}

void loop()
{
    Blynk.run(); //Keeps the Blynk connection active and processes Blynk-related tasks.
    timer.run(); //Executes functions at scheduled intervals without blocking other code.
}