ObjectButton is an Arduino library for detecting common button actions. Because of it, you can map different functions into a single button, i.e. turn on the light on a button click and play an alarm sound on double-click.
This library was inspired by an existing Arduino library, OneButton. It allows you to attach a callback function to a specific action. This callback function is basically a pointer to a function. Following approach works great for C-like functions, but not for C++ object member functions. Also, if your function gets called, it has no idea, which button called it.
To sum up:
- Only C-like functions can be called back
- Function does not know, which button called it
- If you have more buttons, neccesity to map actions to different functions
Approach used in this library was inspired by Android. Instead of passing a function callback, you'll implement listeners inside your class and pass a reference to your object. Our library can call all the functions defined in the listener, no need to pass callbacks to individual functions anymore! Also, each callback function has one input parameter - a reference to ObjectButton
instance. This way we can differentiate, which button produced an event and have code for all the buttons in one place.
Supported listeners:
Check out the examples for an inspiration.
At the moment, following actions are detected:
- Click
- Double-click
- Button press
- Button release
- Button long press
- Button release after a long press
At the moment, we suppport both analog and digital buttons:
- Each digital button needs to be connected to a unique pin
- You can distinguish digital buttons based on their button ID, which matches pin number
- Analog buttons can share the same pin. We detect actions based on different voltage levels
- In order to distinguish analog buttons among themselves, each button requires to set up a button ID as a mandatory parameter. It's an arbitrary number from 0 - 255, and it should be unique. If you use the same button ID on two analog buttons, your code will work, but you won't distinguish, which button triggered an action. At the moment, we don't validate user input. You should know what you're doing.
Note: Detection of simultaneous press of two analog buttons sharing the same pin is not supported.
Sensors are rebranded buttons; they have exactly the same functionality. While buttons react to click, double-click, and press actions, sensors react to motion or other visual changes.
Note: While we defined sensor types, listeners stay the same. It means you can hook up onClickListener to a sensor. Maybe we'll think of a more elegant solution in the future.
This library comes with reasonable defaults for detecting all the actions. However, you can tweak the values to better fit your project. First, create a new ObjectButton
instance. Then, call one of these functions:
setDebounceTicks()
to adjust the debounce interval for more reliable pattern recognitionsetClickTicks()
to adjust the time to detect a click actionsetLongPressTicks()
to adjust the time to detect a long press action
There are many ways in which you can participate in the project, for example:
- Submit bugs and feature requests, and help us verify as they are checked in
- Review source code changes
- Review the documentation and make pull requests for anything from typos to new content
Please read contributing rules for more details.
Copyright (c) JSC electronics. All rights reserved.
Licensed under the Apache-2.0 license.
We hope our library helped to speed up your project development. You can support our effort to convert coffe and pizza into a code with a small donation. Any support is much appreciated.