Toggle Switch Off Position

Support for Arduino in combination with Air Manager and Air Player

Moderators: russ, Ralph

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

Re: Toggle Switch Off Position

#11 Post by BradyBrother100 »

Hi! I got my on/off(on) switch and tried @Sling code. It still didn't detect anything in the off position.
AM 3pos 1.PNG
D22 and D23 both have connection wires and D24 is my dummy port.

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

Re: Toggle Switch Off Position

#12 Post by Sling »

I see the problem. You will have to use one of the other alternatives. I’d suggest adding two 1 way switches and in code when both are off (switch in central position) do whatever action you want for that central position.

When a 1 way switch is added it automatically assumes that if the input is not active then it must mean it’s set to off. You can use the off state to mean whatever you want.

Let us know if you need a code example.

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

Re: Toggle Switch Off Position

#13 Post by BradyBrother100 »

Hi! I used @Ralph code and that seems to be the way to go.

Code: Select all

function input_change(state)
    if state == false then
        print("pos2")
    else print ("pos1")
 end
end
function input_change2(state)
  if state == false then
    print ("pos0")
    else print ("pos1")
end
end
-- Create a new input
hw_input_add("My input", input_change)
hw_input_add("My input2", input_change2)
AM 3pos 2.PNG

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

Re: Toggle Switch Off Position

#14 Post by Sling »

Yes that’s almost exactly the same but using 2 digital input’s rather than 2 one way switches. I know it works with the switch type you are using but be careful with the logic as pos1 is actually only achieved if both inputs are open. It would be cleaner to have the pos1 action code in one place rather than two.

Glad you got it sorted

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

Re: Toggle Switch Off Position

#15 Post by BradyBrother100 »

Hi! I got 12 On/Off/On switches and the code probably could be more simple but, I got the Off position to be registered! (Kind of)
The digital inputs are to detect when there is no signal coming from both input pins and the switch is to detect when the switch is in the on positions.

Code: Select all

function Lpack(pos)
    if pos == 0 then 
     xpl_dataref_write("laminar/B738/air/l_pack_pos", "FLOAT", 2)
    print("pos2")
    else     
     xpl_dataref_write("laminar/B738/air/l_pack_pos", "FLOAT", 0)
    print("pos0")
end
end
function Lpack1(state)
    if state == true then
    xpl_dataref_write("laminar/B738/air/l_pack_pos", "FLOAT", 1)
        print("pos1")
end
end
function Lpack2(state)
    if state == true then
    xpl_dataref_write("laminar/B738/air/l_pack_pos", "FLOAT", 1)
        print("pos1")     
end
end    

hw_switch_add("Lpack", 2, Lpack)
hw_input_add("Lpack1", Lpack1)
hw_input_add("Lpack2", Lpack2)

Post Reply