HEADERS
Creates variables to store distance measurements in
both centimeters and inches. It defines distance thresholds
(like 20, 30, 40, 50, and 60 inches).These are used to decide how
the buzzer should behave depending on how far an object is.
SETUP
communication with the computer starts, so you
can see distance results in a serial monitor.
It also gets the system ready to measure distances
and make sounds (no need to worry about the wiring).
LOOP
The loop measures the distance with the sensor,
and changes the buzzer sound based on the distance.
This all happens continusly as the program loops
over itself. The code loops 5-6 times per second
CODE EXPLANED FOR NERDS
#define PIEZO_PIN 9
Defines pin 9 as the pin for the piezo buzzer.
const int trigger = 6;
Defines pin 6 as the trigger pin for the ultrasonic sensor.
const int echo = 7;
Defines pin 7 as the echo pin for the ultrasonic sensor.
float distance;
Variable to store the measured distance in centimeters.
float dist_inches;
Variable to store the distance in inches.
Multiple float inch_* variables:
Define distance thresholds in inches for different behaviors of the buzzer:
inch_maxthreshold = 60;
inch_secondmax = 50;
inch_medium = 40;
inch_secondmin = 30;
inch_min = 20;
Serial.begin(9600);
Starts serial communication at 9600 baud rate.
pinMode(trigger, OUTPUT);
Sets the trigger pin as an output.
pinMode(echo, INPUT);
Sets the echo pin as an input.
pinMode(PIEZO_PIN, OUTPUT);
Sets the piezo pin as an output.
Distance Measurement
Trigger Signal
digitalWrite(trigger, LOW);
Ensures the trigger pin is low initially.
delayMicroseconds(5);
Adds a short delay.
digitalWrite(trigger, HIGH);
Sends a high signal to trigger the ultrasonic sensor.
delayMicroseconds(10);
Keeps the trigger signal high briefly.
digitalWrite(trigger, LOW);
Turns the trigger pin low again.
Echo Signal and Calculation
distance = pulseIn(echo, HIGH, 38000);
Measures the duration of the HIGH signal on the echo pin (in microseconds).
distance = distance * 0.0135;
Converts the duration into a distance in inches.
dist_inches = distance;
Assigns the computed distance to the dist_inches variable.
Buzzer Control
Behavior Based on Distance
The buzzer's tone and delay change based on the measured distance (dist_inches):
Closest Range
if (dist_inches > 2 && dist_inches < inch_min)
Produces a high-pitched tone (1725 Hz) with a short delay (50 ms).
Second Closest Range
else if (dist_inches >= inch_min && dist_inches < inch_secondmin)
Slightly lower tone (1675 Hz) with a longer delay (125 ms).
Medium Range
else if (dist_inches >= inch_secondmin && dist_inches < inch_medium)
Further reduced tone (1550 Hz) with a delay of 150 ms.
Second Farther Range
else if (dist_inches >= inch_medium && dist_inches < inch_secondmax)
Tone: 1475 Hz; delay: 175 ms.
Farthest Range (Under Max Threshold)
else if (dist_inches >= inch_secondmax && dist_inches < inch_maxthreshold)
Tone: 1400 Hz; delay: 225 ms.
Serial Output
Serial.print("Distance: ");
Prints "Distance: " to the serial monitor.
Serial.print(dist_inches);
Prints the calculated distance in inches.
Serial.println(" in");
Completes the line with " in" (indicating inches).