Hi there, I'm running this code on my Uno, it's a thermostat. It all works fine except for one thing: when the set temperature(des) and the actual temperature are the same, or when the des is one point below the actual temperature, if I press the up button (to add one degree to des) the des reading will jump up in increments of anywhere between 2 and 6 points. I have de-bounced the switches, which seems to have worked for the down button, and for the up button so long as the des is below the actual temperature. Can anybody see why this might be happening?
/*
The circuit:
* LCD RS pin to digital pin 8
* LCD Enable pin to digital pin 9
* LCD D4 pin to digital pin 10
* LCD D5 pin to digital pin 11
* LCD D6 pin to digital pin 12
* LCD D7 pin to digital pin 13
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
*relay connected to D0 to control heating element
*LED connected to D4 as quick visual indicator
*LM35 temperature sensor to A0
*up and down buttons to D1 and D2
*/
// include the library code:
#include <LiquidCrystal.h>
int des = 0; //set temperature variable
int lastdes = 0;
int pin = 1; //LM35 input
int tempc = 0; //temperature variable
int Counter1 = 0; // counter for the number of button presses
int Counter2 = 0; // counter for the number of button presses
int State1 = 0; // current state of the button
int State2 = 0; // current state of the button
int lastState1 = 0; // previous state of the button
int lastState2 = 0;
long lastDebounceTime1 = 0; // the last time the output pin was toggled
long debounceDelay1 = 50; // the debounce time; increase if the output flickers
long lastDebounceTime2 = 0; // the last time the output pin was toggled
long debounceDelay2 = 50; // the debounce time; increase if the output flickers
int pot = 5; //temperature adjust input
const int set1 = 1;
const int set2 = 2;
int heat = 0;
int indicate = 4;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.print("SET: ACT: HEAT?"); // Print headings the LCD.
analogReference(INTERNAL);
pinMode(heat, OUTPUT);
pinMode(indicate, OUTPUT);
pinMode(set1, INPUT);
pinMode(set2, INPUT);
}
void loop()
{
State1 = digitalRead(set1);
State2 = digitalRead(set2);
if (State1 != lastState1) {
if (State1 == HIGH) { //if up button is pressed, add 1 to des
des++;
}
State2 = digitalRead(set2);
}
else if (State2 != lastState2) { //if down button is pressed, subtract 1 from des
if (State2 == HIGH) {
des--;
}
int reading1 = digitalRead(set1); //debounce buttons 1 and 2 (up and down)
if (reading1 != lastState1) {
lastDebounceTime1 = millis();
}
if ((millis() - lastDebounceTime1) > debounceDelay1) {
State1 = reading1;
}
int reading2 = digitalRead(set2);
if (reading2 != lastState2) {
lastDebounceTime2 = millis();
}
if ((millis() - lastDebounceTime2) > debounceDelay2) {
State2 = reading2;
}
lastState1 = reading1;
lastState2 = reading2;
}
analogRead(pin);
delay(5);
tempc = (100* (long)analogRead(pin)/1024.0); //save temperature reading as 'tempc'
delay(5); //gives a range of about 30
lcd.setCursor(1, 1);
lcd.print(" "); //clear field
lcd.setCursor(1, 1);
lcd.print(des); //print set temperature
lcd.setCursor(6,1);
lcd.print(" "); //clear field
lcd.setCursor(6,1);
lcd.print(tempc ); //print actual temperature
if(tempc <= des){ //if actual temperature is less than
digitalWrite(heat, HIGH); //or equal to the set temperature, turn the heater on
digitalWrite(indicate, HIGH);
lcd.setCursor(11,1);
lcd.print("ON ");
}
else if(tempc > des){ //otherwise leave the heater off
digitalWrite(heat, LOW);
digitalWrite(indicate, LOW);
lcd.setCursor(11,1);
lcd.print("OFF");
delay(100);
}
}
Thanks guys.