#define Trigger 7 // define the trigger pin of the HC-SR04
#define Echo 6 // define the echo pin of the HC-SR04
#define Led 11 // define the anode pin of the led
#define Buzzer 13 // define the anode pin of the buzzer
void setup() {
pinMode(Trigger, OUTPUT); // Configure the trigger pin of the HC-SR04
pinMode(Echo, INPUT); // Configure the echo pin of the HC-SR04
pinMode(Led, OUTPUT); // Configure the Led
digitalWrite(Trigger, LOW);// Prepare the HC-SR04
}
void loop() {
digitalWrite(Trigger, HIGH); // Active and the trigger
delayMicroseconds(10); // pin on the HC-SR04 and
digitalWrite(Trigger, LOW); // deactive it after 10 microseconds
unsigned long Ultrasonic_Sensor_Time = pulseIn(Echo, HIGH); // calculate the
float meters = 0.0003438 * Ultrasonic_Sensor_Time / 2; // distance in meters
if(meters <= 1){ // when the meters
// are less than 1
digitalWrite(Led, HIGH); // the led lights up
tone(Buzzer, 1000, 100); // and the buzzer
delay(100); // sounds
}else{ // when the meters
// are more than 1
digitalWrite(Led, LOW); // the led turn off
}
delay(100); // a little delay
}
Comments