Detect change of state on input
-
Hi Folks,
I've been lurking around, searching, trying to find some info on detecting change of state and using that to trigger my MyController rules.I have a steel framed house, and it's a nightmare to run new wiring in the walls, so I was hoping to utilise the existing light switches and detect the change of state (from OPEN to GROUND) on the MEGA.
If any of you fine people have done this or something similar, I'd love to hear from you, or if anyone knows of some further reading, I'd appreciate if you could point me in the right direction.
Cheers
Z
-
Maybe you could detect voltage on the bulb side by using an optoisolator. I took a quick look on Digikey and did not see one built for this, but i did not look anywhere else. You would need some electronics to bring the voltage down to the few volts usually required to drive the LED side.
Most of the discussion I have seen on MySensors is about how to power these nodes in a light switch box. Doing it safely can be tricky. Search the forum on "in wall transformer" or "in wall" and you will get lots of hits. Maybe one of them will be helpful.
-
@ZachFlem is the interrupts/interrupt handler the thing you are looking for?
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
-
@pjr I don't think so. I have a simple switch that is either on or off, so the input will see either ground/5v or nothing, all i need to do is detect when that state changes and update the switchState variable to suit the opposite of it's current value (ON to OFF or vice versa)
I haven't had a chance to play with it yet, but I think I can get that part working on the Mega without too much trouble, it will just be getting the MySensors node sending that data to MyController.
I'm very green at all of this
-
@zachflem Perhaps you are misunderstanding @pjr response. Usually the node is sleeping until a changed state wakens it, via interrupts is the way to do so normally.
Although you can detect the change of state on any pin with the node running full time and polling the pin this is not energy efficient if it only has something to do on a state change, and fatal if it running on batteries. Viz - Running 8 hours continuously between the time you switched the light off at night, until morning when you turn the light on?
-
@zboblamont said in Detect change of state on input:
@zachflem Perhaps you are misunderstanding @pjr response. Usually the node is sleeping until a changed state wakens it, via interrupts is the way to do so normally.
Although you can detect the change of state on any pin with the node running full time and polling the pin this is not energy efficient if it only has something to do on a state change, and fatal if it running on batteries. Viz - Running 8 hours continuously between the time you switched the light off at night, until morning when you turn the light on?No batteries here, it's a star wired setup that I'm trying to achieve. And there will be more switches than interupt enabled pins on the MEGA(s)
-
@zachflem Ah ok, that clears up your scenario, not a single node per sensor then, but multiple signals hard-wired from sensor to a single central node....
-
So you will be wiring all the switches with "0-5v line" to mega? Then just digitalRead all the pins on loop and compare with previous state. Simple as that..
Perhaps some (de)bounce library could be helpfull since the switches can cause a bit of bounce.Btw. there is also a library to get interrupts from more pins but it wont help too much with Mega(from only 6 to 16 pins interrupt)..
https://github.com/NicoHood/PinChangeInterrupt