int trigpin = 4;
int echopin = 5;
float duration, distance;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW);
duration = pulseIn(echopin, HIGH);
distance = 0.017 * duration;
if(distance <= 10){
tone(33, 562);
delay(1000);
noTone(33);
delay(1000);
}else if(distance > 10 && distance <= 20 ){
tone(33, 352);
delay(1000);
noTone(33);
delay(1000);
tone(33, 396);
delay(1000);
noTone(33);
delay(1000);
tone(33, 440);
delay(1000);
noTone(33);
delay(1000);
}else{
noTone(33);
}
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
delay(1000);
}