Code for trash bin

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
}

What part are you having problems with?
What happens when you try it?

I didn't try it yet but I just want to check my code it it there is any error.

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?

If it compiles, then upload it.

Just have the servo on the bench, not mechanically connected to anything, and try it.

Be adventurous...

Tom.... :smiley: :+1: :coffee: :australia:

If you don't #include your servo library, I'm pretty sure it doesn't run.

look this over

  • separate functions
  • more debug statements
  • report sensor when changed
  • added testDistance
#include <Servo.h>

Servo servo;

const int TrigPin       = 7;
const int EchoPin       = 8;
const int ServoPin      = 9;
const int GreenLedPin   = 10;
const int RedLedPin     = 11;
const int CapSensorPin = A0;

enum { Off = LOW, On = HIGH };

// Thresholds
const int CapSensorThreshold  = 50; // Adjust based on your sensor's readings
const int TrashDepth          = 20; // Adjust based on your bin's height (cm)

// -----------------------------------------------------------------------------
// Capacitive sensor check
int capSensorLast;

bool
openRequest ()
{
    int capSensorValue = analogRead (CapSensorPin);

    if (2 < abs(capSensorLast - capSensorValue)) {
        capSensorLast  = capSensorValue;
        Serial.print   ("Capacitive Sensor Value: ");
        Serial.println (capSensorValue);
    }

    return capSensorValue > CapSensorThreshold;
}

// -----------------------------------------------------------------------------
int testDistance = 50;

int getDistance ()
{
    // Ultrasonic sensor reading
    digitalWrite      (TrigPin, LOW);
    delayMicroseconds (2);
    digitalWrite      (TrigPin, HIGH);
    delayMicroseconds (10);
    digitalWrite      (TrigPin, LOW);

    int duration = pulseIn (EchoPin, HIGH);
    int distance = duration * 0.034 / 2;

 // distance = testDistance;                // for testing

    Serial.print   (" Ultrasonic distance: ");
    Serial.println (distance);

    return distance;
}

// -----------------------------------------------------------------------------
void openLid ()
{
    Serial.println ("  open lid");
    servo.write    (90);
    delay          (1000);
    Serial.println ("  close lid");
    servo.write    (0);

    testDistance = 19;
}

// -----------------------------------------------------------------------------
void loop ()
{
    if (openRequest ())  {
        Serial.println ("Open request");
        if (TrashDepth < getDistance ())
            openLid ();

        if (TrashDepth > getDistance ())  {     // check again
            Serial.println ("   Trash full");
            digitalWrite (RedLedPin,   On);
            digitalWrite (GreenLedPin, Off);
        }

        while (openRequest ())
            ;
    }
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);

    pinMode      (TrigPin,     OUTPUT);
    pinMode      (EchoPin,     INPUT);

    pinMode      (GreenLedPin, OUTPUT);
    pinMode      (RedLedPin,   OUTPUT);
    digitalWrite (RedLedPin,   Off);
    digitalWrite (GreenLedPin, Off);

    servo.attach (ServoPin);
}

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().

thank you all for helping me.

Did it work?

Tom.... :smiley: :+1: :coffee: :australia: