Hi,
I have posted this issue in the Project Guidance forum as part of the greater project of building a spa pool controller but have had no replies and i really need some help.
I am working on building a new spa pool controller, as part of this project I am trying to get elements of the overall code working, the final project will pull what I have learned together. I have encountered a problem that I can't solve. While the code is not finialised in any sense for the project as a whole I'm hoping someone can help me with this specific problem.
The code started with simply trying to set a desired comfort temperature of the spa that is set by a keypad. I used the 1602 shield with integrated buttons for ease but the final project probably will use a different LCD.
The code is prominently about setting and using the buttons which move the "ComfTemp" up and down. As part of the code I added in a "Current Temperature" which is run off a LM35 connected to A5.
If I run the code with the up and down section commented out so it only shows the current temperature it works fine and shows 21 deg (room temp here).
If I run the code that has the comfort temp only, the up and down works fine.
If I run both parts in the same loop the up and down works fine but the current temp shows 40 deg + .
Can anyone see in my code why the current temp would be inflated, I apologise in advance if it is a bit messy as I have been hacking it around to make it work?
Cheers
Al
#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
int ButtonsPin = A0;
int comfTemp = 40; // default spa temp
boolean lcdNeedsUpdate = true;
int tempPin = A5; // the output pin of LM35
int curTemp = 0; // defines current spa temp
long lastread = 0; // 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() >= lastread + 10000) { // check if last temp reading was greater than 10 sec
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;
}
}