#include <Servo.h>
const int trashTrigPin = 3; // Ultrasonic sensor for trash level, trig pin
const int trashEchoPin = 2; // Ultrasonic sensor for trash level, echo pin
const int peopleTrigPin = 12; // Ultrasonic sensor for people detection, trig pin
const int peopleEchoPin = 13; // Ultrasonic sensor for people detection, echo pin
const int servoPin = 11; // Servo motor control pin
const int ledPin = 5; // Red LED notification pin
Servo lidServo;
void setup() {
Serial.begin(9600);
pinMode(trashTrigPin, OUTPUT);
pinMode(trashEchoPin, INPUT);
pinMode(peopleTrigPin, OUTPUT);
pinMode(peopleEchoPin, INPUT);
pinMode(servoPin, OUTPUT);
pinMode(ledPin, OUTPUT);
lidServo.attach(servoPin);
}
void loop() {
int trashLevel = getDistance(trashTrigPin, trashEchoPin);
int peopleDistance = getDistance(peopleTrigPin, peopleEchoPin);
// Blink the LED until trash level reduces below the threshold
if (trashLevel < 7) {
blinkLED();
}
// Open lid when people come close
if (peopleDistance < 12) {
openLid();`
delay(5000);
closeLid();
}
}
int getDistance(int trigPin, int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
return pulseIn(echoPin, HIGH) * 0.034 / 2; // Convert echo time to centimeters
}
void openLid() {
lidServo.write(180);
}
void closeLid() {
lidServo.write(0);
}
void blinkLED() {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}