I'm developing with the Arduino UNO a device that uses a TSL238 light-to-frequency photodiode to measure light.
Just because in astronomy the 'standard' is a negative logaritmic one unit, I use log10 function (from math.h) here to pass from Hz (output of the TSL237, measured with pulseIn as the pulses are pretty slow for FreqCounter and FreqPeriod) to the logaritmic unit system with an offset of 22.0 .
magnitude = 22.0-2.5*log10(frq);
But something isn't ok with that simple operation.
1.03hz => 21.970mag
1.01hz => 21.994mag (that's OK, decreasing frequency means higher magnitude)
0.92hz => 22.91 (that doesn't seem to be correct. I expected 22.091)
0.91hz => 22.98 (also incorrect!)
0.78hz => 22.272 (correct!)
0.64hz => 22.480 (correct!)
What is wrong in the 22.0-2.5log10(0.92) and 22.0-2.5log10(0.91) ?
Based on the last version published by mem (which uses byte precision)
void printDouble( double val, byte precision){
// prints val with number of decimal places determine by precision
// precision is a number from 0 to 6 indicating the desired decimial places
// example: printDouble( 3.1415, 2); // prints 3.14 (two decimal places)
Serial.print (int(val)); //prints the int part
if( precision > 0) {
Serial.print("."); // print the decimal point
unsigned long frac;
unsigned long mult = 1;
byte padding = precision -1;
while(precision--)
mult *=10;
if(val >= 0)
frac = (val - int(val)) * mult;
else
frac = (int(val)- val ) * mult;
unsigned long lfact = 10;
while(int(lfact*(val-int(val)))==0 && lfact<mult){
lfact *= 10;
Serial.print("0");
}
unsigned long frac1 = frac;
while( frac1 /= 10 )
padding--;
Serial.print(frac,DEC) ;
}
}
I need to test extensively, but the first measures seems to be pretty ok.