/**
HC-SR04 Distance Sensor Example
https://wokwi.com/arduino/projects/304444938977804866
*/
#define PIN_TRIG 3
#define PIN_ECHO 2
#define TONE_USE_INT
#define TONE_PITCH C3. 1000
#define TONE_PITCH C5. 1000
int red = 12; //COMMENT Establish red as my led.
void setup() {
Serial.begin(115200);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
pinMode(red, OUTPUT); //red is an output
pinMode(5, OUTPUT);
}
void loop() {
// Start a new measurement:
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
digitalWrite(red, LOW);
tone(5, A3, 1000);
delay(1000);
// 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);
digitalWrite(red, HIGH);
tone(5, A5, 1000);
delay(1000);
}