I have used the following code in order to measure the speed of sound, it works!
but I can't understand the logic of it!!! especially this line "pingTime = pulseIn(echoPin, HIGH);"
I would really appreciate that if anyone answers me and tells me the exact reason why this code prints the speed of sound.
what are the rules of
delayMicroseconds(2000);
delayMicroseconds(10);
int trigPin = 13;
int echoPin = 11;
float pingTime;;
float speedOfSound;
float targetDist = 15;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2000);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pingTime = pulseIn(echoPin, HIGH);
speedOfSound = 2*targetDist / pingTime ;
speedOfSound = speedOfSound * 10000;
Serial.print("The Speed of Sound is ");
Serial.print(speedOfSound);
Serial.println(" m/s");
delay(1000);
}
Moderator: added code tags