const byte trig_pin = 12;
const byte echo_pin = 14;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(trig_pin, OUTPUT);
pinMode(echo_pin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int distance = get_distance();
Serial.println(distance);
delay(10); // this speeds up the simulation
}
int get_distance(){
digitalWrite(trig_pin, LOW);
delayMicroseconds(2);
digitalWrite(trig_pin, HIGH);
delayMicroseconds(10);
digitalWrite(trig_pin, LOW);
long int duration = pulseIn(echo_pin, HIGH);
float sound_speed = 0.034;
long int distance = (duration * sound_speed) / 2;
return distance;
}