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: Ralph, russ

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

Re: Native Support for ON/OFF/ON Switches

#21 Post by Keith Baxter » Wed Mar 31, 2021 9:29 am

Hi,

I have shared my thoughts and I am moving on. I have the ability to do work around's so it does not affect me much.

Brady has a few examples of how to use the switches he has so that should assist him.

An interesting discussion anyway and I think others would have taken a lot out of the content. I certainty learned one or two things.

Keith
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 

BradyBrother100
Posts: 32
Joined: Tue Oct 13, 2020 4:21 pm

Re: Native Support for ON/OFF/ON Switches

#22 Post by BradyBrother100 » Wed Mar 31, 2021 3:41 pm

Hi. Keith, I appreciate your thoughts and contributions to this thread. Thank you. When dealing with an ON/OFF, 2 Position switch and using a hw_switch_add line, how does Air Manager know if the switch is in the OFF position appose from the ON position since nothing is being sent through the Arduino or the the Switch?

Thanks!
Brady

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

Re: Native Support for ON/OFF/ON Switches

#23 Post by Keith Baxter » Wed Mar 31, 2021 4:00 pm

BradyBrother100 wrote:
Wed Mar 31, 2021 3:41 pm
Hi. Keith, I appreciate your thoughts and contributions to this thread. Thank you. When dealing with an ON/OFF, 2 Position switch and using a hw_switch_add line, how does Air Manager know if the switch is in the OFF position appose from the ON position since nothing is being sent through the Arduino or the the Switch?

Thanks!
Brady
Brady.

AM does not know it assumes. :D and there is different logic in the API

I think we have exhausted this topic and I doubt there will be change. Something we will all have to accept and adapt in our coding.
Not a big deal, but a nice to have.


Keith
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: 4389
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: Native Support for ON/OFF/ON Switches

#24 Post by Sling » Wed Mar 31, 2021 8:50 pm

BradyBrother100 wrote:
Wed Mar 31, 2021 3:41 pm
Hi. Keith, I appreciate your thoughts and contributions to this thread. Thank you. When dealing with an ON/OFF, 2 Position switch and using a hw_switch_add line, how does Air Manager know if the switch is in the OFF position appose from the ON position since nothing is being sent through the Arduino or the the Switch?

Thanks!
Brady
AM and the Arduino don’t have to assume anything they know. When nothing is connected to the Arduino pin it will always be held high by an internal pull-up (logic level 1). When the switch is made it grounds this pin (logic level 0).

User avatar
jph
Posts: 560
Joined: Fri Apr 10, 2020 12:50 pm
Location: A Brit exiled to Portugal

Re: Native Support for ON/OFF/ON Switches

#25 Post by jph » Thu Apr 01, 2021 7:52 am

Sling wrote:
Wed Mar 31, 2021 8:50 pm
BradyBrother100 wrote:
Wed Mar 31, 2021 3:41 pm
Hi. Keith, I appreciate your thoughts and contributions to this thread. Thank you. When dealing with an ON/OFF, 2 Position switch and using a hw_switch_add line, how does Air Manager know if the switch is in the OFF position appose from the ON position since nothing is being sent through the Arduino or the the Switch?

Thanks!
Brady
AM and the Arduino don’t have to assume anything they know. When nothing is connected to the Arduino pin it will always be held high by an internal pull-up (logic level 1). When the switch is made it grounds this pin (logic level 0).
Exactly.
Brady, with respect, It is apparent to me that all your problems seem to be entirely related to a lack of misunderstanding of how AM even deals with a single switch - A simple ON/OFF. as you queeried above. There is little hope of grasping the concept of any other type of switch use unless you get your head around that.
Try to understand the internal pull-ups to hold the port pin high (logical 1) with nothing active. It is pulled low to ground to get a low logic level (Logical 0).
You don't 'have' to know this but it helps if you are struggling with the concept of the numerical return value of the switch movement function.
You also seem to be transfixed with 'on' and 'off' and perhaps confusing them with normal switch nomenclature and the use of 'on' or 'off' for a function or action in AM. There is no relationship between the hardware 'OFF' and any action that you chose to perform in AM. the function called on movement returns a value, it is entirely up to you to do what you want with this value.
On or OFF for a switch of any type in AM is a concept. An 'open' or 'inactive' switch can be 'ON' to your program, or, an active switch can be ON in your program. Same as an 'active' switch can be OFF in your program. If you get your head around that it should become far clearer to you and the rest should fall into place.
Joe
“Everything in moderation, including moderation.”
“I am not young enough to know everything.”
“Experience is merely the name men gave to their mistakes.”
-- Oscar Wilde --
Joe (CISSP) -IT security consultant

BradyBrother100
Posts: 32
Joined: Tue Oct 13, 2020 4:21 pm

Re: Native Support for ON/OFF/ON Switches

#26 Post by BradyBrother100 » Thu Apr 01, 2021 3:00 pm

You are absolutely right when you said, "It is apparent to me that all your problems seem to be entirely related to a lack of misunderstanding of how AM even deals with a single switch." I am very new to this, I just started learning how to connect switches to Arduino and code them in AM about a month ago. I am always learning new things from this forum which is what I really like about this forum. With that said, is it possible to have the OFF/Open/Inactive position on an ON/OFF/ON switch be programed as if it were to be on as you said here, "An 'open' or 'inactive' switch can be 'ON' to your program, or, an active switch can be ON in your program," using only ONE hw_switch_add? I really feel like it should if AM knows when the switch is OFF/Open/Inactive and AM knows when the switch is ON/Closed/Active. I appreciate everyone's contributions to this thread and I could have never learned as much as I have without you all.


Thanks!
Brady

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

Re: Native Support for ON/OFF/ON Switches

#27 Post by Keith Baxter » Thu Apr 01, 2021 4:11 pm

HI

My Last post on this.

Lets take a 12 position rotary switch. Picture it as a clock.

If the switch is at position 2 O-clock then the AM logic tells us that it is position 1. We all understand that.

Now lets move the rotary switch "SLOWLY" to the 3 O-clock position. When I say slowly I mean 30 seconds between leaving 2 O-clock and arriving at 3 O-clock.

AM will only record the position change when the switch arrives at 3 O-clock.
AM "ASSUMES" that the switch is still at 2 O-clock until it arrives at 3 O-clock.

AM might know that the switch is in transit, but it does not convey/return the the logic value. And that value is a nil.

My 2C

Keith
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: 560
Joined: Fri Apr 10, 2020 12:50 pm
Location: A Brit exiled to Portugal

Re: Native Support for ON/OFF/ON Switches

#28 Post by jph » Fri Apr 02, 2021 9:04 am

Keith Baxter wrote:
Thu Apr 01, 2021 4:11 pm
HI
My Last post on this.
Lets take a 12 position rotary switch. Picture it as a clock.
If the switch is at position 2 O-clock then the AM logic tells us that it is position 1. We all understand that.
Now lets move the rotary switch "SLOWLY" to the 3 O-clock position. When I say slowly I mean 30 seconds between leaving 2 O-clock and arriving at 3 O-clock.
AM will only record the position change when the switch arrives at 3 O-clock.
AM "ASSUMES" that the switch is still at 2 O-clock until it arrives at 3 O-clock.
AM might know that the switch is in transit, but it does not convey/return the the logic value. And that value is a nil.
My 2C
Keith
Hello and good morning brother Keith :) - this reply is more to help Brady as I believe you know what you refer to, but, I think the use of the word 'assumes' and the use of 'nil' could certainly be construed as confusing here. AM doesn't assume anything. it knows, it is digital high or low,
AM - when assigned a single switch with more than 1 position - meaning anything with 2 positions and above, is programmed internally to IGNORE the change of state of the port pins during switch rotation. In this type of hardware switch AM will only respond to an active low on one of the pins assigned and only one per assigned switch. The non active state during switching (which is simply all the port pins registering logic high (1) as they are inactive - is DELIBERATELY ignored, never assumed. The API is designed and programmed to only return a position value only when a different port pin is pulled low. No assumption and most importantly the value is NEVER NIL. During a switch movement transition there are simply all logic 1'a on the port pins. Again , there is no 'intermediate state', no 'NIL'.
Why I believe this is important to stress is that is because when using a single toggle switch AM is also fully aware of the state but in this case does not IGNORE the 'inactive' or what has been referred to, possibly also confusingly I believe, as 'open circuit'
The change that the API is looking for here is from an active state on the single assigned pin, to a non active state. Or, to put it another way, from a logic 1 (HIGH LEVEL) on the port pin assigned when the switch is open (inactive) to a logic 0 (LOGIC LOW) on the port pin (active) when the pin is grounded by the switch. It returns a 0 or 1 depending on the state of the switch, active or inactive. In this case, AM is responding to a logic change on a single pin because it is programmed to do that.
In a rotary switch, for example, or, any switch with more than one pin in use, it is simply programmed to ignore any transition as it is programmed to respond only to a change from one pin being pulled low (active / closed switch on that pin / port pin pulled low or however someone may want to defne it ) to another pin being pulled low by another part pole of the switch being connected to ground (active). It will simply ignore the period where no segment of the switch is active. - which again, is never 'NIL' - it is simply all logic 1's on the pins. Apples and oranges etc.
So, to sum up, on a multi pin assignment for a single switch, the API only responds to an active low on one of the pins as this is how it is programmed to respond.
In the case of a single switch with 1 data input pin, then this is not the case as it is programmed internally in AM to do this. When the single switch is active then the port pin is pulled low (Logic Level 0) and AM sends a 1 as the return value. Conversely, When the port pin is high - non active, (and it is only HIGH (logic 1) due to the arduino internal pull up resistors being assigned to the arduino pin by AM) s now, then it KNOWS that the switch is in an inactive position and returns a 0 value on the callback return.
To use use the word 'assume' when referring to coding can certainly cause confusion. I UNDERSTAND WHAT YOU ARE GETTING AT.. but for others it may not be the case. Especially when comparing a single data pin switch to a multi data pin switch which are programmed to respond in completely different ways in AM. Again, I think this may be leading to some of Brady's understandable confusion. There is no 'nil' state ever involved here. It is probably better to replace 'assumes' with 'ignores'.

Just for shits and giggles ;) , there are also two types of multipole switches - 'Make before Break' and 'Break before Make'... yikes ! but, AM takes care of both due to the way it is programmed and they are used interchangeably.
Joe
“Everything in moderation, including moderation.”
“I am not young enough to know everything.”
“Experience is merely the name men gave to their mistakes.”
-- Oscar Wilde --
Joe (CISSP) -IT security consultant

User avatar
Corjan
Posts: 2358
Joined: Thu Nov 19, 2015 9:04 am

Re: Native Support for ON/OFF/ON Switches

#29 Post by Corjan » Fri Apr 02, 2021 9:29 am

Hi,


Didn't have time to read though the whole thread here, but I think I get the gist of it.
I don't think it is wise to try to incorporate this into the hw_switch_add function, it would make it harder to understand for the most common use cases.

What I can do though is make the hw_add_input accept multiple pins. That would make it easier to define the translation between the pins and the positions.

Was thinking of something like this:

Code: Select all

hw_input_add("HW_ID_0", "HW_ID_1", function(pin_0_state, pin_1_state)
	position = nil
	if not pin_0_state and pin_1_state then
		position = 0 -- Left ON
	elseif pin_0_state and pin_1_state then
		position = 1 -- OFF
	elseif pin_0_state and not pin_1_state then
		position = 2 -- Right ON
	else
		-- Should never happen (both pins are low)
	end
	
	print("switch state = " .. tostring(position))
end)
Note that this does not work at the moment. You would have to use two seperate hw_input_add calls and use hw_input_read to get the state of all pins.


Corjan

User avatar
jph
Posts: 560
Joined: Fri Apr 10, 2020 12:50 pm
Location: A Brit exiled to Portugal

Re: Native Support for ON/OFF/ON Switches

#30 Post by jph » Fri Apr 02, 2021 10:00 am

Nice, that would be great for all kinds of logic work a really nice feature :D The basis of DIY single define logic gate :mrgreen:
'I commend that motion to the house' as they say in the UK Parliament... :)
“Everything in moderation, including moderation.”
“I am not young enough to know everything.”
“Experience is merely the name men gave to their mistakes.”
-- Oscar Wilde --
Joe (CISSP) -IT security consultant

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests