#include <Servo.h>
Servo servoMain1; // Define our Servo
Servo servoMain2;
int trigpin = 10;
int echopin = 11;
int distance;
float duration;
float cm;
void setup() {
// put your setup code here, to run once:
servoMain1.attach(9); // servo on digital pin 10
servoMain2.attach(7);
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trigpin, LOW);
delay(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW);
duration = pulseIn(echopin, HIGH);
cm = (duration/58.82);
distance = cm;
if(distance<10)
{
servoMain1.write(90); // Turn Servo back to center position (90 degrees)
servoMain2.write(90);
delay(3000);
}
else{
servoMain1.write(0);
servoMain2.write(0);
delay(50);
}
}