#include<Servo.h>
int trig= 5;
int echo= 0;
Servo servo;
int MyServoPin=3;
int time;
float distant ;
void setup() {
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
servo.attach(MyServoPin);
Serial.begin(9600);
}
void loop()
{
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
time=pulseIn(echo,HIGH);
distant=(time*0.034/2)/100;
Serial.print("distant ");
Serial.print(distant);
Serial.println(" meters");
if (distant<=2.5)
servo.write(0);
else if (distant>2.5)
servo.write(180);
delay(1000);
}