You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.3 KiB

#include "temperature_sensor.h"
TemperatureSensor::TemperatureSensor(byte pin, int refResistance, double a, double b, double c) {
this->pin = pin;
this->refResistance = refResistance;
this->a = a;
this->b = b;
this->c = c;
init();
}
#define DEBUG_OUTPUT Serial
void TemperatureSensor::init() {
pinMode(pin, INPUT);
}
void TemperatureSensor::preload() {
for(int i=0; i< 10; i++){
update();
}
}
void TemperatureSensor::update() {
if(measurementCounter < 10){ //Measure 10 times before calculating temperature from average
measurementAccumulative += analogRead(pin);
measurementCounter++;
} else {
int sensorValue = measurementAccumulative/measurementCounter;
// voltage = sensorValue*5.0/1024.0
// r1 = (vdd-vout)*r2/vout
double resistance = ((1.0-sensorValue/1024.0)*refResistance/(sensorValue/1024.0)); // this is our probe resistance.
double lt = log(resistance);
temperature = (1 / (a+b*lt+c*lt*lt*lt)) - 273.15; //Calculate temperature in Kelvin according to Steinhart-Hart equation, then convert to Celcius
if(temperature < -35) temperature = sqrt (-1); // If value is unpossibly low, return NaN
measurementAccumulative = 0;
measurementCounter = 0;
}
}
float TemperatureSensor::getTemperature() {
return temperature;
}