I have made a simple thermostat for controlling a 12V heater. The temperature range can be from 0 F to around 190 F. For some reason when the displayed temperature on my LCD goes from 99 to 100 everything is fine but if it goes back down from 100 to 99 the value ends out being 990. I think this is something to do with the rounding in my Thermistor Temp calculation. Any thoughts? Code posted below.
/*
The circuit:
* 5V to Arduino 5V pin
* GND to Arduino GND pin
* CLK to Analog #5
* DAT to Analog #4
* BUTTON to Analog #2
* Thermistor to Analog #3
* Heater to digital #7
*/
// include the library code:
#include <Wire.h>
#include <LiquidCrystal.h>
#include "OneButton.h"
#include <EEPROM.h>
LiquidCrystal lcd(0);
int heater = 7; // heater is connected to pin 2
OneButton button(A2, true);
float settemp;
unsigned long currentTime;
unsigned long loopTime;
void setup()
{
currentTime = millis();
loopTime = currentTime;
EEPROM.read (1); // make the eeprom or atmega328 memory address 1
Serial.begin(9600);
// link the doubleclick function to be called on a doubleclick event.
button.attachDoubleClick(doubleclick);
button.attachClick(click);
pinMode(heater, OUTPUT); // Set the Heater Valve pin as output
lcd.begin(16,2);
delay(1000);
lcd.setBacklight(HIGH);
delay(1000);
lcd.setCursor(0,0);
lcd.print(" D.W. VEGGIE");
lcd.setCursor(0,1);
lcd.print(" BURNER SYSTEMS");
delay(4000);
lcd.clear();
delay(1000);
lcd.setCursor(0,0);
lcd.print(" CENTRITHERM");
lcd.setCursor(0,1);
lcd.print(" V1.0");
delay(4000);
lcd.clear();
delay(1000);
lcd.setCursor(0,0);
lcd.print("SET TEMP F ");
lcd.setCursor(0,1);
lcd.print("OIL TEMP F ");
}
double Thermister2(int RawADC) {
double Temp2;
Temp2 = log(((10240000/RawADC) - 10000));
Temp2 = 1 / (0.001129148 + (0.000234125 * Temp2) + (0.0000000876741 * Temp2 * Temp2 * Temp2));
Temp2 = Temp2 - 273.15; // Convert Kelvin to Celcius
Temp2 = (Temp2 * 1.8) + 32; // Convert to F
Temp2 = round(Temp2);
return Temp2;
}
void loop()
{
currentTime = millis();
settemp = EEPROM.read(1); // read the settemp on the eeprom
button.tick();
if(currentTime >= (loopTime + 500)){
double Temp2 = Thermister2(analogRead(1)); // read Thermistor
if (Temp2 < settemp) digitalWrite(heater, HIGH); // turn heater on
else if (Temp2 >= settemp) digitalWrite(heater, LOW); // turn heater off
lcd.setCursor(9,1);
lcd.print(Temp2,0);
loopTime = currentTime;
}
lcd.setCursor(9,0);
lcd.print(settemp,0);
EEPROM.write (1,settemp);
delay(10);
}
// this function will be called when the button was pressed 2 times in a short timeframe.
void doubleclick() {
settemp -- // remove one to the settemp, the settemp is the ideal temperature for you
;
} // doubleclick
// this function will be called when the button was pressed 1 time in a short timeframe.
void click() {
(settemp ++); // add one to the settemp, the settemp is the ideal temperature for you
} // click
// End