CODE:- int trigpin=12;
int echopin=11;
int traveltime;
float pingtraveldistance;
float targetdistance;
int buzzer=9;
void setup() {
// put your setup code here, to run once:
pinMode(trigpin,OUTPUT);
pinMode(echopin,INPUT);
pinMode(buzzer,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trigpin,LOW);
delayMicroseconds(10);
digitalWrite(trigpin,HIGH);
delayMicroseconds(10);
digitalWrite(trigpin,LOW);
delayMicroseconds(25);
traveltime=pulseIn(echopin,HIGH);
targetdistance=(traveltime*0.034)/2;
digitalWrite(buzzer,LOW);
if (targetdistance<45) {
digitalWrite(buzzer,HIGH);
}
}
1 Comments
Diagram
ReplyDelete