code:
#define trigger 5
#define echo 4
#define Relay 6
float time=0,distance=0;
void setup()
{
Serial.begin(9600);
pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);
pinMode(Relay,OUTPUT);
delay(2000);
}
void loop()
{
measure_distance();
if(distance<5)
{
digitalWrite(Relay,LOW);
}
else
{
digitalWrite(Relay,HIGH);
}
delay(500);
}
void measure_distance()
{
digitalWrite(trigger,LOW);
delayMicroseconds(2);
digitalWrite(trigger,HIGH);
delayMicroseconds(10);
digitalWrite(trigger,LOW);
delayMicroseconds(2);
time=pulseIn(echo,HIGH);
distance=time*200/20000;
}
Post a Comment