const int trigpin = 5;
const int echopin = 18:
#define SOUND SPEED 0.034 //define sound speed in cm/us
#define CM_TO_INCH 0.393701
long duration:
float distanceCm/
float distanceInch;
void setup()
{
Serial.begin(115200) // Starts the serial communication
pinMode (trigPin, OUTPUT); // Sets the trigpin as an output
pinMode (echopin, INPUT);
// Sets the echopin as an Input
void loop()(
digitalWrite(trigPin, LOW); // Clears the trigin
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); // Sets the trigPin on HIGH state for 10 micro seconds
delayMicroseconds (10):
digitalWrite (trigPin, LOW);
duration= pulsein (schopin, HIGH); // Reads the echopin, returns the sound wave travel time in microseconds
distanceCm = duration SOUND SPEED/2;// Calculate the distance
distanceInch = distancem CM_TO_INCH;// Convert to inches
Serial.print ("Distance (cm): "); // Prints the distance in the Serial Monitor
Serial.println(distanceCm) >
Serial.print ("Distance (inch): ");
Serial.println(distanceInch);
delay(1000)