I’m using some code, that I haven’t written myself, to monitor a toggle switch. It works brilliantly to control a WiFi controlled light bulb. However, if the Particle device is power cycled for whatever reason, the toggle switch class will be inadvertently triggered to do an initial ‘update’. I’d like to suppress that initial update and only update the switch if it’s physically toggled. Am I making any sense?
TLDR: ToggleSwitchAction::update();
when executed for the first time after a reset toggles the state even if the switch itself hasn’t been toggled.
This is how the switch is updated in loop():
ToggleSwitchAction::update();
and here it is in my support libraries:-
class ToggleSwitchAction{
public:
ToggleSwitchAction(functionPtr openAction, functionPtr closeAction) : onOpen(openAction), onClosed(closeAction){};
void bindToPin(uint8_t togglePin);
static void update(void);
private:
uint8_t pin;
uint32_t lastPressMillis;
functionPtr onOpen;
functionPtr onClosed;
enum SwitchState{
OPEN,
CLOSED,
UNKNOWN,
};
SwitchState lastState = UNKNOWN;
static std::vector<ToggleSwitchAction*>instances;
};
void IndicatorLED::toggle(void)
{
state = !state;
digitalWrite(pin, state? HIGH : LOW);
}
Any help appreciated. My first thought was to add a ‘flag’ into setup() like donotupdate = 1, then then set that flag to 0 at the END of loop, then add an if check somewhere to only update the switch if donotupdate is 0. But it seems like there must be a more elegant solution. And I’m not even sure my proposed solution IS a solution!
Thank you