rlogiacco / AnalogButtons

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

Fix for when Analog Button value is Zero #10

Closed fraser125 closed 7 years ago

fraser125 commented 7 years ago

Unless both values in an if statement are signed, the type is assumed to be unsigned. https://en.wikipedia.org/wiki/Type_conversion#Implicit_type_conversion

When the button[i].value - margin goes negative this comparison will fail because the result instead of being a "minimum" value rolls around and becomes larger than the max value. Casting these 2 values to int16_t fixes this issue.

Using the default margin and the maximum valid button.value would result in 1033. Since the reading value should never exceed 1023 the Max comparison and only through developer error would it exceed the capacity of the uint16_t, therefore it shouldn't have this problem.