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
BradyBrother100
Posts: 54
Joined: Tue Oct 13, 2020 4:21 pm
Location: United States MDT

Native Support for ON/OFF/ON Switches

#1 Post by BradyBrother100 »

Hi! It would be nice if Air Manager had a feature to support ON/OFF/ON switches without having to add two ON/OFF switches and combine the OFF states because that can break your code pretty easily. Having the abilty to assign a pin position to OFF or GROUND would be awesome. For example, you add the ON/OFF/ON with the hw_switch_add command and assign it three positions. You would then assign pin pos 0 and pin pos 2 to what ever pins you connected the two ON positions to. For pin pos 1, you could simply select OFF or GROUND and you would be good to go. You could also assign any pin pos to OFF or Ground if you have a Rotary Switch and the first position is OFF. If there is no signal for pin pos 0 and pin pos 2, AM would assume that the switch is in the OFF position. This would also be great for rotary switches since it is hard to find lets say an ON/ON/ON/ON switch that is also at a reasonable price. Could this be a future feature?


Thanks!
Brady

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

Re: Native Support for ON/OFF/ON Switches

#2 Post by Corjan »

Hi,


Can't you connect both signals from pos 0 and pos 2 to the same pin and make a two position hw_switch_add?


Corjan

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

Re: Native Support for ON/OFF/ON Switches

#3 Post by Keith Baxter »

Hi,

Yes this is far easier. You need the right switch though.




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
BradyBrother100
Posts: 54
Joined: Tue Oct 13, 2020 4:21 pm
Location: United States MDT

Re: Native Support for ON/OFF/ON Switches

#4 Post by BradyBrother100 »

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?
Last edited by BradyBrother100 on Mon Mar 29, 2021 8:44 pm, edited 1 time in total.

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

Re: Native Support for ON/OFF/ON Switches

#5 Post by Sling »

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.

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

Re: Native Support for ON/OFF/ON Switches

#6 Post by Keith Baxter »

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
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
Keith Baxter
Posts: 4671
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Native Support for ON/OFF/ON Switches

#7 Post by Keith Baxter »

Hi,

OK I have not tested it because I do not have an arduino set up but you can give this code a try. I suspect that a two position switch that is not in position 0 or 1 will return a nil. If that is the case then this should solve your issue.

Code: Select all

---First we must read the switch state.

--Create a function to read the seatbelt sign position.
sign_state= 0
function seatbelt_callback(sign)
   sign_state= sign
   print("The sim sign state is    "..sign_state)
end
xpl_dataref_subscribe("laminar/B738/toggle_switch/seatbelt_sign_pos","FLOAT",seatbelt_callback)
----------------------------------------------------------------------------------------------------------
new_pos=0
---Create a switch function
function switch_fasten_belts(pos)
---Create your 3 position switch
        if pos == 0 then new_pos =0 end
        if pos == nil then new_pos =1 end
        if pos == 1 then new_pos = 2 end
        
	if sign_state < new_pos then 
		xpl_command("laminar/B738/toggle_switch/seatbelt_sign_dn")
	elseif sign_state > new_pos then
		xpl_command("laminar/B738/toggle_switch/seatbelt_sign_up")
	end
end

-- Create a new 2 position switch
fasten_belts_switch=hw_switch_add("Fasten Seat Belts",2,switch_fasten_belts)

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
Keith Baxter
Posts: 4671
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Native Support for ON/OFF/ON Switches

#8 Post by Keith Baxter »

Hi,

Ok it looks like the switch is a one time read and there is no nil state.

Your only other option is to use a few 2N3904. But honestly as Tony says, far easier to just get the correct switch.

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

Re: Native Support for ON/OFF/ON Switches

#9 Post by Sling »

Keith Baxter wrote: Tue Mar 30, 2021 6:06 am Your only other option is to use a few 2N3904. But honestly as Tony says, far easier to just get the correct switch.
I was referring to the 4 way switch that was mentioned not the on-off-on switch. For that one the solution has already been given in Brady’s other thread about the same subject.

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

Re: Native Support for ON/OFF/ON Switches

#10 Post by Keith Baxter »

Hi,

Ok I tested this HACK. This i think is the best one can do only using three iO pins that will accommodate your switches.

My beer of choice is Amstel. :lol:

Code: Select all

---First we must read the switch position.

--Create a function to read the seatbelt sign position.

sign_state= 0
new_pos =0
function seatbelt_callback(sign)
   sign_state= sign
   print("The sim sign state is    "..sign_state)
end
xpl_dataref_subscribe("laminar/B738/toggle_switch/seatbelt_sign_pos","FLOAT",seatbelt_callback)
----------------------------------------------------------------------------------------------------------
--Create a function to send the command to the sim.
function send_command()
	if sign_state < new_pos then 
		xpl_command("laminar/B738/toggle_switch/seatbelt_sign_dn")
	elseif sign_state > new_pos then
		xpl_command("laminar/B738/toggle_switch/seatbelt_sign_up")
	end
end


--Simulate a switch using button array
-- Callback function which is called when a button is pressed
function button_pressed(row, column)
   if column == 0 then new_pos = 0 else new_pos = 2 end
   print(new_pos)
   send_command()
end

-- Callback function which is called when a button is released
function button_released(row, column)
    if row == 0 then new_pos = 1 end
	print(new_pos)
	send_command()
end

-- Create button array with name "my keypad" with 1 rows and 2 columns
hw_button_array_add("my keypad", 1, 2, button_pressed, button_released)

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 

Post Reply