int trigPin = 2;
int echoPin = 3;
// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
int buzzer = 4;
void setup()
{
pinMode(buzzer, OUTPUT);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
Serial.begin(9600); // Serial Communication is starting with 9600 of baudrate speed
Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
Serial.println("with Arduino UNO R3");
}
void loop(){
// put your main code here, to run repeatedly:
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2); // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH); // Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
if (distance < 10) {
tone(buzzer, 1000);
Serial.println(("on"));
}
else {
noTone(buzzer);
}
}