/**********************************
*Project : CoEIS Symposium – IoT contest
*Institution : DeVry University
*Example : Reading Sensor
*Program Purpose: Demonstrating Reading Sensor
*Date created : 2024.06.11
**********************************/
// Define alias for Trig and Echo ports
#define Trig 26
#define Echo 25
const double speedOfSoundInAir = 343.; //Speed of sound used for th edistance calculation
long int pulseTime;
float distance;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(Trig, OUTPUT); // Configure the trigger pin as output. We will use this pin to send out ultrasonic burst
pinMode(Echo, INPUT);// Configure the echo pin as input. We will use this pin to read the pulse coming back from the ultrasonic sensor
}
void loop() {
//Sends a beep towards an obstacle and measures the time it takes for the beep to return back.
digitalWrite(Trig, LOW);
delayMicroseconds(5);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
pulseTime = pulseIn(Echo, HIGH, 35000);
// Use the pulseTime in microseconds to calculate the distance.
distance = double(pulseTime) * speedOfSoundInAir * 1.e-4 / 2.;//Calculates the distance in cm.
Serial.print(F("Sensor Distance: "));
Serial.print(String(distance, 1));
Serial.println(F("cm"));
delay(2000); // this speeds up the simulation
}