I am using a d1 robot lcd keypad shield (Arduino_LCD_KeyPad_Shield__SKU__DFR0009_-DFRobot), but I am having some issues from time to time with the analog buttons. While basically they are working, some times when pressing one of the buttons, I get a signal for a different button. E.g. I am pressing the down button, however I get the signal for the select button.
I have already implemented a full menu control, and its really annoying that sometimes control jumps to a submenu or triggers actions unwantedly.
As for the code for button detection, I am using 100% their code on the homepage
// read the buttons
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 > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
// 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;
return btnNONE; // when all others fail, return this...
}
I also have some checks for button change (so I don't get repeated signals of the same button etc.), but right now I am not at home, so I can not provide a minimal working example which will reproduce the issue (will hand it in in the evening).
However I was hoping that this was some kind of common problem which was already solved by other people.
OK, the trick is that the resistance values for the buttons may be a bit marginal.
You need to write a simple code that tells you what the actual analog value is returned for each button, by measuring it every 200 ms and printing it to the serial monitor.
Then firstly see whether there is any gross unsteadiness in the values you see (which means you have a bigger problem), and if not, adjust the threshold numbers in the above code to be halfway between the actual values read from the buttons.
Paul__B:
OK, the trick is that the resistance values for the buttons may be a bit marginal.
You need to write a simple code that tells you what the actual analog value is returned for each button, by measuring it every 200 ms and printing it to the serial monitor.
Then firstly see whether there is any gross unsteadiness in the values you see (which means you have a bigger problem), and if not, adjust the threshold numbers in the above code to be halfway between the actual values read from the buttons.
Get the idea?
Okay, I'll try that out. So there is bigger difference from time to time between the different devices, although same manufacturer & model? I just thought I'd pick up their code and be good.
But no problem, I'll try to measure the correct values, lets see if it gets better.
Why do you specifically mention 200ms as interval for measuring?
BTW: sorry, a bit newbie in arduino and all that, thats my first project with it.
s710:
Why do you specifically mention 200ms as interval for measuring?
Five times a second is a reasonable rate at which you can discriminate the numbers as they scroll up the screen. It will probably be better to use println rather than just print so each is on a new line.