I need help trying to make a servo move after a value is read for a few seconds.

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)

IF your ultrasonic sensor is giving false values, then ignore those values.

Change the delay(60) to whatever time you want when you get a value that you want.

Paul