I use analog buttons on my products, to save pins. I have a simple library I use to do the grunt work. It allows the caller to specify the A/D pin to use, as well as the voltage ranges corresponding to each button on that pin. Using this, at least 8 or more buttons can be connected to a single pin, using a resistor network to ensure each button, when pressed, results in a unique voltage on the A/D pin.
The user can also specify an integer "button code" to be associated with each button.
The library does debounce, to eliminate false events. In addition to reporting button presses several "gestures" are recognized:
SHORT PRESS - this is a quick press and release of a button. The minumum duration of the press is the debounce interval (50 mS by default). The maximum duration of the press is 250mS.
DOUBLE PRESS - this is two quick, back-to-back SHORT PRESSES on a single button. Press and release timing is identical to SHORT PRESS. The maximum period between the two presses is the same as the SHORT PRESS maximum duration
LONG PRESS - this is a press-and-hold of a button. The minimum hold time is twice the SHORT PRESS maximum duration, or 500 mS.
An optional REPEAT mode can be enabled which will cause REPEAT events to be generated at four evennts/second between the time a LONG PRESS event is reported, and the button is released.
The ReadButtons function must be called periodically in the loop() function. Use of delay() with long periods will very likely result in missed events. ReadButtons will return a structure which contains an event type, and a button code. The button code is the user-provided code for the button that generated the event.
The event types are:
BUTTON_EVENT_NONE - NO button events to report
BUTTON_EVENT_PRESSED - A button has been pressed
BUTTON_EVENT_SHORTPRESS - A button has been SHORT pressed
BUTTON_EVENT_LONGPRESS - A button has bene LONG pressed
BUTTON_EVENT_REPEAT - A button has been LONG pressed, and is being held
BUTTON_EVENT_RELEASED - A button has been released
BUTTON_EVENT_DOUBLEPRESS - A button has been DOUBLE pressed
In most applications, only BUTTON_EVENT_SHORTPRESS, and perhaps BUTTON_EVENT_DOUBLEPRESS will be used. If BUTTON_EVENT_LONGPRESS is used, then BUTTON_EVENT_RELEASED should also be used to tell when the button has been released.
The event sequences are:
SHORT PRESS:
BUTTON_EVENT_PRESSED
BUTTON_EVENT_SHORTPRESS
LONG PRESS:
BUTTON_EVENT_PRESSED
BUTTON_EVENT_LONGPRESS
BUTTON_EVENT_REPEAT ==> Only issued if REPEAT is enabled
BUTTON_EVENT_RELEASED
DOUBLE PRESS:
BUTTON_EVENT_PRESSED
BUTTON_EVENT_SHORTPRESS
BUTTON_EVENT_PRESSED
BUTTON_EVENT_DOUBLEPRESS
Note that rollover is NOT supported!! pressing multiple buttons will result in erroneous event reports unless your hardware configuration takes rollover into account.
There is a Test directory in the ZIP file which contains a simple test program that demonstrates how to use the library. You will probably want to re-define the Buttons structure in the test program to match your hardware configuration. The configuration I used here is described in the file.
The library is below. I won't guarantee this is 100% bug-free, but I believe it is.
Regards,
Ray L.
NewAnalogButtons.zip (4.5 KB)