rlogiacco / AnalogButtons

Arduino library to wire multiple buttons to one single analog pin
GNU Lesser General Public License v3.0
57 stars 14 forks source link

Button ADC Value is Zero #5

Closed fraser125 closed 7 years ago

fraser125 commented 7 years ago

I'm using a fairly common LCD + Button shield designed by DFRobot and available many other places. It has 5 buttons on A0 analog pin.

The problem I seem to be having is with the Right button not firing it's press or hold functions. I believe the problem is because the right button has an ADC reading of 0 (ZERO). When it gets to the following line: if (reading >= buttons[i].value - margin && reading <= buttons[i].value + margin) { The comparison fails with the following values being tested reading:0 buttons[i].value - margin : 65526 buttons[i].value + margin : 10 By changing a couple of variable definitions to support unsigned integers seems to fix it.

Change the following lines, sorry I don't know how to make a pull request :(

void AnalogButtons::check() {
/* other misc lines */
      uint16_t reading = analogRead(pin);

to int16_t reading = analogRead(pin);

AND

class Button {
public:
    uint16_t value;

to int16_t value;

I got the following values which fixed my problem. reading:0 buttons[i].value - margin : -10 buttons[i].value + margin : 10 I'm interested in feedback on this code change, since I'm not much of a C programmer.

fraser125 commented 7 years ago

Looks like I figured out how to make a pull request on Github :)

rlogiacco commented 7 years ago

After merging your latest pull request I've applied another change which should achieve the same result without impacting the library performances.

Will you be so kind to double check it works for you?

Thanks

UPDATE Forget about it, my improvement doesn't work and I have already reverted it.