#include <Servo.h>
Servo servo_3;
double fnc_ultrasonic_distance(int _t, int _e){
unsigned long dur=0;
digitalWrite(_t, LOW);
delayMicroseconds(5);
digitalWrite(_t, HIGH);
delayMicroseconds(10);
digitalWrite(_t, LOW);
dur = pulseIn(_e, HIGH, 18000);
if(dur==0)return 999.0;
return (dur/57);
}
void setup()
{
servo_3.attach(3);
pinMode(4, OUTPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
servo_3.write(0);
}
void loop()
{
if (((fnc_ultrasonic_distance(4,5) < 20) || digitalRead(6))) {
servo_3.write(0);
}
else {
servo_3.write(90);
}
delay(100);
}