Native Support for ON/OFF/ON Switches

Let Sim Innovations know about your Air Manager experience and let us know about your dream feature addition

Moderators: russ, Ralph

Message
Author
User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: Native Support for ON/OFF/ON Switches

#11 Post by jph »

Sling wrote: Mon Mar 29, 2021 8:43 pm I would not be in favour of wasting an I/O pin for no use. Ask yourself a question. How would this work if it were just the switch and Arduino without AM. Answer the same as it currently works with AM.

12 way adjustable stop switches can be had for around $2 so not sure why you can’t make a 4 way.
+1
I dont believe there is any real need for any 'native' on/off/on function as Tony says, if you were to look at how it would work in Arduino it is pretty simple. It is simply a matter of monitoring 2 pins. The pins active low denote if the switch is in either 'on' and which one, and if both switches are high (not active) then the centre position is selected.
Note also, this kind of switch can be used as an ON/OFF/ON or indeed an ON/ON/ON or ON/ON/OFF or any combination with exactly the same 2 pins and wiring. For that it is simply a matter of instructing the code as to what the activated and non activated position(s) should do.
Also as said above, for rotary switches use the standard multi position units, the 12 way is a good example. It is possible to adjust the stop on most units to select any number of positions.
Joe
Joe. CISSP, MSc.

User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: Native Support for ON/OFF/ON Switches

#12 Post by jph »

Keith Baxter wrote: Mon Mar 29, 2021 8:49 pm
BradyBrother100 wrote: Mon Mar 29, 2021 8:41 pm Hi! The 3 Position switch I have only has three pins instead of six. The two outboard pins being the signal pins and the middle pin being the ground. How would that work?
Hi,

Look at getting something like this.

https://www.amplifiedparts.com/products ... 3-position

Keith
Hi Keith and good morning :) . The switch that brady has should be fine. If it is an on off on then unlike a normal DPDT switch with 3 pins, this works differently. Where a normal DPDT 3 pin either covers pin 1 and 2, or, pin 2 and 3, in the ON OFF ON unit the center position leaves both outer terminals completely disconnected from the center terminal. It's order is - for example - ON - pins 1 and 2 connected, OFF no pins connected together at all, the other ON is pins 2 and 3 connected.
Regards,
Joe
Joe. CISSP, MSc.

User avatar
Keith Baxter
Posts: 4671
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Native Support for ON/OFF/ON Switches

#13 Post by Keith Baxter »

Joe,

100% But you have to then have TWO switch adds.

AM returns the following.

1) One position switch. That is a switch that is either open circuit or closed circuit. The position value returned by AM is 0 or 1. In my opinion that is incorrect. It should be nil or 0
2) Two position switch. That is a switch that closed circuit in position one or closed circuit in position two. The position values returned by AM are also 0 or 1. This is correct.
3) Three position switch. on-non-on. That is the switch you describe. The position values returned by AM should be 0-nil-1. This is not the case and is what is been asked for.

In the case of three then 2 IO pins are used and the ground.

Yes it can be achieved by adding two switches or the button array.

Keith

EDIT: What also might be an option @Corjan is to have a N/C pin in the arduino
Last edited by Keith Baxter on Tue Mar 30, 2021 10:43 am, edited 1 time in total.
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

User avatar
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: Native Support for ON/OFF/ON Switches

#14 Post by Sling »

Keith,

Sorry but I agree with Joe. You should know my response by now to new API requests. Don’t get me wrong if it’s not achievable already or is a brand new feature then I’m normally in favour of adding to the API but it will soon get cluttered if the API is expanded for things that can already be done with the existing functions. What happens if someone wants another combination.

This is a little bit of a work around but it’s not that hard to implement. It would be a good one to add to the community code snippets actually.

User avatar
Keith Baxter
Posts: 4671
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Native Support for ON/OFF/ON Switches

#15 Post by Keith Baxter »

Sling wrote: Tue Mar 30, 2021 10:36 am Keith,

Sorry but I agree with Joe. You should know my response by now to new API requests. Don’t get me wrong if it’s not achievable already or is a brand new feature then I’m normally in favour of adding to the API but it will soon get cluttered if the API is expanded for things that can already be done with the existing functions. What happens if someone wants another combination.

This is a little bit of a work around but it’s not that hard to implement. It would be a good one to add to the community code snippets actually.
Tony,

Yes I agree and I hear you.

However I do not agree with 1 and 2 position switches returning the same position value. They should be different. If a switch is open circuit then the position returned should be nil or -0 or some other value.

Correcting the logic is not adding to the API. It is simply making a switch work as it should.

What I am saying is that if a switch is OC then the position returned is ~.

Keith
Last edited by Keith Baxter on Tue Mar 30, 2021 11:50 am, edited 1 time in total.
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: Native Support for ON/OFF/ON Switches

#16 Post by jph »

I think there is an interesting discussion to be had regarding switch positions for actual electronic purposes as opposed to switch position(s) for coding.
For example Keith, I presume when you refer to a 1 way or a two way switch you are referring to an ON/OFF and an ON/ON where an ON/OFF would be two pins, and an ON/ON would be 3 pins (on the switch!).
This is for actual electronic circuit use in reality. For use in code only it is in effect the same switch - a simple on/off. a 0 or a 1. As can be the ON/ON 3 pin switch, you simply use 2 pins only and you then have the same 0 and 1 where the code utilises the return to signify the state. Hence, the actual 0 / 1 return would be correct for both. It is simply what you do in the code. I don't see any reason for a differentiation for coding, but it would be imperative for actual use in an electrical circuit.
Joe
Joe. CISSP, MSc.

User avatar
Keith Baxter
Posts: 4671
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Native Support for ON/OFF/ON Switches

#17 Post by Keith Baxter »

Joe,

My point. When a switch is OC (Open circuit) it does not have a ground directed to a IO pin. In that case hw_switch_get_position(switch_id) and switch_callback(pos) should be returning <nil> .
Any other value should not be fetched via a.... hw_switch_get_position(switch_id) or the switch_callback(pos) because there is no position.

@Sling @Corjan am I correct on that point? I need to know If my thinking is wonky or not.

If the switch <ground> is not directed to IO pin 'A' or IO pin 'B' or any other IO pin, then it does not have a position. How can it have a position if either pin 'A' or pin 'B' or any other pin is NOT held to ground.

Am I also correct on the above point?

My deduction and issue is. When a {pull to ground} is no longer persistent, then AM does not register it like it does a button. The {pull to ground}, to that IO pin, is released and that IO pin should no longer a hold position value.

IMHO If a switch has no persistent {pull to ground} It should then return a <nil> as a position. As simple as that.

I am just wanting things to work as one would expect and be consistent.
Anyway just trying to make things easier for others that are not very code literate.

@Sling You getting what I am on about?

Keith
Last edited by Keith Baxter on Tue Mar 30, 2021 8:28 pm, edited 1 time in total.
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

User avatar
BradyBrother100
Posts: 54
Joined: Tue Oct 13, 2020 4:21 pm
Location: United States MDT

Re: Native Support for ON/OFF/ON Switches

#18 Post by BradyBrother100 »

Hi! It was nice seeing everyone's thoughts on this topic! I agree the most with @Keith Baxter and his thoughts on the returning of positions being a little weird. I have both ON/ON switches and ON/OFF switches and I always noticed how weird is was that both switches were returning the same value. @jph You are correct on explaining how the switch I have works. Sorry for not including that in the original statement. @Sling said, "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

User avatar
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: Native Support for ON/OFF/ON Switches

#19 Post by Sling »

@Keith Baxter I understand what you are suggesting. I guess what you have to consider is in the digital world it’s always 0 or 1. With the simplest of switches that means the 2 states are 0 and 1 regardless of the switch used. When the switch is 0/C in your 1 way switch example the state is still either active/inactive, on/off, 0/1. We can call it what we want the state is still one of two states and in digital electronics and in bit level software 0 and 1 is used to represent these two states. All the educational material is based around this and to introduce another concept for the same thing would muddle the established.

Sorry we have gone a bit off topic.

I truly understand why Brady would ask the original question as using the on-off-on switch like this is not as straight forward as a simple on-off switch. I hope what I posted and what Joe explained did shed some light on this subject.

Tony

User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: Native Support for ON/OFF/ON Switches

#20 Post by jph »

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.
Last edited by jph on Wed Mar 31, 2021 9:36 am, edited 2 times in total.
Joe. CISSP, MSc.

Post Reply