PLEASE CHECK THIS CODE
the capacitive proximity sensor is for the bin lid when it detects something it will open via servo motor. the ultrasonic sensor is for the trash height detection if it detects something the capacitive sensor will not function if the ultrasonic sensor detects something because this means that the trash bin is full and you cannot throw plastics. when ultrasonic sensor didn't detect something the green led will glow but if it will detect something led red will glow. green led represent the trash bin is not full. red led trash bin is full.
const int trigPin = 7;
const int echoPin = 8;
const int servoPin = 9;
const int greenLedPin = 10;
const int redLedPin = 11;
const int capSensorPin = A0;
// Thresholds
const int capSensorThreshold = 50; // Adjust based on your sensor's readings
const int ultrasonicThreshold = 20; // Adjust based on your bin's height (cm)
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(servoPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
Serial.begin(9600); // For debugging (optional)
//Servo setup
servo.attach(servoPin);
}
void loop() {
long duration, distance;
// Ultrasonic sensor reading
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
//Serial.print("Distance: ");
//Serial.println(distance); // Uncomment for debugging
if (distance < ultrasonicThreshold) {
digitalWrite(redLedPin, HIGH); // Bin is full
digitalWrite(greenLedPin, LOW);
} else {
digitalWrite(redLedPin, LOW); // Bin is not full
digitalWrite(greenLedPin, HIGH);
// Capacitive sensor check (only if bin isn't full)
int capSensorValue = analogRead(capSensorPin);
//Serial.print("Capacitive Sensor Value: ");
//Serial.println(capSensorValue); // Uncomment for debugging
if (capSensorValue > capSensorThreshold) {
openLid();
}
}
delay(100); // Adjust delay as needed
}
void openLid() {
servo.write(90); // Adjust angle as needed for your servo to open the lid
delay(1000); // Keep lid open for 1 second
servo.write(0); // Close the lid
}
Without knowing all the details about the sensors, servos and Arduino and how you have them connected, along with the actual mechanical set-up there is no way of knowing if it is correct or not.
Try it first and let us know if you have any problems.
Is this how you burned out your Uno?
You're using servo.attach(servoPin); but haven't included the Servo library. You need to add #include <Servo.h> at the top. You need to declare a Servo object before using servo.attach().