#include <Servo.h>
const int TRIG_PIN = 9;
const int ECHO_PIN = 10;
const int MOISTURE_PIN = A0;
const int SERVO_PIN = 6;
const int DETECT_DISTANCE = 15; // Distance in cm
const int WET_THRESHOLD = 500; // Adjust after testing
// Most resistive moisture modules give lower readings when wet.
// Change to false if your sensor gives higher readings when wet.
const bool WET_IS_LOW = true;
// Adjust angles according to your sorting flap.
const int CENTER_ANGLE = 90;
const int DRY_ANGLE = 20;
const int WET_ANGLE = 160;
Servo sortingServo;
bool readyForWaste = true;
long getDistance()
{
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
unsigned long duration = pulseIn(ECHO_PIN, HIGH, 30000UL);
if (duration == 0)
{
return -1; // No valid echo
}
return duration / 58;
}
int readMoisture()
{
long total = 0;
for (int i = 0; i < 10; i++)
{
total += analogRead(MOISTURE_PIN);
delay(20);
}
return total / 10;
}
void setup()
{
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
sortingServo.attach(SERVO_PIN);
sortingServo.write(CENTER_ANGLE);
delay(1000);
Serial.println(“Dry/Wet Smart Dustbin Ready”);
}
void loop()
{
long distance = getDistance();
if (readyForWaste &&
distance > 0 &&
distance <= DETECT_DISTANCE)
{
readyForWaste = false;
// Allow waste to settle on the moisture sensing plate.
delay(1000);
int moisture = readMoisture();
Serial.print(“Moisture reading: “);
Serial.println(moisture);
bool isWet;
if (WET_IS_LOW)
{
isWet = moisture < WET_THRESHOLD;
}
else
{
isWet = moisture > WET_THRESHOLD;
}
if (isWet)
{
Serial.println(“WET waste detected”);
sortingServo.write(WET_ANGLE);
}
else
{
Serial.println(“DRY waste detected”);
sortingServo.write(DRY_ANGLE);
}
delay(2000);
sortingServo.write(CENTER_ANGLE);
delay(700);
}
// Rearm only after a valid reading shows the tray is clear.
if (!readyForWaste && distance > DETECT_DISTANCE + 5)
{
readyForWaste = true;
}
delay(100);
}
Leave a Reply