The following illustrates alternative method of calculating the distance within the Arduino yun sketch for some ultrasonic sensors as hc-SR04 :
The method of calculation is similar to the calculation of the distance from themselves to thunder after seeing the lightning and hearing the thunder later.
Calculate the speed of sound in m/s. In formula of variable “v”, the value “25” is environment temperature. Follow this link :
The sound is a vibration through the air and sound travels faster with increasing temperature. So the calculation of the speed of sound is very different, for example if calculated in Norway or Congo. In dettail, the vibration and soundwave propagation would also depend on the air pressure, but this aspect was considered negligible. However, in some places of high mountain would be appropriate to consider a light sensor calibration because of this variable (air pressure/humidity):
Theformula in m/s is as follows ==> v= 331.3+0,606*T.
At 20°C thespeed is 343m/s and therefore moreof 1200Km/h.
float v = 331.5+0.6*25;
Send thepulse sound. We uses the same pin for input and output (3). It initially sets the pin to LOW and then wait three microseconds to be sure that the value isset. Then we send a high signal (beep) for 5 micro seconds.
It calculates how much time has elapsed until the return of the pulse so that thesignal is returned low (pulseIn function): http://www.divilabs.com/2013/03/pulsein-function-of-arduino-programming.html
float t1=pulseIn(pingPin, HIGH);
Transformationin seconds, and division by two, since the trip includes round trip.
Assuming a constant speed, the distance is time multiplied by the speed. It also multiplies by 100 in order to have a measure in cm.
float d = t2*v *100;
After, you can send with YunClient result onbrowser via json (key variable defined at will by the developer):
Please remember to put at the end of the loop()function at least a delay of 200 ms in order to avoid overloading the powersupply and CPU processor.