LCD Button Shield and Detecting Button Holds

Hello,

I am using the classic LCD shield with buttons, where the buttons are cascaded through a resistor network and an analog signal is then read by the Arduino. These shields have become universal and all vendors use the same code to detect which buttons are pressed. Refer to the link to the shield below.

More or less the function provided is usually this:

int read_LCD_buttons()
{
  adc_key_in = analogRead(A0);      // read the value from the sensor
//  Serial.println(adc_key_in);
  delay(5); //switch debounce delay. Increase this delay if incorrect switch selections are returned.
  int k = (analogRead(0) - adc_key_in); //gives the button a slight range to allow for a little contact resistance noise
  if (5 < abs(k)) return btnNONE;  // double checks the keypress. If the two readings are not equal +/-k value after debounce delay, it tries again.
  // 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 > 530) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
  if (adc_key_in < 50)  return btnRIGHT;  
  if (adc_key_in < 150)  return btnUP; 
  if (adc_key_in < 250)  return btnDOWN; 
  if (adc_key_in < 400)  return btnLEFT;   
  if (adc_key_in < 510)  return btnSELECT; 
  return btnNONE;  // when all others fail, return this...

I am trying to change this function to detect button holds as well. I have tried implementing tricky millis() resetting but I haven't been able to do so.

Some suggestions would be appreciative.

Thank you!

dgelman:
adc_key_in = analogRead(A0); // read the value from the sensor

int k = (analogRead(0) - adc_key_in); //gives the button a slight range to allow for a little contact resistance noise

is analogRead(0) same as analogRead(A0)?

value int k always have "0".

iroiroys,

I don't think so, the buttons are working just fine, so I am assuming your answer is no.

If adc_key_in is greater than 530, then record the current millis or micros time. Now using a method similar to Blink Without Delay, you see if the value is the same after a certain amount of time has passed.

If that certain amount of time has passed and the button did not change, then you can consider that button is HELD.

I have a small library that I wrote to handle my analog Buttons on the LCD shield that implements an auto repeat if you hold the button down. This shows the technique for detecting the long press. You can find the library in my code repository linked in the signature block.