#define BLYNK_TEMPLATE_ID "TMPL3V6lRdlXC"
#define BLYNK_TEMPLATE_NAME "SMART WASTE MANAGEMENT"
#define BLYNK_AUTH_TOKEN "ZpOOGgEraGUjK-jkDBm2TgheujvaKzw8"
#define TRIGGER_PIN_1 12 // GPIO pin connected to the trigger pin of the first ultrasonic sensor
#define ECHO_PIN_1 14 // GPIO pin connected to the echo pin of the first ultrasonic sensor
#define TRIGGER_PIN_2 16 // GPIO pin connected to the trigger pin of the second ultrasonic sensor
#define ECHO_PIN_2 17 // GPIO pin connected to the echo pin of the second ultrasonic sensor
#define ECHO_PIN_3 25
#define TRIGGER_PIN_3 27
#define LED_PIN_1_ON 2 // GPIO pin connected to the first LED ON pin
#define LED_PIN_1_OFF 0 // GPIO pin connected to the first LED OFF pin
#define LED_PIN_2_ON 4 // GPIO pin connected to the second LED ON pin
#define LED_PIN_2_OFF 5
#define LED_PIN_3_ON 19
#define LED_PIN_3_OFF 18// GPIO pin connected to the second LED OFF pin
#define SERVO_PIN_1 3
#include<Ultrasonic.h>
#include <ESP32Servo.h>
#include<BlynkSimpleEsp32.h>
Ultrasonic ultrasonic1(TRIGGER_PIN_1, ECHO_PIN_1);
Ultrasonic ultrasonic2(TRIGGER_PIN_2, ECHO_PIN_2);
Ultrasonic ultrasonic3(TRIGGER_PIN_3,ECHO_PIN_3);
Servo servo1;
int pos = 0;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
char auth[] = "ZpOOGgEraGUjK-jkDBm2TgheujvaKzw8";
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(LED_PIN_1_ON, OUTPUT); // Set LED 1 ON pin as an output
pinMode(LED_PIN_1_OFF, OUTPUT); // Set LED 1 OFF pin as an output
pinMode(LED_PIN_2_ON, OUTPUT); // Set LED 2 ON pin as an output
pinMode(LED_PIN_2_OFF, OUTPUT);
pinMode(LED_PIN_3_ON, OUTPUT);
pinMode(LED_PIN_3_OFF, OUTPUT); // Set LED 2 OFF pin as an output
servo1.attach(SERVO_PIN_1,1000,5000);
Blynk.begin(auth, ssid, pass);
}
void loop() {
Blynk.run();
long distance1 = measureDistance(TRIGGER_PIN_1, ECHO_PIN_1);
long distance2 = measureDistance(TRIGGER_PIN_2, ECHO_PIN_2);
long distance3 = measureDistance(TRIGGER_PIN_3, ECHO_PIN_3);
long a = 399 - distance1;
long b = 399 - distance2; // Read distance from the second ultrasonic sensor
long c = 399 - distance3;
Serial.print("Bin1 fill level: ");
Serial.print(a);
Serial.print(" cm | Bin2 fill level: ");
Serial.print(b);
Serial.print(" cm |Bin3 fill level: ");
Serial.print(c);
Serial.println(" cm");
Blynk.virtualWrite(V1, a ); // Send distance 1 to Blynk app
Blynk.virtualWrite(V2, b);
Blynk.virtualWrite(V3, c);
if (distance1 < 360) {
Serial.println("Bin1 is full!!. Don't put waste on it ");
digitalWrite(LED_PIN_1_ON, HIGH); // Turn on LED 1
digitalWrite(LED_PIN_1_OFF, LOW); // Turn off LED 1
for(pos = 0;pos <= 90;pos += 1){
servo1.write(pos);
delay(1000);
}
}
else {
digitalWrite(LED_PIN_1_ON, LOW); // Turn off LED 1
digitalWrite(LED_PIN_1_OFF, HIGH); // Turn on LED 1
for(pos = 90;pos >= 0;pos -= 1){
servo1.write(pos);
delay(1000);
}
}
// Control LED 2 and buzzer 2 based on distance from sensor 2
if (distance2 < 50) {
Serial.println("Bin2 is full!!. Don't put waste on it!!");
digitalWrite(LED_PIN_2_ON, HIGH); // Turn on LED 2
digitalWrite(LED_PIN_2_OFF, LOW); // Turn off LED 2
} else {
digitalWrite(LED_PIN_2_ON, LOW); // Turn off LED 2
digitalWrite(LED_PIN_2_OFF, HIGH); // Turn on LED 2
//
}
if (distance3 < 50) {
Serial.println("Bin2 is full!!. Don't put waste on it!!");
digitalWrite(LED_PIN_3_ON, HIGH); // Turn on LED 2
digitalWrite(LED_PIN_3_OFF, LOW); // Turn off LED 2
} else {
digitalWrite(LED_PIN_3_ON, LOW); // Turn off LED 2
digitalWrite(LED_PIN_3_OFF, HIGH); // Turn on LED 2
}
delay(1000); // Delay for stability
}
long measureDistance(int tp, int ep)
{
digitalWrite(tp, LOW);
delay(2);
digitalWrite(tp, HIGH);
delay(10);
digitalWrite(tp, LOW);
long duration = pulseIn(ep, HIGH);
long distance = duration * 0.034 / 2 ;
return distance;
}