Hi,
I have managed to figure my way through making a simple up/down to set the desired spa temp. using the code from post 8 as mentioned above. I have changed some of the code to use standard coding terms out of the reference, even though the way it was done was probably more efficient.
Although this is more of a programming question than a Project Guidance I though I would post here.
So I have a 1602 shield with the 5 buttons. I have made a simple up/down set between 36 and 42. I thought I would be clever and add a second line with the current temperature. his is run of a simple LM35 connected to A5 pin.
My issue is if I run the current temperature part of the code it works fine and displays the current temp (currently 21 deg here) and updates every minute on line one of the LCD, if I put my finger on it it will increase to 30 deg
if I run the code for the adjustable desired temperature it displays correctly on line 2 and can be adjusted up or down as desired,
As soon as I have both running in the same loop the top line immediately reads 41 and putting my finger on it it goes to 57. Somehow it seems the comftemp and curTemp seem to be mixed up.
I have been tackling this all afternoon and can't work it out. Hopefully someone can see my error.
PS this is not how the final project will look, simply just trying to get these two components working.
Code is below
#include <LiquidCrystal.h> // Library for 1602 LCD
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // set the pins for the dispay, if using shield these are standard
#define btnNONE 0
#define btnSELECT 1
#define btnLEFT 2
#define btnUP 3
#define btnDOWN 4
#define btnRIGHT 5
#define NUM_KEYS 6
const byte ButtonsPin = A0;
int comfTemp = 40; // default spa temp
boolean lcdNeedsUpdate = true;
int tempPin = A5; // the output pin of LM35
int curTemp; // defines current spa temp
long lastread; // to allow reading of spa temp once per minute
int read_LCD_buttons() // returns a value from 0-6 using the define list matching the button to corrosponding value
{
int returnValue;
// read ADC value of pressed button
int adc_key_in = analogRead (ButtonsPin); // reads the value from A0 to determin button
int adc_key_in1 = analogRead (ButtonsPin); // reads the value again, used to check the value is constant
// read again and check for stable ADC reading (software debouncing for analog input)
if (abs(adc_key_in1 - adc_key_in) > 1) return btnNONE; // if ADC reading is not stable, return btnNONE
// below values for the buttons coneected to A0 may vary depending on the sheild used, these need to be worked out prior
if (adc_key_in < 50) returnValue = btnRIGHT;
else if ( (adc_key_in > 95) && (adc_key_in < 110) ) returnValue = btnUP;
else if ( (adc_key_in > 250) && (adc_key_in < 350) ) returnValue = btnDOWN;
else if ( (adc_key_in > 400) && (adc_key_in < 450) ) returnValue = btnLEFT;
else if ( (adc_key_in > 630) && (adc_key_in < 700) ) returnValue = btnSELECT;
else returnValue = btnNONE;
// simple "blocking" code: "Busy waiting" until button is released by user
while (adc_key_in < 999) adc_key_in = analogRead(ButtonsPin);
return returnValue;
}
int readTemp() // get the temperature and convert it to celsius
{
curTemp = analogRead(tempPin);
return curTemp * 0.48828125;
}
void setup()
{
Serial.begin(9600); // begin serial monitor so you can view the output of selected temp
lcd.begin(16, 2); // initialise the display
lcd.clear(); // clear the display
lcd.print ("Current Temp:"); // will display current temp of spa
lcd.setCursor (0, 1);
lcd.print ("Comfort Temp:"); // will display desired temp
pinMode(tempPin, INPUT); // defines temp pin as input from LM35
}
void loop()
{
if ((millis() - 60000) >= lastread) { // check if last temp reading was greater than 1 min
curTemp = readTemp(); // get the temperature
lcd.setCursor(14, 0); // adds the current temp to the end of the first line
lcd.print(curTemp); // should print onto line one at end
lastread = millis(); // set the lastread of sensor
}
int key = read_LCD_buttons();
if (key != btnNONE) lcdNeedsUpdate = true;
switch (key)
{
case btnUP:
if (comfTemp < 42) comfTemp++;
break;
case btnDOWN:
if (comfTemp > 36) comfTemp--;
break;
case btnSELECT:
Serial.print("Saved value: ");
Serial.println(comfTemp);
Serial.println(curTemp);
break;
}
if (lcdNeedsUpdate)
{
lcd.setCursor(14, 1); // should print onto line 2
lcd.print(comfTemp);
lcdNeedsUpdate = false;
}
}