An update about the math to include Temp as my thinking yesterday contained some serious flaws.

So lets do the thinking again, and now with focus and real math

`cm = time * 331.45 * sqrt( 1 + t/273 ) / 10000;`

Lets say the temperature is between -55 and 125 Celsius (range of the DS18B20)

that means that sqrt() is between 0.8936 and 1.2074.

as square root is quite linear on such small range we can map T [-55..125] to [915..1237] (*1024)

`y = 915 + (T+55) * 322/180; = 915 + (T+55) * 1.7888888; // do not forget the / 1024 or >> 10`

that makes the formula

`distance = duration * 331.45 * (915 + (T+55) * 1.7888888) /1024 /10000;`

bringing the factors together

distance = duration * (915 + (T+55) * 1.7888888) * 331.45 /1024 /10000;

distance = duration * (915 + (T+55) * 1.7888888) * 0.53032 / 16384;

this leads to the following code

uint32_t PING2cm3(uint32_t duration, int t)

{

uint32_t x = t+55;

// *1.788888888

x = x + (x>>1) + (x>>2) + (x>>5);

x = x + 915;

x = x * duration;

// * 0.53032

x = (x >> 1) + (x >> 5) + (x >> 8) + (x >> 9);

x = x >> 14;

return x;

}

performance - 25.4152 per call

sqrt() formula - 122.2460 per call (~factor 5)

1/29 formula - 38.7380 per call // has no temp correction

varying duration from 300-100,000 at 20C ==> error 0..1% // at shorter range the error increases

varying temperature 0..125C at constant duration of 10,000 ==> error 0..1% (the variation in SOS == 0..14% )

varying temperature 0..125C at constant duration of 100,000 ==> error 0..1%

Conclusion:

The above formula added temperature correction for PING))) with no performance costs, while the error stays within 1% range.

Note1: error can improved by adding extra factors in the approximation formulas.

Note2: ping itself takes about 300 (5cm) - 10000 (150cm) uSeconds , so for the smaller distances the profit wrt the sqrt formula is substantial.