Hi,
I have written a code to sample the temperature from a TM35 and following smoothing output the current temperature, lowest and highest temperature since reset to an LCD display and the serial port.
However the temp and max temp are reported correctly the min temp is reported as 100x smaller than the temp and I cannot see why.
I also want to set up a routine so that the LCD display only updates at a preset interval, i.e 10 seconds, but the serial continues to output in real time.
Hopefully someone can tell me what's wrong.
Output from serial
Temp =18.97 Min Temp =0.19 Max Temp =26.03
Temp =18.97 Min Temp =0.19 Max Temp =26.03
Temp =18.97 Min Temp =0.19 Max Temp =26.03
Temp =18.97 Min Temp =0.19 Max Temp =26.03
Temp =18.97 Min Temp =0.19 Max Temp =26.03
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define the number of samples to keep track of. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input. Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
const int numReadings = 100;
int readings[numReadings]; // the array readings from the analog input
int index = 0; // the index of the current reading
float totaltemp = 0; // the running total
float averagetemp = 0; // the average temperature
float maxtemp = -255.0; // the max temperature
float mintemp = 255.0; // the min temperature
float temp; // the current temperature
char buffer[64];
int tempPin = A0;
// Set up the LCD
// set the LCD address to 0x27 for a 16 chars 2 line display
// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
void setup()
{
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0 for gettemp
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
lcd.begin(16,2); // initialize the lcd for 16 chars 2 lines, turn on backlight
lcd.backlight(); // turnbacklight on intially
}
void loop()
{
gettemp();
delay(2); // delay in between reads for stability
// check to see if the currenttemp is the lowest temperature since reset
// if it is then set lowtemp to currenttemp
if (temp < mintemp) mintemp = temp;
// check to see if the current temp is the highest temperature since reset
// if it is then set hightemp to currenttemp
if (maxtemp < temp) maxtemp = temp;
Serial.print("Temp =");
Serial.print(temp);
Serial.print("Min Temp =");
Serial.print(mintemp);
Serial.print("Max Temp =");
Serial.print(maxtemp);
Serial.println("");
updatedisplay();
}
int gettemp() // routine the get the average temp from the sensor
{
// subtract the last reading:
totaltemp= totaltemp - readings[index];
// read from the sensor:
readings[index] = ((5.0 * analogRead(tempPin) * 100.0)/1024);
// add the reading to the total:
totaltemp= totaltemp + readings[index];
// advance to the next position in the array:
index = index++;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
temp = totaltemp / numReadings;
// send it to the computer as ASCII digits
delay (2);
return 1;
}
// routine to update the LCD display if the displayfreq period has been reached
void updatedisplay()
{
// print the outputs to the LCD display
lcd.setCursor (0,0);
//sprintf(buffer,"Current: %i",temp);
lcd.print (temp);
lcd.setCursor (0,1); // move to the next row
//sprintf(buffer,"Min %i\337C Max %i\337C",mintemp, maxtemp);
lcd.print (mintemp);
lcd.setCursor (6,1);
lcd.print (maxtemp);
// reset the display timer back to zero
//starttime = millis();
}