Two examples here. As small part of my MRMP controller code. The first small and fast and the second, commented out, has a range checking schema.
Both return the temp in degrees C x10. In general, floating point is rarely required and should be avoided.
// TTTTTTTTTTTTTTTTTTTT Temperature 2.2 TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
// TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
int tempOnPin(int pin,int offset,int slope)
{
return ((((analogRead(pin) * 10) - offset) * 100) / slope);
}
/*
#define BitsPerDegree 215 // Slope 2.15
// #define Bits0Degrees 5490 // Offset. 10 x the bits at zero degrees in WATER.
#define Bits0DegreesAir 5650 // Offset. 10 x the bits at zero degrees AIR temp.
// When self heating is an issue for air temp make this lower.
#define MaxTempError 2000 // Used to set upper and lower before error value passes
#define MinTempError -500
#define TempError -32000 // Used by functions to determine if sensor shorted or open
// Read the temperature LM335 sensor 2k2 and 5v
int tempOnPin(int pin,int offset,int slope)
{
long temp;
temp = analogRead(pin); // read the value from the sensor
temp = ((((temp * 10) - offset) * 100) / slope); // 10 x the degrees c on pin returned
if ((temp > MaxTempError) || (temp < MinTempError)) // between Max and Min or error
{
return (TempError) ; // Used by calling routine to determine if sensor is shorted or open
}
else
{
return (int(temp));
}
}
*/