#include <Servo.h>

double distancia;
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);

	servo_3.write(0);
	delay(50);

}


void loop()
{

  	distancia = fnc_ultrasonic_distance(4,5);
  	if ((distancia <= 30)) {
  		servo_3.write(180);
  		delay(50);
  	}
  	else {
  		servo_3.write(0);
  		delay(50);
  	}

}