#define PIN_TRIG 3
#define PIN_ECHO 2
#define BUZZER_PIN 8
void setup() {
Serial.begin(115200);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
}
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);
// Play a tone:
tone(BUZZER_PIN, 262, 250); // Plays a 262Hz tone for 0.250 seconds
delay(1000); // Delay between measurements
}