Hello, I am new here , we are about to make trash bin that giving coin reward after detecting metal. I want to know if the 4 servomotor, 1 ultrasonic sensor, and 1 capacitive proximity will work for 1 Arduino with 9voltage?
this is the idea. The proximity sensor should be connected to 3 servo motor and then move, after that 1 servo will move if the ultrasonic sensor detect some object.
here is the our coding, we just get this from AI, Please check if this is right.
#include <Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
int proximityPin = 2; // Proximity sensor pin
int ultrasonicTrigPin = 3; // Ultrasonic sensor trigger pin
int ultrasonicEchoPin = 4; // Ultrasonic sensor echo pin
void setup() {
servo1.attach(5); // Servo 1 pin
servo2.attach(6); // Servo 2 pin
servo3.attach(7); // Servo 3 pin
servo4.attach(8); // Servo 4 pin
pinMode(proximityPin, INPUT);
pinMode(ultrasonicTrigPin, OUTPUT);
pinMode(ultrasonicEchoPin, INPUT);
}
void loop() {
// Read proximity sensor
int proximityValue = digitalRead(proximityPin);
// If proximity sensor detects something, move servos 1, 2, and 3
if (proximityValue == HIGH) {
servo1.write(90); // Move servo 1 to 90 degrees
servo2.write(90); // Move servo 2 to 90 degrees
servo3.write(90); // Move servo 3 to 90 degrees
delay(1000); // Wait for 1 second
} else {
servo1.write(0); // Move servo 1 back to 0 degrees
servo2.write(0); // Move servo 2 back to 0 degrees
servo3.write(0); // Move servo 3 back to 0 degrees
}
// Read ultrasonic sensor
long duration, distance;
digitalWrite(ultrasonicTrigPin, LOW);
delayMicroseconds(2);
digitalWrite(ultrasonicTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(ultrasonicTrigPin, LOW);
duration = pulseIn(ultrasonicEchoPin, HIGH);
distance = (duration / 2) / 29.1; // Calculate distance in centimeters
// If object detected by ultrasonic sensor, move servo 4
if (distance < 10) { // Adjust this threshold according to your needs
servo4.write(90); // Move servo 4 to 90 degrees
delay(1000); // Wait for 1 second
} else {
servo4.write(0); // Move servo 4 back to 0 degrees
}
}