#include <Servo.h>
long duration;
int distance;
const int trigpin=13, echopin=12;
Servo myservo;
int pos=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
myservo.attach(11);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trigpin, LOW);
delay(1000);
digitalWrite(trigpin, HIGH);
delay(1000);
digitalWrite(trigpin, LOW);
duration = pulseIn(echopin, HIGH);
distance = duration * 0.034/2;
Serial.print("UltraSonic ");
Serial.println(distance);
for (pos = 0; pos <= 180; pos += 1) {
myservo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(15);
}
}