#include <Servo.h>
int TRIG = 13;
int ECHO = 12;
Servo myservo1;
void setup() {
myservo1.attach(11);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
}
void loop() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
int temp = pulseIn(ECHO, HIGH);
int distance = temp / 148;
if (distance <=5 && distance >=10) { // if the object's distance is less than or equal to five the servo will turn 90 degrees
myservo1.write(90);
}
else if (distance >=5 && distance <=10) { // is it is close enough to 10 it will go back to its original position, 0 degree
myservo1.write(0);
}
else {
myservo1.write(0);
}
delay(100);
}