#include <Servo.h>
Servo myServo;
const int trigPin = 9;
const int echoPin = 8;
const int moisturePin = A0;
// Set threshold based on your moisture sensor calibration (0 – 1023)
const int moistureThreshold = 600;
void setup() {
myServo.attach(10);
myServo.write(90); // Neutral position (center)
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
// Clear trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Trigger ultrasonic sensor pulse
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
int distance = duration * 0.034 / 2; // Distance in cm
// If object detected within 10 cm
if (distance > 0 && distance <= 10) {
delay(1000); // Wait for item to rest on sensor plate
int moistureValue = analogRead(moisturePin);
if (moistureValue < moistureThreshold) {
// Wet Waste detected -> Rotate Servo to Wet Bin side
myServo.write(180);
delay(3000);
} else {
// Dry Waste detected -> Rotate Servo to Dry Bin side
myServo.write(0);
delay(3000);
}
// Return flap to center position
myServo.write(90);
delay(1000);
}
}