long duration;
const int trig = 13;
const int echo = 12;
const int saftey = 11;
int distance ;
void setup() {
// put your setup code here, to run once:
pinMode(trig,OUTPUT);
pinMode(echo, INPUT);
pinMode(saftey,OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trig,LOW);
delay(200);
digitalWrite(trig,HIGH);
delay(500);
digitalWrite(trig,LOW);
duration = pulseIn(echo,HIGH);
distance = duration * 0.034/2;
Serial.println(distance);
if (distance<51)
{
digitalWrite(saftey , HIGH);
}
else
{
digitalWrite(saftey, LOW);
}
}