I am trying to adapt a alarm clock sketch that used external buttons, but I would like to utilize a LCD Keypad Shield. The data sheet says all the buttons are on pin A0, so every button is "set time" in the original sketch.

After reading this post

I found the values when I press each button, :

Right    0
Up     100
Down   257
Left   410
Select 640 

and have tried to define them by modifying the existing code:

LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //Arduino pins to lcd
#define bt_time   A0
#define bt_up     A1
#define bt_down   A2
#define bt_alarm  A3


LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //Arduino pins to lcd
int lcd_key     = 0;
int adc_key_in  = 0;
#define bt_time   0
#define bt_up     1
#define bt_down   2
#define bt_alarm  3

#define buzzer 10

int read_LCD_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
 if (adc_key_in < 50)   return bt_time;  
 if (adc_key_in < 150)  return bt_up; 
 if (adc_key_in < 307)  return bt_down; 
 if (adc_key_in < 690)  return bt_alarm; 


I got this from here

It doesn't work.... when you press the buttons nothing happens.

It up loads with no errors, but the buttons don't do anything.

Please help, your my only hope.

Edit: The linked "Real_Time_Clock_With_Alarm.ino" is the source file, all I have changed is the above code. It works except I can't interact with it... because no buttons.... I have also tested the hardware with other sketch and it works as expected.

  • What do you return if no button is pressed? And how can it ever be not less than 4600 when the ADC gives you 0-1023 (or 0-4096 on some better boards)?
    – Majenko
    Jun 15, 2022 at 12:31
  • To true, typo it should have been 690 not 4600..... Jun 15, 2022 at 12:35
  • 1
    you code says analogRead(0) ... is that same as analogRead(A0)?
    – jsotola
    Jun 15, 2022 at 16:14
  • 1
    @jsotola In short, yes. In more detail: Those shields are a 5V design that practically work with the UNO/Leonardo/Mega boards, all of which use a wiring_analog.c implementation that behaves the same for A0 and 0.
    – timemage
    Jun 15, 2022 at 19:02
  • 2
    In your current revision, you're checking < 307 and then < 690 but two of your observed readings 410 and 640 sit between these numbers.
    – timemage
    Jun 15, 2022 at 19:10


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.