// feito na cadeira de Fundamentos de automação - IFRS - Campus Restinga
// Igor B. Farias
// reference: https://randomnerdtutorials.com/esp32-hc-sr04-ultrasonic-arduino/
#define echo 35
#define trigger 25
int response = 0;
int distanceCm = 0;
int duration = 0;
#define soundSpeed 0.034
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
}
void loop() {
//int response = 0;
// Clears the trigPin
digitalWrite(trigger, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echo, HIGH);
// Calculate the distance
distanceCm = duration * soundSpeed/2;
// Prints the distance in the Serial Monitor
Serial.print("Distance (cm): ");
Serial.println(distanceCm);
delay(1000);
}