#include <ESP32Servo.h>
#define PIR 17
#define SERVO 16
#define SERVO_MIN 0
#define SERVO_MAX 90
#define trigp 18
#define echop 5
#define LED 2 // Changed to GPIO 2 for LED pin
Servo myservo; // Changed to myservo for consistency
void setup() {
pinMode(trigp, OUTPUT);
pinMode(echop, INPUT); // Changed to INPUT
pinMode(LED, OUTPUT);
pinMode(PIR, INPUT);
Serial.begin(9600);
myservo.attach(SERVO);
analogReadResolution(10);
}
void loop() {
int x = digitalRead(PIR);
int duration; // Added duration variable declaration
int distance;
digitalWrite(LED, LOW);
digitalWrite(trigp, LOW);
delayMicroseconds(2);
digitalWrite(trigp, HIGH);
delayMicroseconds(10);
digitalWrite(trigp, LOW);
duration = pulseIn(echop, HIGH);
distance = duration * 0.034 / 2;
if (x == HIGH && distance >= 10) { // Changed to x == HIGH to represent logic level
Serial.println("OBJECT DETECTED");
myservo.write(SERVO_MAX);
Serial.println("BIN OPEN");
delay(1000);
} else {
Serial.println("OBJECT NOT DETECTED");
myservo.write(SERVO_MIN);
Serial.print("distance: "); // Corrected the print statements
Serial.print(distance); // Changed to print distance instead of 100-distance
Serial.println(" cm");
if (distance <= 10) {
Serial.println("DUSTBIN is FULL/LID CANNOT BE OPEN");
digitalWrite(LED, HIGH);
}
delay(1000);
}
}