#include <Servo.h>
int trigger = 8;
int echo = 7;
Servo ser;
void setup() {
// put your setup code here, to run once:
ser.attach(22);
Serial.begin(9600);
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
}
void loop() {
// put your setup code here, to run continuous:
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger, LOW);
int duration = pulseIn(echo, HIGH);
int cm = duration * 0.034 / 2;
Serial.println(cm);
check_dist(cm);
}
// function check distance rotate & servo //
void check_dist(int res)
{
if(res < 200){
ser.write(180);
delay(1000);
}else{
ser.write(45);
delay(1000);
}
}