Ignore Buttons being continuously pressed (on LCD Keypad Shield)


I have the LCD Keypad Shield with the 5 Buttons on it, and I’m using these to go through a menu.

Now my problem is, how do I ignore, if I keep the Button continously pressed?
Can I check for rising or falling edges?

The current code to read out which button is pressed is this one (taken from here: http://www.dfrobot.com/wiki/index.php?title=Arduino_LCD_KeyPad_Shield_(SKU:_DFR0009)):

byte read_LCD_buttons()
  if (millis() % 600){
    adc_key_in = analogRead(0);      // read the value from the sensor 
    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;  

 if (adc_key_in < 50)   return btnRIGHT;  
     if (adc_key_in < 195)  return btnUP; 
     if (adc_key_in < 380)  return btnDOWN; 
     if (adc_key_in < 555)  return btnLEFT; 
     if (adc_key_in < 790)  return btnSELECT;   
  return btnNONE; 

Which causes, if I keep the button pressed, continously runs through a menu. Using a delay is a NoGo, because I have somewhat time-critical application running on the Leonardo, and the current solution by using the milli() % 600 is not that good either, because sometimes button presses get ignored.

Can I check for rising or falling edges?

Each time you read the input compare it with the previous value that you saved last time you read it.

See the StateChangeDetection example in the IDE.


I just now have to do some kind of fuzzy compare, because returned value while a button is pressed can fluctuate by about 2 - 3 ADU, something of the sort (abs(oldVal - newVal) < 3)).

You are thinking along the right lines. It is easier of course with digital inputs but you should be able to do it as you suggest but you may need to be generous with range that you allow.

Another way to do it would be to change a flag variable to true when the button is first pressed and you carry out the actions associated with the button. Next time you find that the button is pressed only act if the flag variable is false. When the button is released set the flag to false.

What is the point in comparing the analog values? Would be best I think to compare based on the output of read_LCD_buttons...

You will probably still need some debouncing logic on top, but I am unfamiliar with the shield...