No reaction from DFrobot LCD keypad when button is pressed

Hello, I am having trouble with my LCD keypad, specifically that I have written the sketch and it is compiling with no errors, but the buttons are not displaying anything. I essentially just want to use the up and down to allow the user to be able to scroll on my menu page between 2 sensor options, temperature and heart rate. I have attached my code and even replaced the case of the down button to just print something just to individually test it but it is not printing anything. I would appreciate some help as to what my issue is. Thanks!

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

#define RIGHTbutton 0
#define UPbutton 1
#define DOWNbutton 2
#define LEFTbutton 3
#define SELECTbutton 4
#define NObutton 5

//buttons are connected by analog pin 0
double ButtonValues_adc = 0;
int ButtonValues_lcd = 0;



void setup() {
  // put your setup code here, to run once:
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("Sensor Options: ");
  lcd.setCursor(0, 1);
  lcd.print("> Temperature ");
  //lcd.setCursor(0, 2);
  //lcd.print("> Heart Rate");
  //delay(500);
}
void loop() {
  // put your main code here, to run repeatedly:

  static int col = 0;
  static int row = 0;
  //lcd.setCursor(0, 1);
  ButtonValues_lcd = LCD_ReadButtons();

  switch (ButtonValues_lcd) {
    case DOWNbutton:
      {
        lcd.blink();
        lcd.setCursor(col, row);
        break;
      }
    case UPbutton:
      {
        lcd.blink();
        lcd.setCursor(col, 0);
      }
    case RIGHTbutton:
      {
        lcd.setCursor(col++, row);
        break;
      }
    case LEFTbutton:
      {
        lcd.setCursor(col--, row);
        break;
      }
  }
  //delay(500);
}


int LCD_ReadButtons() {  //function that determines which button is pressed
  ButtonValues_adc = analogRead(0);

  if (ButtonValues_adc > 1000) return NObutton;
  if (ButtonValues_adc < 850) return SELECTbutton;
  if (ButtonValues_adc < 650) return LEFTbutton;
  if (ButtonValues_adc < 450) return DOWNbutton;
  if (ButtonValues_adc < 250) return UPbutton;
  if (ButtonValues_adc < 50) return RIGHTbutton;
  return NObutton;
}

You might want to think about the order of these statements... example a value of < 50... is also < 850... the < 850 if statement will trigger first and exit the routine.

You probably want to do this regardless of the button pressed.

Thank you so much that was what the issue was!
I was also wondering about something similar to a low-power mode and how that would work? Digital pin 10 controls the backlight of the lcd and I tried making an if statement before my void loop ends with these conditions where im basically saying if a certain amount of time has passed and no button has been pressed, to turn the backlight off and the second that if the backlight is off and a button is pressed for it to turn on, but I am not getting it to work. Thanks!

if(millis()==10,000 && ButtonValues_adc == 1023){
digitalWrite(BACKlight, LOW);
  }

  if(ButtonValues_adc<1000 && digitalRead(BACKlight) == LOW){
    digitalWrite(BACKlight, HIGH);
  }

This probably doesn't do what you want. Firstly, you shouldn't have a comma (,) in the 10000.

Secondly, even if that syntax was correct, the statement will only be true for a single millisecond.

What you need to do is create another variable, say buttonPressed.

unsigned long buttonPressed = 0;

When any button is pressed you set buttonPressed to millis(), and also turn on the backlight;

Then in your main loop you can check...

if (millis() - buttonPressed > 10000)
  // Turn the backlight off.

So basically you are checking the amount of time that has passed since a button was pressed, if more than 10 seconds turn off the backlight.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.