![]() |
ButtonToSwitch Library for Arduino v4.0.1
A library that provides elaborated switch mechanism behavior simulation for digital signals inputs
|
This Arduino library is a refactoring of my own ButtonToSwitch_STM32 (RTOS) and ButtonToSwitch_ESP32 (RTOS-Arduino) libraries. For making this possible this library depends on the TimerOne library and supports every platform suported by that library. Several forks of the library exist to support different architectures, including ESP32 boards, Arduino Uno-R4, and others.
By using just a push button (a.k.a. momentary switches or momentary buttons, MPB for short from here on) the classes implemented in this library will manage, calculate and update different parameters to generate the behavior of standard electromechanical switches. Those parameters include presses, releases, timings, counters or secondary input readings as needed.
The instantiated switch state is calculated independently by the use of the timer, that keeps the state of the objects created constantly updated including the ON/OFF condition, without constant polling needed. The timer setup is managed by in-class methods, including the possibility to pause, resume or end the timer of each object independently of the others.
Each class offers a wide range of methods to set, read and modify every significant aspect of each switch mechanism simulated, and the signal received from the push button is debounced for a correct behavior of the event processing.
The benefits of the use of those simulated switches mechanisms are not just economic, as push buttons come in a wide range of prices and qualities as the simulated hardware switches come.
In any device powering on/off project, for example, detecting after the implementation, through the daily use, that an installed switch was not the best choice when a physical switch is involved, requires for the correction to a best suited kind of switch a bunch of activities to get the change of the switch unit done:
While with the simulated switches this situation might be solved by just changing the object class, modifying the instantiation parameters or calling a method if needed, and the switch is already changed!!
In an Industrial production machines environment the operator's physical security policies enforcement implies that no time can be wasted and the switch must fit perfectly it's purpose, while still giving the chance to change it's working parameters to adjust the mechanism to changing production operations, or to use the security device in other kind of machine. In this case the development depending on a simple "OK to proceed"/"The security device is correctly activated" might become a dangerous issue if not correctly isolated and solved as a remove and replace solution. The security switches must comply with issues as:
Just to add possibilities, consider that everywhere the "Momentary Push Button" is mentioned, any kind of momentary digital activation signal provider might be used instead: touch sensors, PIR sensors, RFID signals, fingerprint reader and so on...
All classes provide several communication mechanisms to keep it's output states available by request and by having their value change automatically notified.
Those mechanisms include:
Those listed mechanisms are independent, so one or more might be simultaneously used according to implementation needs and convenience.
Method | Parameters |
---|---|
**_DbncdMPBttn_** | None |
**_DbncdMPBttn_** | uint8_t mpbttnPin(, bool pulledUp(, bool typeNO(, unsigned long int dbncTimeOrigSett))) |
begin() | (unsigned long int pollDelayMs) |
clrStatus() | (bool clrIsOn) |
disable() | None |
enable() | None |
end() | None |
getCurDbncTime() | None |
getFnWhnTrnOff() | None |
getFnWhnTrnOn() | None |
getIsEnabled() | None |
getIsOn() | None |
getIsOnDisabled() | None |
getOtptsSttsPkgd() | None |
getOutputsChange() | None |
getStrtDelay() | None |
init() | uint8_t mpbttnPin(, bool pulledUp(, bool typeNO(, unsigned long int dbncTimeOrigSett))) |
pause() | None |
resetDbncTime() | None |
resetFda() | None |
resume() | None |
setDbncTime() | unsigned long int newDbncTime |
setFnWhnTrnOffPtr() | void* fnWhnTrnOff |
setFnWhnTrnOnPtr() | void* fnWhnTrnOn |
setIsOnDisabled() | bool newIsOnDisabled |
setOutputsChange() | bool newOutputChange |
The reasons to add the delay are design related and are usually used to avoid registering unintentional presses, or to give some equipment that needs time between repeated activations the benefit of the pause. If the push button is released before the delay configured, no press is registered at all. The delay time in this class as in the other classes that implements it, might be zero (0), defined by the developer and/or modified in runtime.
Method | Parameters |
---|---|
**_DbncdDlydMPBttn_** | None |
**_DbncdDlydMPBttn_** | uint8_t mpbttnPin(, bool pulledUp(, bool typeNO(, unsigned long int dbncTimeOrigSett(, unsigned long int strtDelay)))) |
init | uint8_t mpbttnPin(, bool pulledUp(, bool typeNO(, unsigned long int dbncTimeOrigSett(, unsigned long int strtDelay)))) |
setStrtDelay() | (unsigned long int newStrtDelay) |
This is an Abstract Class meaning that no object can be instantiated from it. The members defined are meant to be available for the LtchMPBttn subclasses instantiated objects, and define common behavior for all the "Latched Button Subclasses".
The un-latching mechanisms include but are not limited to:
The different un-latching events defines the sub-classes of the LDD-MPB class.
Attention: The range of signals accepted by the instantiated objects to execute the unlatch process is diverse, and their nature and characteristics might affect the expected switch behavior. While some of the signals might be instantaneous, meaning that the start of the unlatch signal is coincidental with the end of the unlatch signal, some others might extend the time between both ends. To accommodate the logic required by each subclass, and the requirements of each design, the **_unlatch_** process is then split into two stages:
The class provides methods to generate those validated signals independently of the designated signal source to modify the instantiated object behavior if needed by the design requirements, Validated Unlatch signal (see LtchMPBttn::setUnlatchPend(const bool) ), Validated Unlatch Release signal (see LtchMPBttn::setUnlatchRlsPend(const bool) ), or to set both flags to generate an unlatch (see LtchMPBttn::unlatch() ).
Method | Parameters |
---|---|
getIslatched() | None |
getTrnOffASAP() | None |
getUnlatchPend() | None |
getUnlatchRlsPend() | None |
setTrnOffASAP() | bool newVal |
setUnlatchPend() | bool newVal |
setUnlatchRlsPend() | bool newVal |
unlatch() | None |
So this simulates a simple On-Off switch like the ones used to turn on/off a room light, or any electronic device. There's a lot of advantages in software simulated switches: any amount of switches might be set up in a parallel configuration, so that an unlimited number of entrances or easy accessible points can each have a switch to turn on/off the same resource, the switch might be temporarily disabled, either keeping the On State or the Off State, and some more.
Method | Parameters |
---|---|
**_TgglLtchMPBttn_** | uint8_t mpbttnPin(, bool pulledUp(, bool typeNO(, unsigned long int dbncTimeOrigSett(, unsigned long int strtDelay)))) |
The Service time is set at instantiation, and can be modified through the provided methods. The switch implementation gives the option to allow to reset the timer before reaches the time limit if the push button is pressed again.
Method | Parameters |
---|---|
**_TmLtchMPBttn_** | uint8_t mpbttnPin, unsigned long int actTime(, bool pulledUp(, bool typeNO(, unsigned long int dbncTimeOrigSett(, unsigned long int strtDelay)))) |
getSrvcTime() | None |
setSrvcTime() | unsigned long int newSvcTime |
setTmerRstbl() | bool newIsRstbl |
The Service time is set at instantiation, and can be modified through the provided methods. The switch implementation gives the option to allow to reset the timer before it gets to the end if the push button is pressed, the option to give a warning when the time is close to the end through a second flag (remaining time is defined as a percentage of the total ON time and it's configurable), and the possibility to set a third signal ON while the switch is off, just like the pilot light (hint) in a staircase timer switch. The warning signal is independent of the pilot hint.
Method | Parameters |
---|---|
**_HntdTmLtchMPBttn_** | uint8_t mpbttnPin, unsigned long int actTime(, unsigned int wrnngPrctg(, bool pulledUp(, bool typeNO(, unsigned long int dbncTimeOrigSett(, unsigned long int strtDelay))))) |
getFnWhnTrnOffPilot() | None |
getFnWhnTrnOffWrnng() | None |
getFnWhnTrnOnPilot() | None |
getFnWhnTrnOnWrnng() | None |
getPilotOn() | None |
getWrnngOn() | None |
setFnWhnTrnOffPilot() | void* newFnWhnTrnOff |
setFnWhnTrnOffWrnng() | void* newFnWhnTrnOff |
setFnWhnTrnOnPilot() | void* newFnWhnTrnOn |
setFnWhnTrnOnWrnng() | void* newFnWhnTrnOn |
setKeepPilot() | bool keepPilot |
setWrnngPrctg() | unsigned int newWrnngPrctg |
This kind of switch is used when an "abnormal situation" demands the push of the switch On, but a higher authority is needed to reset it to Off from a different signal source. Smoke, flood, Supervisor callout, intrusion alarms and "last man locks" are some examples of the use of this switch. As the external release signal can be physically or logically generated it can be implemented to be received from a switch or a remote signal of any usual kind.
Method | Parameters |
---|---|
**_XtrnUnLtchMPBttn_** | uint8_t mpbttnPin(, bool pulledUp(, bool typeNO(, unsigned long int dbncTimeOrigSett(, unsigned long int strtDelay)))) |
**_XtrnUnLtchMPBttn_** | uint8_t mpbttnPin, DbncDlydMPBttn* unltchBttn(, bool pulledUp(, bool typeNO(, unsigned long int dbncTimeOrigSett(, unsigned long int strtDelay)))) |
This is an Abstract Class meaning that no object can be instantiated from it. The members defined are meant to be available for the Double Action Latched DD-MPB (DblActnLtchMPBttn) subclasses instantiated objects, and define common behavior for all the "Double Action Latched Button Subclasses".
The pattern selected for this class is the following:
The presses patterns are:
[!NOTE] The short press will always be calculated as the Debounce + Delay set attributes.
[!NOTE] The long press is a configurable attribute of the class, the Secondary Mode Activation Delay (scndModActvDly) that holds the time after the Debounce + Delay period that the MPB must remain pressed to activate the mentioned mode. The same time will be required to keep pressed the MPB while in Main Behavior to enter the Secondary behavior.
Method | Parameters |
---|---|
getFnWhnTrnOffScndry() | None |
getFnWhnTrnOnScndry() | None |
getIsOnScndry() | None |
getScndModActvDly() | None |
setFnWhnTrnOffScndryPtr() | void* fnWhnTrnOff |
setFnWhnTrnOnScndryPtr() | void* fnWhnTrnOn |
setScndModActvDly() | unsigned long newVal |
This is a subclass of the DALDD-MPB whose secondary behavior is that of a DbncdDlydMPBttn (DD-MPB), that implies that:
Method | Parameters |
---|---|
DDlydDALtchMPBttn() | uint8_t mpbttnPin(, bool pulledUp(, bool typeNO(, unsigned long int dbncTimeOrigSett(, unsigned long int strtDelay)))) |
This is a subclass of the DALDD-MPB whose secondary behavior is analog to that of a Digital potentiometer (DigiPot) or a Discreet values increment/decrement register. That means that when in the second mode, while the MPB remains pressed, an attribute set as a register changes its value -the Output Current Value (otptCurVal) register-.
When the timer callback function used to keep the MPB status updated is called -while in the secondary mode state- the time since the last call is calculated and the time lapse in milliseconds is converted into Steps, using as configurable factor the outputSliderSpeed in a pre-scaler fashion. At instantiation the outputSliderSpeed is configured to 1 (step/millisecond, i.e. 1 step for each millisecond).
The resulting value in "steps" is then factored by the outputSliderStepSize, which holds the value that each step will modify the otptCurVal register.
The implemented embedded behavior mechanisms of the class determine how the modification of the otpCurVal register will be made, and the associated effects to the instantiated object's attribute, such as (but not limited to):
The minimum and maximum values, the rate in steps/millisecond, the size of each step and the variation direction (sign of the variation, incrementing or decrementing) are all configurable, as is the starting value and the mechanism to revert the "direction" that includes:
Method | Parameters |
---|---|
SldrDALtchMPBttn() | uint8_t mpbttnPin(, bool pulledUp(, bool typeNO(, unsigned long int dbncTimeOrigSett(, unsigned long int strtDelay(, uint16_t initVal))))) |
getOtptCurVal() | None |
getOtptCurValIsMax() | None |
getOtptCurValIsMin() | None |
getOtptSldrSpd() | None |
getOtptSldrStpSize() | None |
getOtptValMax() | None |
getOtptValMin() | None |
getSldrDirUp() | None |
setOtptCurVal() | uint16_t newVal |
setOtptSldrSpd() | uint16_t newVal |
setOtptSldrStpSize() | uint16_t newVal |
setOtptValMax() | uint16_t newVal |
setOtptValMin() | uint16_t newVal |
setSldrDirDn() | None |
setSldrDirUp() | None |
setSwpDirOnEnd() | bool newVal |
setSwpDirOnPrss() | bool newVal |
swapSldrDir() | None |
This is an Abstract Class meaning that no object can be instantiated from it. The members defined are meant to be available for the VdblMPBttn DD-MPB (VDD-MPB) subclasses instantiated objects, and define common behavior for all the "Voidable Button Subclasses".
Depending on the subclasses the voided state might be Voided & Off state, Voided & On state or Voided & Not enforced states, being this last one those which enter the voided state but the On state will not be forced to change due to the voidance.
Those conditions to change to a voided state include -but are not limited to- the following conditions:
The mechanisms to "un-void" the MPB and return it to an operational state include -but are not limited to- the following actions:
The voiding conditions and the un-voiding mechanisms define the VDD-MPB subclasses.
Method | Parameters |
---|---|
getFnWhnTrnOffVdd() | None |
getFnWhnTrnOnVdd() | None |
getFrcOtptLvlWhnVdd() | None |
getIsVoided() | None |
getStOnWhnOtpFrcd() | None |
setFnWhnTrnOffVddPtr() | void* newFnWhnTrnOff |
setFnWhnTrnOnVddPtr() | void* newFnWhnTrnOn |
setIsNotVoided() | None |
setIsVoided() | None |
Then the switch will return to the Off position until the push button is released and pushed back. This kind of switches are used to activate limited resources related management or physical safety devices, and the possibility of a physical blocking of the switch to extend the ON signal artificially beyond designer's plans is highly undesired. Water valves, door unlocking mechanisms, hands-off security mechanisms, high power consuming devices are some of the usual uses for these type of switches. It's implemented in the TmVdblMPBttn class.
Method | Parameters |
---|---|
**_TmVdblMPBttn_** | uint8_t mpbttnPin, unsigned long int voidTime(, bool pulledUp(, bool typeNO(, unsigned long int dbncTimeOrigSett(, unsigned long int strtDelay(, bool isOnDisabled))))) |
getVoidTime() | None |
setVoidTime() | None |
This class models a Single Service Voidable DD-MPB a.k.a. Trigger switch (SSVDD-MPB)
After the attribute flag is set to true (the only mandatory action is the attribute flag setting) and the configured mechanisms are triggered the MPB will enter the Voided State, forcing the MPB into the Off State by design. The SnglSrvcVdblMPBttn class objects requires the MPB to be released to exit the Voided State, restarting the cycle.
This kind of switches are used to handle "Single Shot Trigger" style signals, ensuring one single signal triggered per push.
[!CAUTION] The setting of the isOn attribute flag is part of the basic design requirements of the library. Depending on checking the reading trough the getIsOn(**) method will surely fail due to the high risk of missing the short time the flag will be raised before it is again taken down by the voidance of the MPB, although the use of the outputsChange attribute flag is enabled. The use of the non-polling facilities ensures no loss of signals and enough time to execute the code depending on the "trigger activation", i.e. the **fnWhnTrnOn function.
[!NOTE] Due to the short time the isOn flag will be raised, as described above, the short time between the fnWhnTrnOn function and the fnWhnTrnOff function callings must also need to be carefully evaluated by the user.
Method | Parameters |
---|---|
**_SnglSrvcVdblMPBttn_** | uint8_t mpbttnPin(, bool pulledUp(, bool typeNO(, unsigned long int dbncTimeOrigSett(, unsigned long int strtDelay)))) |