Paul__B:
It would appear that what is wanted here is a toggle with a Web indication.
Press the Web button and the corresponding switch changes over; press the real pushbutton and it does the same. The Web version presumably will have some means for conveying the present on/ off state.
My understanding from reading the requirements is close to this. However, instead of pushbuttons, it sounds like he wants to use toggle switches. It sounds like one use case might be this:
- Initial conditions: relay is off, toggle switch is down
- web page button is pressed: relay turns on
- web page button is pressed: relay turns off
- toggle switch is flipped up: relay turns on
- web page button is pressed: relay turns off
- toggle switch is flipped down: relay turns on
- web page button is pressed: relay turns off
- web page button is pressed: relay turns on
- toggle switch is flipped up: relay turns off
Basically, pressing the web button toggles the relay, and changing the switch state toggles the relay. This is almost the same as Paul's provided example, except that his example would require the switch to be turned on then turned off to toggle the output. In this case, sometimes flipping the switch up turns on the relay, sometimes it turns it off -- it all depends on the previous state of the relay. Just like a hallway light controlled by a switch on either end: sometimes flipping a switch up turns it on, and sometimes it turns it off -- it all depends on the position of the switch at the other end of the hall.
The change to do the above use case is simple. In Paul's code is a state machine to debounce the mechanical switch contacts (an important consideration!) the butndown() function only returns true when the button has been up long enough to be stable (case 2, where butnstate is set to 1.) To make it work with a latching switch, it also needs to return true when the button has been down long enough to be stable (case 3, just after butnstate is set to 0.)
Basically, any time the switch has reached a stable state different from the previous stable state, toggle the output.
MorganS:
"3 way" = to accomplish the above, you need to use switches with 3 terminals on them
Remember those definitions are written for household electricians who don't want to know what electrons are.
Precisely. The US terminology comes from the number of connections on the switch, not the number of switches in the circuit or the number of positions on the switch (they are always two positions.)
A 2-way switch has two screw terminals. It is an SPST switch. It is used as a standard single switch.
A 3-way switch has three screw terminals. It is an SPDT switch. It is used in pairs where two switches are to control one load.
A 4-way switch has four screws. It is a DPDT switch internally wired as a reversing switch, where the two redundant connections are not brought out (the two common DPDT terminals are one pair of screws, the two NC contacts are brought out as the other pair of screws, and the NO contacts are internally wired to the NC contacts in a crossover manner.) This switch is used with a pair of 3-way switches when three or more switches are to control one load.
The UK naming conventions refer to the number of switches, while the US naming conventions refer to the number of terminals.