#include <Servo.h>
Servo s;
int echo = 26;
int trig = 27;
float duration , distance;
void setup() {
// put your setup code here, to run once:
Serial1.begin(115200);
s.attach(28);
pinMode(echo , INPUT);
pinMode(trig , OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trig , LOW);
delayMicroseconds(2);
digitalWrite(trig , HIGH);
delayMicroseconds(10);
digitalWrite(trig , LOW);
duration = pulseIn(echo , HIGH);
distance = duration * 0.0343 / 2;
if(distance <= 200){
s.write(45);
}else{
s.write(180);
}
}