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!