#include <WiFi.h>
//wifi connection
const char *ssid = "Wokwi-GUEST";
const char *pass = "";
//wifi declaration
WiFiClient espClient;
//pin cofiguration
const int rxpin = 26; //echopin
const int txpin = 25;//trig
//wifi conn
void wificonn(){
//connecting to wifi network
WiFi.begin(ssid, pass);
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("connecting to WiFi..");
}
Serial.println("connected to WiFi");
}
void setup(){
//initialize serial communication
Serial.begin(9600);
//configure pin modes
pinMode(txpin, OUTPUT);
pinMode(rxpin, INPUT);
}
void loop(){
// send a 10 microseconds pulse to trigger pin
digitalWrite(txpin, LOW);
delayMicroseconds(2);
digitalWrite(txpin, HIGH);
delayMicroseconds(10);
digitalWrite(txpin, LOW);
//measure the duration of the echo pulse
long duration = pulseIn(rxpin, HIGH);
/*Introduction to C++ long. In C++,
long is a data type for a constant or variable which has the capability
of storing the variable or constant values with 64-bits storage
and is signed integer data type which is used for storing variable or
constants with larger values larger than standard integer 32-bit data type*/
// Calculate distance (speed of sound = 340 m/s or 0.034 cm/µs)
// Divided by 2 because the signal travels to the object and back
float distance = (duration * 0.034) / 2;
//diplay in serial monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(1000);
}