#include <Servo.h>
#define TRIG_PIN_TRASH 3 // First ultrasonic sensor for trash level
#define ECHO_PIN_TRASH 2 // First ultrasonic sensor
#define TRIG_PIN_PERSON 4 // Second ultrasonic sensor for person detection
#define ECHO_PIN_PERSON 5 // Second ultrasonic sensor
#define LED_RED 13 // Red LED for full trash
#define LED_GREEN 12 // Green LED for not full trash
#define SERVO_PIN 6 // Servo motor control pin
Servo myServo; // Create a Servo object
bool isLidOpen = false; // Lid state
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN_TRASH, OUTPUT);
pinMode(ECHO_PIN_TRASH, INPUT);
pinMode(TRIG_PIN_PERSON, OUTPUT);
pinMode(ECHO_PIN_PERSON, INPUT);
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
myServo.attach(SERVO_PIN); // Attach the servo
myServo.write(0); // Start with the lid closed
}
float getDistance(int trigPin, int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
return duration * 0.034 / 2; // Calculate distance in cm
}
void loop() {
// Check trash level
float trashDistance = getDistance(TRIG_PIN_TRASH, ECHO_PIN_TRASH);
Serial.print("Trash level distance: ");
Serial.println(trashDistance);
if (trashDistance <= 10) { // Trash is very close (adjusted threshold)
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, LOW);
} else {
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH);
}
// Check for person detection
float personDistance = getDistance(TRIG_PIN_PERSON, ECHO_PIN_PERSON);
Serial.print("Person detection distance: ");
Serial.println(personDistance);
if (personDistance <= 20) { // Person is detected
if (!isLidOpen) {
myServo.write(90); // Open lid
isLidOpen = true;
}
} else {
if (isLidOpen) {
myServo.write(0); // Close lid
isLidOpen = false;
}
}
delay(500); // Adjust delay as needed
}