#define PIN_TRIG 26
#define PIN_ECHO 25
#define BUZZER 5
#define MOTOR 27
unsigned int level = 0;
void setup() {
pinMode(BUZZER, OUTPUT);
pinMode(MOTOR, OUTPUT);
digitalWrite(MOTOR, LOW);
Serial.begin(115200);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
}
void loop() {
// Start a new measurement:
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
// Read the result:
int duration = pulseIn(PIN_ECHO, HIGH);
Serial.print("Distance in CM: ");
Serial.println(duration / 58);
Serial.print("Distance in inches: ");
Serial.println(duration / 148);
level = duration / 58;
// Produce auditory feedback based on the measured distance:
if (level < 100) {
tone(BUZZER, 500, 100); // Short, high-pitched tone
digitalWrite(MOTOR, HIGH); // Activate motor
} else if ((level >= 100) && (level < 200)) {
tone(BUZZER, 300, 100); // Medium-pitched tone
digitalWrite(MOTOR, LOW); // Deactivate motor
} else if (level >= 200) {
tone(BUZZER, 200, 100); // Long, low-pitched tone
digitalWrite(MOTOR, LOW); // Deactivate motor
}
delay(1000); // Delay before next measurement
}