# Arduino Rounding question

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;
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 Temp2;
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();
button.tick();
if(currentTime >= (loopTime + 500)){
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


No, the problem is that you didn't erase the "0" at the end of "100". Output enough spaces after the current value to blank out the previous value.

• Thanks, All i did was add another lcd.print(" "); on the line after the set temps. Thanks – D.W. Nov 3 '13 at 20:44
• Funny enough this happened to me as well when I first began using LCD screens so I knew immediately what you're problem was. I was so confused on why my counter would go from 0 -> 255, and then when it counted backwards that, once below 100, it would start at 990 and go down by 10s! – sherrellbc Jun 22 '14 at 16:56

This isn't down to rounding, but down to the way the characters are displayed on your lcd. Once you display 100 you are displaying three characters on your display then when it drops down to 99 you are only displaying 2 characters, yet the display shunts it right leaving the 0 from the 100 still displayed.

Not sure of the exact solution but try experimenting with the lcd.setCursor(9,1); and lcd.setCursor(9,0); functions.

Or call the lcd.clear(); function somewhere in your void loop();

Also, writes to EEPROM will wear it out.

Better to use a variable on the stack, or heap.

Another tip, the only way to debug arduino is Serial.println() it's tedious however it will confirm what they said.