#include <science.h>
#include <math.h>
Temperature t( PIN2 );
double
kelvin( int value )
{
double Temp = log( 10000.0 * ( ( 1024.0 / value - 1 ) ) );
return 1.0 / ( 0.001129148 + ( 0.000234125
+ ( 0.0000000876741 * Temp * Temp ) ) * Temp );
}
class Stats
{
unsigned long n;
double mean;
double variance;
double sum;
public:
Stats( void )
{
n = 0; mean = 0; variance = 0; sum = 0;
}
void sample( double s )
{
double delta = s - mean;
n++;
mean += delta / n;
sum += delta * delta;
}
double get_mean( void ) { return mean; }
double get_variance( void ) { return sum / n; }
double get_sd( void ) { return sqrt( sum / n ); }
double get_snr( void ) { return get_mean() / get_sd(); }
};
Stats stats_digital;
Stats stats_analog;
const double CALIBRATION_OFFSET = -26.0625;
void
setup()
{
Serial.begin( 115200 );
}
unsigned long x = 0;
void
loop()
{
int raw_adc = analogRead( A0 );
double k = kelvin( raw_adc );
x++;
Serial.print( x );
t.read( 0 );
double digital = t.fahrenheit( 0 );
stats_digital.sample( digital );
Serial.print( ": Digital: " );
Serial.print( digital, 4 );
Serial.print( ", Mean=" );
Serial.print( stats_digital.get_mean(), 4 );
Serial.print( ", SD=" );
Serial.print( stats_digital.get_sd(), 4 );
Serial.print( ", SNR=" );
Serial.print( stats_digital.get_snr(), 4 );
double analog = (k - 273.15) * 1.8 + 32 + CALIBRATION_OFFSET;
stats_analog.sample( analog );
Serial.print( " Analog: " );
Serial.print( analog, 1 );
Serial.print( ", Mean=" );
Serial.print( stats_analog.get_mean(), 4 );
Serial.print( ", SD=" );
Serial.print( stats_analog.get_sd(), 4 );
Serial.print( ", SNR=" );
Serial.println( stats_analog.get_snr(), 4 );
}