int pingPin = 27;
int echoPin = 26;
int buzz = 25;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(buzz, OUTPUT);
pinMode(pingPin,OUTPUT);
pinMode(echoPin,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
//delay(10); // this speeds up the simulation
long duration,inches,cm;
digitalWrite(pingPin,LOW);
delayMicroseconds(2);
digitalWrite(pingPin,HIGH);
delayMicroseconds(10);
digitalWrite(pingPin,LOW);
duration = pulseIn(echoPin,HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
if(cm>100){digitalWrite(buzz,HIGH);}
else{digitalWrite(buzz,LOW);}
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
long microsecondsToInches(long microseconds) {
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;
}