Hello, I'm new to programming and using Arduino and I'm trying to do this project for school. Basically I want to use an ultrasonic sensor to detect when an object is close enough for a claw to grab it using a servo motor. Whenever I try this the ultrasonic sensor gives false values briefly that make the servo motor move. So, I was wondering if there was a way to make it so that the servo motor moves after a value is held for a couple of seconds.
Thanks
Here is my code:
#include <Servo.h>
#include <NewPing.h>
const int TriggerPin = 3;
const int EchoPin = 2;
const int ServoPin = 11;
// 100 = maxDistance
NewPing sonar(TriggerPin, EchoPin, 100);
Servo servo;
void setup() {
Serial.begin(9600);
servo.attach(ServoPin);
}
void loop() {
int cm = sonar.ping_cm();
Serial.println(cm);
int angle = map(cm, 0, 8, 15, 45);
servo.write(angle);
delay(60);
}
Claw.ino (396 Bytes)