Simplifies coding for
- Single and Multiple taps
- Short presses and Long presses
- Auto-repeat
- Use with almost any GPIO pin, without the need for interrupts
- Active LOW or active HIGH switches
- Debouncing to remove switch noise
- Cleaner code as each button has its own data storage
- Fully customisable settings
At at absolute minimum, it will save you writing debounce code for each button.
But it can do a lot more - it's ideal for any device using single taps or multi-taps, or even a 10 second press to reboot a device, or perhaps all of these things using only one button.
It can also simplify coding if you have several buttons, and has the advantage that each button contains its own settings and data.
If you have a button which pulls GPIO2 LOW when pressed, and you want to define it as 'button1' you can use
MultiTapButton button1(2,LOW);
To check if the button was tapped, use if(button1.tapped()){...your code here}
For multiple taps, you can find the number of taps with int x = button1.tapCount();
Tip : x can be the button's internal variablebutton1.userIntA = button1.tapCount();
To check if a button is currently down use
if(button1.down()){...your code here}
To check if a button was just pressed (switch closed) use if(button1.downEvent()){...your code here}
To check if a button just released (switch opened) use if(button1.upEvent()){...your code here}
To check how long the button has been down, use unsigned long x = button1.downMillis();
Tip : x can be the button's internal variablebutton1.userUlongA = button1.downMillis();
Hold down the button for more than a second to start auto-repeating taps.
To enable auto-repeat
button1.autoRepeatEnabled(true)
To disable auto-repeat
button1.autoRepeatEnabled(false)
To check if auto-repeat is enabled
if(button1.autoRepeatEnabled()){...your code here}
To set the delay to 1500ms before auto-repeat kicks in, followed by a repeat every 300ms
button1.autoRepeatConfig(1500,300);
the default is button1.autoRepeatConfig(1000,250)
;
For ease of coding I have provided each button with a set of general purpose variables that you can use for any purpose you like. You can use the integers as counters, booleans for toggling things on/off, and unsigned
longs for large numbers such as milliseconds
These bonus variables can be addressed
using the following code (assuming the switch is named 'button1')
button1.userIntA
button1.userIntB
button1.userBoolA
button1.userBoolB
button1.userULongA
button1.userULongB
As you may have noticed, the minimum you need to create a MultiTapButton is two parameters, the GPIO port number and the active level.
If GPIO2 is pulled LOW when operated (default) -
MultiTapButton button1(2, LOW);
If D4 is pulled HIGH when operated -
MultiTapButton button1(D4, HIGH);
'Noisy' contacts may require extra debounce time. The third parameter here sets debounce time to 20ms (the default is 10ms)
MultiTapButton button1(2, LOW, 20);
Sometimes you may want to set the maximum tap period (after which it becomes a press). The forth parameter here sets it to 400ms (default is 500ms)
MultiTapButton button1(2, LOW, 20, 400);
You may want to set the inter-tap gap (after which it returns the number of taps). The last parameter here sets it to 200ms (default is 250ms)
MultiTapButton button1(2, LOW, 20, 400, 200);
To set the delay to 1500ms before auto-repeat starts (default is 1000ms), and the interval of auto taps to 200ms (default is 250ms)
button1.autoRepeatConfig(1500, 200)
Ensure you check the button regularly in your code as the timing accuracy depends on refreshing the button's state. In practice you should have a fast non-blocking loop and check the button in the loop() function. Avoid using delay(..)
as it blocks the program flow.