|
| 1 | +/** |
| 2 | + ****************************************************************************** |
| 3 | + * @file : 02_DbncdDlydMPBttn_1e.ino |
| 4 | + * @brief : Example for the ButtonToSwitch library DbncdDlydMPBttn class |
| 5 | + * |
| 6 | + * Framework: Arduino |
| 7 | + * Platform: AVR |
| 8 | + * |
| 9 | + * The example instantiates a DbncdDlydMPBttn object using: |
| 10 | + * - 1 push button between GND and dmpbMainInpt |
| 11 | + * - 1 led with it's corresponding resistor between GND and dmpbIsOnOtpt |
| 12 | + * - 1 led with it's corresponding resistor between GND and dmpbIsEnabledOtpt |
| 13 | + * - 1 led with it's corresponding resistor between GND and dmpbOnOffFnOtpt |
| 14 | + * |
| 15 | + * This simple example instantiates the DbncdDlydMPBttn object in the setup(), |
| 16 | + * and then checks it's attributes flags through the getters methods in the |
| 17 | + * loop(). Two functions are added to be executed when the object's isOn |
| 18 | + * attribute flag value changes. |
| 19 | + * |
| 20 | + * The fnWhnTrnOn() will be called when the object enters the isOn=True state, |
| 21 | + * and fnWhnTrnOff() when the object enters the isOn=false state. |
| 22 | + * For this example purpose the functions code was kept to the bare minimum, |
| 23 | + * please keep in mind that for more complex and time consuming functions a valid |
| 24 | + * approach would be to treat the function as an INT callback execution: instead |
| 25 | + * of executing a complex and time consuming code in the function set flags and |
| 26 | + * values needed to identify the situation in the loop() and add the needed code |
| 27 | + * at it. |
| 28 | + * |
| 29 | + * When a change in the object's outputs attribute flags values is detected, it |
| 30 | + * manages the loads and resources that the switch turns On and Off, in this |
| 31 | + * example case are the output of some GPIO pins. |
| 32 | + * |
| 33 | + * A time controlling code section changes the isEnabled attribute of the MPBttn |
| 34 | + * after a time period, changing the MPBttn from enabled to disabled and then |
| 35 | + * back, to test the MPBttn behavior when enabling and disabling the MPBttn. |
| 36 | + * |
| 37 | + * Note: The setIsOnDisabled() method affects the behavior of the MPBttn when it |
| 38 | + * enters the Disabled state, check the documentation and experiment with it. |
| 39 | + * |
| 40 | + * @author : Gabriel D. Goldman |
| 41 | + * |
| 42 | + * @date : 01/08/2023 First release |
| 43 | + * 07/10/2024 Last update |
| 44 | + * |
| 45 | + ****************************************************************************** |
| 46 | + * @attention This file is part of the examples folder for the ButtonToSwitch |
| 47 | + * library. All files needed are provided as part of the source code for the library. |
| 48 | + * |
| 49 | + * Released into the public domain in accordance with "GPL-3.0-or-later" license terms. |
| 50 | + * |
| 51 | + ****************************************************************************** |
| 52 | + */ |
| 53 | +#include <Arduino.h> |
| 54 | +#include <ButtonToSwitch.h> |
| 55 | + |
| 56 | +//==========================================>> BEGIN Function Prototypes |
| 57 | +void fnWhnTrnOn(); |
| 58 | +void fnWhnTrnOff(); |
| 59 | +//==========================================>> END Function Prototypes |
| 60 | + |
| 61 | +//------------------------------> Inputs/Outputs related values |
| 62 | +const uint8_t dmpbMainInpt{6}; |
| 63 | +const uint8_t dmpbIsOnOtpt{3}; |
| 64 | +const uint8_t dmpbIsEnabledOtpt{4}; |
| 65 | +const uint8_t dmpbOnOffFnOtpt{10}; |
| 66 | + |
| 67 | +//------------------------------> isEnabled state related values |
| 68 | +unsigned long int enbldOnOffTm{10000}; |
| 69 | +unsigned long int lstEnblSwpTm{0}; |
| 70 | + |
| 71 | +//------------------------------> DbncdDlydMPBttn class object instantiation |
| 72 | +DbncdDlydMPBttn myDMPBttn (dmpbMainInpt); |
| 73 | + |
| 74 | +void setup() { |
| 75 | + digitalWrite(dmpbIsOnOtpt, LOW); |
| 76 | + digitalWrite(dmpbIsEnabledOtpt, LOW); |
| 77 | + digitalWrite(dmpbOnOffFnOtpt, LOW); |
| 78 | + |
| 79 | + pinMode(dmpbIsOnOtpt, OUTPUT); |
| 80 | + pinMode(dmpbIsEnabledOtpt, OUTPUT); |
| 81 | + pinMode(dmpbOnOffFnOtpt, OUTPUT); |
| 82 | + |
| 83 | + myDMPBttn.setFnWhnTrnOnPtr(fnWhnTrnOn); |
| 84 | + myDMPBttn.setFnWhnTrnOffPtr(fnWhnTrnOff); |
| 85 | + myDMPBttn.setStrtDelay(200); |
| 86 | + myDMPBttn.setIsOnDisabled(false); |
| 87 | + |
| 88 | + myDMPBttn.begin(20); |
| 89 | +} |
| 90 | + |
| 91 | +void loop() { |
| 92 | + if((millis() - lstEnblSwpTm) > enbldOnOffTm ){ |
| 93 | + if(myDMPBttn.getIsEnabled() == true) |
| 94 | + myDMPBttn.disable(); |
| 95 | + else |
| 96 | + myDMPBttn.enable(); |
| 97 | + lstEnblSwpTm = millis(); |
| 98 | + } |
| 99 | + |
| 100 | + if(myDMPBttn.getOutputsChange()){ //This checking is done for saving resources, avoiding the rewriting of the pin value if there are no state changes in the MPB status |
| 101 | + digitalWrite(dmpbIsOnOtpt, (myDMPBttn.getIsOn())?HIGH:LOW); |
| 102 | + digitalWrite(dmpbIsEnabledOtpt, (myDMPBttn.getIsEnabled())?LOW:HIGH); |
| 103 | + myDMPBttn.setOutputsChange(false); //If the OutputChanges attibute flag is used, reset it's value to detect the next need to refresh outputs. |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +//==========================================>> BEGIN Functions declarations to executed when isOn status is modified |
| 108 | +void fnWhnTrnOn(){ |
| 109 | + digitalWrite(dmpbOnOffFnOtpt, HIGH); |
| 110 | + |
| 111 | + return; |
| 112 | +} |
| 113 | + |
| 114 | +void fnWhnTrnOff(){ |
| 115 | + digitalWrite(dmpbOnOffFnOtpt, LOW); |
| 116 | + |
| 117 | + return; |
| 118 | +} |
| 119 | +//==========================================>> END Functions declarations to executed when isOn status is modified |
0 commit comments