Need help for code for Automatic Waste Segregator Project

Hi
I am new to Arduino and I have been doing Automatic Waste Segregator which segregates metal, wet and dry waste.
I am using Moisture Sensor (for detecting wet waste and dry waste), Ultrasonic sensor, Inductive Sensor (for detecting metallic waste) and 2 Servo motor

I have seperate my project into two. One is for opening/closing flap. Other is for rotating the bin according to waste.

I've used this code

#include <LiquidCrystal.h>
#include <NewPing.h>
#include <HCSR04.h>
#include <Servo.h>

const int moisturePin = A3; // Moisture sensor pin
const int inductivePin = 5; // Inductive sensor pin
const int servoBottomPin = 4; // Servo motor for bin rotation
const int servoFlapPin = 1; // Servo motor for flap open/close
const int triggerPin = 3; // Ultrasonic sensor trigger pin
const int echoPin = 2; // Ultrasonic sensor echo pin

Servo servoBottom; // Bottom servo object
Servo servoFlap; // Top servo object

const int metallicThreshold = LOW; // Inductive sensor threshold for metal
const int wetThreshold = 870; // Moisture sensor threshold for wet waste
const int dryRangeMax = 1000; // Moisture sensor range for dry waste

void setup() {
  Serial.begin(9600); // Initialize serial communication

  // Initialize servo motors
  servoBottom.attach(servoBottomPin);
  servoFlap.attach(servoFlapPin);

  // Set pin modes
  pinMode(moisturePin, INPUT);
  pinMode(inductivePin, INPUT);
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  int moistureValue = analogRead(moisturePin);
  int distance = readHCSR04();
  bool isMetal = digitalRead(inductivePin) == metallicThreshold;
  bool isWet = moistureValue < wetThreshold;
  bool isDryRange = moistureValue >= wetThreshold && moistureValue <= dryRangeMax;

  // Check for metallic waste
  if (isMetal) {
    Serial.println("Metal detected!");
    servoBottom.write(180);
    delay(3000);
    servoBottom.write(0);
    delay(3000);
  }

  // Check for wet waste
  else if (isWet) {
    Serial.println("Wet waste detected!");
    servoBottom.write(90);
    delay(3000);
    servoBottom.write(0);
    delay(3000);
  }

  // Check for dry waste
  else if (isDryRange) {
    Serial.println("Dry waste detected!");
    servoBottom.write(45);
    delay(3000);
    servoBottom.write(0);
    delay(3000);
  }

  // Check for object within 2 cm range
  if (distance < 3) {
    Serial.println("Opening flap");
    servoFlap.write(90);
  } else {
    servoFlap.write(45);
    Serial.println("Closing flap");
  }

  // Delay before next cycle
  delay(3000);
}

int readHCSR04() {
  // Trigger the ultrasonic sensor
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);

  // Read the echo pulse duration
  long duration = pulseIn(echoPin, HIGH);

  // Convert pulse duration to distance
  int distance = duration * 0.034 / 2;

  return distance;
}

I am having problem with my servo motor (flap) which is connected to pin1 and ultrasonic sensor
In this mentioned code, I have added this concept
" If the object is detected by ultrasonic sensor in within 2 cm range, then the servo motor (flap) rotated to 90 degrees. If not, then it is rotated to 45 degrees."

But I have noticed the servo motor is rotating form 90 to 45 degrees in a loop
Even though, I have fixed that ,when the object is within 2 cm range. The servo motor rotates.

Please someone help me with rewrite this code by the working mentioned above
Thank You

Do you have a budget?

It would help if you said what it is doing now that it should not, or what it is not doing now that it should.

Did you write this code?

a7

I am having problem with my servo motor (flap) which is connected to pin1 and ultrasonic sensor
In this mentioned code, I have added this concept
" If the object is detected by ultrasonic sensor in within 2 cm range, then the servo motor (flap) rotated to 90 degrees. If not, then it is rotated to 45 degrees."

But I have noticed the servo motor is rotating form 90 to 45 degrees in a loop
Even though, I have fixed that ,when the object is within 2 cm range. The servo motor rotates.

I reuploaded my code in my post.
Thank you for your kindness. :slight_smile:

Don’t use pin 1 for your servo. Pins 0 and 1 are used for Serial
Communication.
/regs

If you disconnect the servos, do all you print statements make sense?

If you temporarily further modify your circuitry to use pushbuttons and/or potentiometers to supply the values that would otherwise come from sensors, does your code run and make sensible print statements?

I would add extra print statements, but you should already have enough to see the basic flow.

Pro tip: for this testing without servos, you can speed things up a bit in a sketch based on delay() by using smaller numbers.

# define WAIT 333    // use 3000 in final 

up top, and then always

  delay(WAIT);    // standard pause

HTH

a7

My servo motor is stalled (connected to pin1).
Does connecting my servo to other pin solve this problem.
Anyway. Thanks for this advice.

What does this mean "your servo motor is "stalled"?"

whenever you print something to the serial monitor this is done on IO-pin 1
This means printing to the serial monitor is disturbing the servo-signal
and the servo-signal makes the serial data to crab

You should really use a different IO-pin
IO-pin number 0 and IO-pin number 1 are taboo
they are exclusively for serial communication with your computer

best regards Stefan

this include is useless, you make distance measuring self in readHCSR04()