#define PIN_TRIG 3
#define PIN_ECHO 2
void setup(){
pinMode(8, OUTPUT); // Set buzzer - pin 9 as an output
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: ");
int distance = duration / 58;
Serial.println(distance);
if(distance<25){
tone(8, 350, 100);
delay(125);
}else if(distance<50){
tone(8, 300, 100);
delay(250);
}else if(distance<75){
tone(8, 250, 100);
delay(500);
}else if(distance<100){
tone(8, 200, 100);
delay(1000);
}else if(distance<150){
tone(8, 150, 100);
delay(1000);
}else if(distance<200){
tone(8, 100, 100);
delay(1000);
}
}