BradyBrother100 wrote: ↑Tue Mar 30, 2021 3:17 pm
"What happens if someone wants another combination." I am assuming you are saying if someone has lets say an OFF/ON/ON/ON switch. The first position, OFF, would return nil and then the ONs would return 0, 1, 2, etc. From looking at the Wiki, there is no native way to code really any switch with 2 or more ON positions and one OFF position without adding multiple hw_switch_add and combining OFF states which can be a little jank in some code.
Brady
Hi Brady.
You need to consider the appropriate switch to use. What a switch that is designed to do in an electronic circuit is not necessarily the same as the one you would chose to use to provide the same function for use with code.
As an example, you mention an OFF/ON/ON/ON switch ?. and 'ideal' returns of 'nil' 0,1,2 etc etc. This is not needed at all.
Consider it another way. You simply use a multi turn switch (providing it is a rotary input you want). You select the correct switch for the job. In this case, it would be - for example - a standard rotary 4 way (which is a standard cheap rotary 12 way with the stop pin set for 4 way operation) with a single declaration. Each position of the switch is an active output. Even what you want to be an 'OFF'
There doesn't have to be a
physical 'OFF here - mechanically.
'OFF'' for your use in code is simply what you would refer to as 'ON' mechanically in the first position. The OFF in this case is a concept only. If the 4 way switch returns 0,1,2,3, then the return of '0' is OFF if that is what you want it to be.
If you wanted a switch - again a rotary for simplicity - that had ON/ON/OFF/ON/OFF/ON/ON then the same principle applies. You would define a 7 pos switch and the returns would be 0,1,2,3,4,5,6 where, in your code, a return of 2 or 4 would indicate to you that the switch is 'OFF' for the purpose of coding.
So no complex coding at all for that example.
If you had a rotary ON/OFF/ON for example, then the return would still be 0,1,2 (when used as a rotary 3 way) but you would utilise the '1' return value to signify the 'OFF' action in your code. The values returned from the switch function are simply a number for you to then decide what to do with. I hope that makes sense ?
Obviously the ON/OFF/ON 3 pin switch that you mention is a different case in that due to the mechanical design you need to use a bit of logic in code to determine the state based on the 2 hardware switch adds it would need. Hopefully you can see that the use of 'nil' is not required or actually helpful. The operation of the switch is, in this case, based on 2 switches with a 0 and 1 output.
You could build a simple truth table for the 2 switches here as follows - if we called the switches used switch A and B then presuming a 1 is returned if active then
A B
0 0 = OFF
1 0 = ON 1
0 1 = ON 2
1 1 = this is not physically possibly to achieve so has no relevance. I am simply showing the complete logic truth table for the two switches.
Also consider - If you wanted the same switch to be an OFF/ON/ON then exactly the same logic levels apply. It is only what you decide to do with it in the code. In this case, the truth table would be interpreted as follows
A B
0 0 = ON 1
1 0 = OFF
0 1 = ON 2
1 1 = this is not physically possibly to achieve so has no relevance. I am simply showing the complete logic truth table for the two switches.
Identical switch mechanically, yet you have now completely changed the function in code by interpreting the truth table slightly differently. Even though it is /mechanically/ an ON/OFF/ON, you have made it an OFF/ON/ON without altering any wiring.
Again Brady, I hope that makes sense ?
Joe.