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