#include <Servo.h>
Servo myservo;
const int trigPin = 3;
const int echoPin = 2;
void setup() {
// put your setup code here, to run once:
myservo.attach(11);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
long duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
if (distance <=30){
myservo.write(180);
}
else {
myservo.write(0);
}
}