LCD Keypad Shield button input - Thermostat with Relay

Good afternoon,

I found an old bit of code for controlling relays dependent on temperature on the internet, but it was created to use an older LCD Screen without the analog buttons (a0) attached. I figured my LCD screen has these buttons so I might as well use them.

I can get the buttons to work at their respective variables below using a simple button test , press left for Left, right for right etc.

I cannot get the “setpoint” portion (full code below):

//Get user input for setpoints  
  if(analogRead(btnDOWN)==LOW)
  
  { 
    if(SetPoint>0)
    {
      SetPoint--;    
    }
  }
  if(analogRead(btnUP)==LOW)
  {
    if(SetPoint<150)
    {
      SetPoint++;
    }
  }

to cooperate with the analog buttons, but if I hook up digital buttons to say pins 10 and 11, and change the code to use them, that works:

Code:

  // https://www.dfrobot.com/wiki/index.php/LCD_KeyPad_Shield_For_Arduino_SKU:_DFR0009

//**************************************************************************************/

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);           // select the pins used on the LCD panel

// define interface pins:
const int LED_RED=10; //Red LED
const int LED_GREEN=11; //Green LED
const int RELAY=12; //Lock Relay or motor

// Value for Default Setpoint (temp for Relay):
int SetPoint=40;

// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;

#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5

int read_LCD_buttons(){               // read the buttons
    adc_key_in = analogRead(0);       // read the value from the sensor 

    // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
    // we add approx 50 to those values and check to see if we are close
    // We make this the 1st option for speed reasons since it will be the most likely result

    if (adc_key_in > 1000) return btnNONE; 

    // For V1.1 us this threshold
    if (adc_key_in < 50)   return btnRIGHT;  
    if (adc_key_in < 250)  return btnUP; 
    if (adc_key_in < 450)  return btnDOWN; 
    if (adc_key_in < 650)  return btnLEFT; 
    if (adc_key_in < 850)  return btnSELECT;   

    return btnNONE;                // when all others fail, return this.
}

//=================================================================
//                  SETUP
//=================================================================

void setup(){
pinMode(LED_RED,OUTPUT);
  pinMode(LED_GREEN,OUTPUT);  
  pinMode(RELAY,OUTPUT);  
  
  //Pull up for setpoint keys
  digitalWrite(btnUP,HIGH);
  digitalWrite(btnDOWN,HIGH);
  
   // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Temp Sensor Reset");  
  lcd.setCursor(0,1); //Move coursor to second Line
  lcd.print("Temp. Controller");
  digitalWrite(LED_GREEN,HIGH);  //Green LED Off
  digitalWrite(LED_RED,LOW);     //Red LED On
  digitalWrite(RELAY,LOW);       //Turn off Relay
  delay(2000);
}

//=================================================================
//                  LOOP
//=================================================================
 
void loop(){
  double Temperature = ((5.0/1024.0) * analogRead(A0)) * 100;  //10mV per degree 0.01V/C. Scale
 
  lcd.setCursor(0,0);
  lcd.print("Temperature:");    //Do not display entered keys
  lcd.print(Temperature);
  
//Get user input for setpoints  
  if(analogRead(btnDOWN)==LOW)
  
  { 
    if(SetPoint>0)
    {
      SetPoint--;    
    }
  }
  if(analogRead(btnUP)==LOW)
  {
    if(SetPoint<150)
    {
      SetPoint++;
    }
  }
 
//Display Set point on LCD
  lcd.setCursor(0,1);
  lcd.print("Set Point:");
  lcd.print(SetPoint);
  lcd.print("C   ");
 
//Check Temperature is in limit
if(Temperature > SetPoint)
{
   digitalWrite(RELAY,LOW);    //Turn off heater
   digitalWrite(LED_RED,LOW);
   digitalWrite(LED_GREEN,HIGH);  //Turn on Green LED
}
else
{
  digitalWrite(RELAY,HIGH);    //Turn on heater
  digitalWrite(LED_GREEN,LOW);
  digitalWrite(LED_RED,HIGH);  //Turn on RED LED  
}
 
  delay(100); //Update at every 100mSeconds
}
//=================================================================

It’s probably something simple I am overlooking, but if I can please get some fresh eyes and opinions on this, it would really help.

Thank you!

Looks like you are not using the read_LCD_buttons() function…
I think the code should look more like this…

//Get user input for setpoints  
  if(read_LCD_buttons() == btnDOWN)
   { 
    if(SetPoint>0)
    {
      SetPoint--;    
    }
  }
  if(read_LCD_buttons() == btnUP)
  {
    if(SetPoint<150)
    {
      SetPoint++;
    }
  }

You have got in a tangle with how to read a button value and how to read the sensor

To read the buttons you should use analogRead(0) and compare the value returned with a range of values to determine which button is pressed. Something like this

int iGetLCDButtonState()
{
  int iADC_Value;
  int iADC_Button_State = E_LCD_BTN_NONE;

  iADC_Value = analogRead(0);      /* Read the ADC */


  /* If ADC reads above 1000 then assume no buttons have been pressed */
  if (iADC_Value > 1000) 
  {
    iADC_Button_State = E_LCD_BTN_NONE;
  } 
  else /* If it was below 1000 then a button is pressed... */
  {

    /* If ADC reads between 0 & 49, then the RIGHT button has been pressed */
    if (iADC_Value < 50)             
    {
      iADC_Button_State = E_LCD_BTN_RIGHT; 
    }
    else
    {
      /* If ADC reads between 50 & 194, then the UP button has been pressed */
      if (iADC_Value < 195)
      {      
        iADC_Button_State = E_LCD_BTN_UP;      
      }
      else
      {
        /* If ADC reads between 195 & 379, then the DOWN button has been pressed */
        if (iADC_Value < 380) 
        { 
          iADC_Button_State = E_LCD_BTN_DOWN;
        }
        else
        {
          /* If ADC reads between 380 & 555, then the LEFT button has been pressed */
          if (iADC_Value < 555)  
          {
            iADC_Button_State = E_LCD_BTN_LEFT;
          }
          else
          {
            /* If ADC reads between 555 & 789, then the SELECT button has been pressed */
            if (iADC_Value < 790)  
              iADC_Button_State = E_LCD_BTN_SELECT;  
          }
        }
      }
    }
  }
  return iADC_Button_State;
}

The sensor must not be connected to analogue pin 0 as that is used by the buttons

Thank you for both answers, but as a direct fix, "read_LCD_buttons" was defined and then not used. Swapping out the "analogread" for "read_LCD_buttons" did the trick. Facepalm

I noticed from the schematics that A0 had all of the buttons attached and that is was reading resistance to determine the 6 buttons, found this interesting as it's the first time I have seen this.

I caught what you mean Heli, the spliced code is trying to find the temp input on A0, I have moved it to A1 in my code.

Thank you two for the help!