#include <Servo.h>
#define trigPin 7
#define echoPin 6
#define servo 9
#define led 13
Servo binservo;
int position = 0;
long duration;
float distance;
void setup(){
binservo.attach(servo);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);;
binservo.write(position);
}
void loop(){
Serial.begin(9600);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = 0.03423*(duration/2);
digitalWrite(led, HIGH);
if (distance<= 2)
{
binservo.write(180);
delay(1500);
}
else
{
digitalWrite(led, LOW);
binservo.write(30);
delay(10);
}
delay(300);
}