ESP32 Starter Kit
(continued from previous page)
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
// Read the signal from the sensor: a high level pulse
//Duration is detected from the point sending "ping" command to the time receiving␣
˓→echo signal (unit: um).
float distance = pulseIn(EchoPin, HIGH) / 58.00; //Convert into distance
delay(10);
return distance;
}
void setup() {
Serial.begin(9600);//Set the baud rate to 9600
pinMode(TrigPin, OUTPUT);//Set Trig pin to output
pinMode(EchoPin, INPUT); //Set Echo pin to input
}
void loop() {
distance = checkdistance();
if(distance < 10){
tone(beeppin, 262);//Play DO
delay(1000);
}
if(distance < 20 && distance > 10){
tone(beeppin, 294);//Play Re
delay(1000);
}
if(distance < 30 && distance > 20){
tone(beeppin, 330);//Play Mi
delay(1000);
}
if(distance < 40 && distance > 30){
tone(beeppin, 349);//Play fa
delay(1000);
}
if(distance < 50 && distance > 40){
tone(beeppin, 392);//Play So
delay(1000);
}
if(distance < 60 && distance > 50){
tone(beeppin, 440);//Play La
delay(1000);
}
if(distance < 70 && distance > 60){
tone(beeppin, 494);//Play Si
delay(1000);
}
Serial.println(distance);
noTone(beeppin);//Stop
}
8.5. Arduino Project 143