Thermostat with User Input issues

Hi Guys,

I'm wondering if any of you wonderful people can help me.
Basically, Im trying to make a thermostat with user button to increase or decrease my threshold temperature.
I'm using an LM35 sensor. For my LCD display, I'm using Grove-LCD RGB Backlight v2.0 display which is wonderful.
The problem is when I tried to put in the button, the temperature that I can set will not stay. So when I set for 25 degrees Celcius, after about 5 secs it will either go to 24, 23,22 or it will rise up and wont stop. I honestly don't know what I'm not doing correctly.
And yes I am very new to Arduino and coding too.
I'm using an Arduino Uno by the way.
Please help, below are my codes:

Thanks.. =(

// Declare variables
#include <Wire.h>
#include <EEPROM.h>
#include "rgb_lcd.h"

rgb_lcd lcd;
const int colorR = 30;
const int colorG = 30;
const int colorB = 60;

float tempC;
float settemp;
int tempPin = A0; //temp sensor plugged in pin 0
int ledPin = 13; //closest to ground
int fan1 = 2; // fan connected to pin 2
int buttonup= 7;
int buttondown = 6;

// Write setup programme

void setup()
{
Serial.begin(9600); //Open serial port to communicate. sets data rate to 9600
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// initialize the serial communications:
lcd.setRGB(colorR, colorG, colorB);
lcd.print("Temp = ");
delay(1000);
pinMode(ledPin, OUTPUT);
pinMode(fan1, OUTPUT);
EEPROM.read(1); //make eeprom memory address
}

//Write loop that will control the
void loop()
{
tempC = analogRead(tempPin); // read the analog value from the lm35 sensor.
tempC = (5.0 * tempC * 100.0)/1024.0; // convert the analog input to temperature in centigrade.
lcd.setCursor(8,0);
lcd.print(tempC);
lcd.print("'C");
Serial.print((byte)tempC); // send the data to the computer.
settemp = EEPROM.read(1); // read the settemp at memory 1

delay (250);

if (digitalRead (buttonup)==1)
{
(settemp ++);
EEPROM.write(1, settemp);
}
else{
}
if (digitalRead(buttondown)==1)
{
(settemp --);
EEPROM.write(1, settemp);
}
else {
}
if (tempC > settemp)
{
digitalWrite(ledPin, HIGH);
digitalWrite(fan1, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
digitalWrite(fan1, LOW);
}

lcd.setCursor(0,1);
lcd.print("Set Temp To: ");
lcd.print(settemp);
Serial.print((byte)settemp);
// EEPROM.write(1, settemp);
delay(250);
}

So you've got 2 buttons.

Each button is, I assume, a momentary contact pushbutton, with one end going to the pin, and the other end, it looks like, going to +5v.

There's a problem here - when the switch is off, what is the wire going into the pin connected to? From your description, it sounds like nothing - it's just floating. So it may intermittently be high enough to read as high, or as low, depending on the electric fields around the wire. There's nothing to make the wire go back down to 0 after you've released the button - nor, in fact, to ensure that the voltage doesn't creep up until it triggers the input*. This is why you shouldn't let things float.

Reverse the logic (ie, look for the buttons being held low), and add pinMode(pin,INPUT_PULLUP), and connect the other side of the buttons to ground instead of +5. -OR- Wire a pulldown resistor between wire going to arduino and ground (to ensure that it's held low when switch is off.

  • This can be demonstrated dramatically by wiring up a MOSFET to drive an LED, but just connecting the gate to a piece of wire. You can do all sorts of things by holding power or ground and tapping the gate wire, or let go of that and just run your hands over the gate wire, etc. I've seen that produce a kalidescope effect. Which of course is why you should put a little pulldown (high value is fine here - 100k is plenty) between gate and source when using mosfets. Even if driving off an MCU, if the MCU tristates the pin, it's floating, and you'll want that pulldown.
if (digitalRead (buttonup)==1)
  {
    (settemp ++);
    EEPROM.write(1, settemp);
  }
  else{
  }

Why is there an empty else block? If there is nothing to do, get rid of it.
There is no reason for the parentheses around settemp++.
You should be looking at the state change detection example. You don't want to change values when the switch IS pressed. You want to do it when the switch BECOMES pressed.

And, of course, DrAzzy has some good points.