Multiple functions with 1 button

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Post Reply
Message
Author
Gixxar
Posts: 63
Joined: Mon Oct 03, 2016 9:55 pm

Multiple functions with 1 button

#1 Post by Gixxar »

Hi all,

I have been using this code which was created by @russ back in 2018, it works great for me. But now I am trying to change it from using 9 buttons to use just 1. What I have been trying to do for the last few days is make it so the one button can cycle through the different functions.
Any help would be massively appericated.

Thanks

Lee

Code: Select all

-- Load sounds
heading_sound = sound_add("heading.wav")
altitude_sound = sound_add("altitude.wav") 
vertspd_sound = sound_add("verticalspd_baro.wav") 
com1_sound = sound_add("stby_com1.wav")
com2_sound = sound_add("stby_com2.wav")
nav1_sound = sound_add("stby_nav1.wav")
nav2_sound = sound_add("stby_nav2.wav")
com1xfr_sound = sound_add("com1_txfr.wav")
com2xfr_sound = sound_add("com2_txfr.wav")
nav1xfr_sound = sound_add("nav1_txfr.wav")
nav2xfr_sound = sound_add("nav2_txfr.wav")
obs1_sound = sound_add("obs_one.wav")
obs2_sound = sound_add("obs_two.wav")
dirv1_sound = sound_add("direct_vor1.wav")
dirv2_sound = sound_add("direct_vor2.wav")
nogps_1_sound = sound_add("no_gps1.wav")
nogps_2_sound = sound_add("no_gps2.wav")
g530_1_sound = sound_add("G530_1.wav")
g530_2_sound = sound_add("G530_2.wav")
g1000_1_sound = sound_add("G1000_1.wav")
g1000_2_sound = sound_add("G1000_2.wav")
ignition_1_sound = sound_add("eng1_ign.wav")
ignition_2_sound = sound_add("eng2_ign.wav")
sync_hdg_sound = sound_add("sync_hdg.wav")
sync_alt_sound = sound_add( "sync_alt.wav")
sync_vs_sound = sound_add( "sync_vs.wav")
cursor_sound = sound_add( "cursor.wav")
inop_sound = sound_add("inop.wav")
engage_1_sound = sound_add("engage_start1.wav")
engage_2_sound = sound_add("engage_start2.wav")
release_sound = sound_add("release_start.wav")
-- Play the sound
--sound_play(mysound1)

local outer_accel=3
local outer_clockwise= nil
local outer_counter_clockwise = nil
local inner_accel=1
local inner_clockwise= nil
local inner_counter_clockwise = nil
local button_push = "none"
local button_release = "none"
local type_gps1 = "NONE"
local type_gps2 = "NONE"
local dir_bear1= 0
local dir_bear2 = 0
local heading_bug = 0
local alt_bug = 0
local obs_1 = 0
local obs_2= 0
local play_sync = nil

-- User properties
gps1_prop = user_prop_add_enum("No. 1 GPS Type", "G530,G1000,NONE","NONE", "Choose type for GPS 1 knob")
type_gps1 = user_prop_get(gps1_prop)
gps2_prop = user_prop_add_enum("No. 2 GPS Type", "G530,G1000,NONE","NONE",  "Choose type for GPS 2 knob")
type_gps2 = user_prop_get(gps2_prop)


--Button 1,1   Headingx10, Heading, HDG sync

function button11_pressed()
	sound_play(heading_sound)
	outer_accel=10
	outer_clockwise= "sim/autopilot/heading_up"
	outer_counter_clockwise = "sim/autopilot/heading_down"
	inner_accel=1
	inner_clockwise= "sim/autopilot/heading_up"
	inner_counter_clockwise = "sim/autopilot/heading_down"
	button_push = "sim/autopilot/heading_sync"
	button_release = "none"
	play_sync = sync_hdg_sound
end

hw_button_add("ARDUINO_NANO_A_D7", button11_pressed)

--Button 1,2   ALTx10, Alt, Alt sync

function button12_pressed()
	sound_play(altitude_sound)
	outer_accel=10
	outer_clockwise= "sim/autopilot/altitude_up"
	outer_counter_clockwise = "sim/autopilot/altitude_down"
	inner_accel=1
	inner_clockwise= "sim/autopilot/altitude_up"
	inner_counter_clockwise = "sim/autopilot/altitude_down"
	button_push = "sim/autopilot/altitude_sync"
	button_release = "none"
	play_sync = sync_alt_sound
end

hw_button_add("ARDUINO_NANO_A_D8", button12_pressed)

--Button 1,3   VS, baro, VS sync

function button13_pressed()
	sound_play(vertspd_sound)
	outer_accel= 1
	outer_clockwise= "sim/autopilot/vertical_speed_up"
	outer_counter_clockwise = "sim/autopilot/vertical_speed_down"
	inner_accel= 1
	inner_clockwise= "sim/instruments/barometer_up"
	inner_counter_clockwise = "sim/instruments/barometer_down"
	button_push = "sim/autopilot/vertical_speed_sync"
	button_release = "none"
	play_sync = sync_vs_sound
end

hw_button_add("ARDUINO_NANO_A_D9", button13_pressed)

--Button 2,1   Com 1 stby, Com 2 stby, flip coms

local  com_sel = 0
function button21_pressed()
	outer_accel= 1
	inner_accel= 1
	com_sel = math.abs( com_sel -  1)
		if com_sel == 1 then
		n= "1"
		sound_play(com1_sound)
	else
		n= "2"
		sound_play(com2_sound)
	end
	outer_clockwise= "sim/radios/stby_com"..n.."_coarse_up_833"
	outer_counter_clockwise = "sim/radios/stby_com"..n.."_coarse_down_833"
	inner_clockwise= "sim/radios/stby_com"..n.."_fine_up_833"
	inner_counter_clockwise = "sim/radios/stby_com"..n.."_fine_down_833"
	button_push = "com_toggle"..n
	button_release = "none"
	play_sync = nil
end

hw_button_add("ARDUINO_NANO_A_D10", button21_pressed)

--Button 2,2   Nav 1 stby, Nav 2 stby, flip navs
local  nav_sel = 0
function button22_pressed()
	outer_accel= 1
	inner_accel= 1
	nav_sel = math.abs( nav_sel -  1)
	if nav_sel == 1 then
		n= "1"
		sound_play(nav1_sound)
	else
		n= "2"
		sound_play(nav2_sound)
	end
	outer_clockwise= "sim/radios/stby_nav"..n.."_coarse_up"
	outer_counter_clockwise = "sim/radios/stby_nav"..n.."_coarse_down"
	inner_clockwise= "sim/radios/stby_nav"..n.."_fine_up"
	inner_counter_clockwise = "sim/radios/stby_nav"..n.."_fine_down"
	button_push = "nav_toggle"..n
	button_release = "none"
	play_sync = nil
end

hw_button_add("ARDUINO_NANO_A_D11", button22_pressed)

--Button 2,3   Obs 1, Obs 2, Toggle
local  obs_sel = 0
function button23_pressed()
	outer_accel= 10
	inner_accel= 1
	obs_sel = math.abs( obs_sel -  1)
	if obs_sel == 1 then
		n= "1"
		sound_play(obs1_sound)
	else
		n= "2"
		sound_play(obs2_sound)
	end
	outer_clockwise= "sim/radios/obs"..n.."_up"
	outer_counter_clockwise = "sim/radios/obs"..n.."_down"
	
	inner_clockwise= "sim/radios/obs"..n.."_up"
	inner_counter_clockwise = "sim/radios/obs"..n.."_down"
	button_push = "direct"..n
	button_release = "none"
	play_sync = nil
end

hw_button_add("ARDUINO_NANO_A_D12", button23_pressed)

--Button 3, 1   GPS1

function button31_pressed()
	outer_accel= 1
	inner_accel= 1
	if type_gps1 == "NONE" then
		sound_play(nogps_1_sound)
		outer_clockwise=  "none"
		outer_counter_clockwise =  "none"
		inner_clockwise=  "none"
		inner_counter_clockwise =  "none"
		button_push = "none"
		button_release = "none"
		play_sync = inop_sound
	elseif  type_gps1 == "G530"  then
		sound_play(g530_1_sound)
		outer_clockwise= "sim/GPS/g430n1_chapter_up"
		outer_counter_clockwise = "sim/GPS/g430n1_chapter_dn"
		inner_clockwise= "sim/GPS/g430n1_page_up"
		inner_counter_clockwise = "sim/GPS/g430n1_page_dn"
		button_push = "sim/GPS/g430n1_cursor"
		button_release = "none"
		play_sync = cursor_sound
	elseif  type_gps1 == "G1000"  then
		sound_play(g1000_1_sound)
		outer_clockwise= "sim/GPS/g1000n1_fms_outer_up"
		outer_counter_clockwise = "sim/GPS/g1000n1_fms_outer_down"
		inner_clockwise= "sim/GPS/g1000n1_fms_inner_up"
		inner_counter_clockwise = "sim/GPS/g1000n1_fms_inner_down"
		button_push = "sim/GPS/g1000n1_cursor"
		button_release = "none"
		play_sync = cursor_sound
	end
	
end

hw_button_add("ARDUINO_NANO_A_A5", button31_pressed)

--Button 3, 2   GPS2

function button32_pressed()
	outer_accel= 1
	inner_accel= 1
	if type_gps2 == "NONE" then
		sound_play(nogps_2_sound)
		outer_clockwise=  "none"
		outer_counter_clockwise =  "none"
		inner_clockwise=  "none"
		inner_counter_clockwise =  "none"
		button_push = "none"
		button_release = "none"
		play_sync = inop_sound
	elseif  type_gps2 == "G530"  then
		sound_play(g530_2_sound)
		outer_clockwise= "sim/GPS/g430n2_chapter_up"
		outer_counter_clockwise = "sim/GPS/g430n2_chapter_dn"
		inner_clockwise= "sim/GPS/g430n2_page_up"
		inner_counter_clockwise = "sim/GPS/g430n2_page_dn"
		button_push = "sim/GPS/g430n2_cursor"
		button_release = "none"
		play_sync = cursor_sound
	elseif  type_gps2 == "G1000"  then
		sound_play(g1000_2_sound)
		outer_clockwise= "sim/GPS/g1000n2_fms_outer_up"
		outer_counter_clockwise = "sim/GPS/g1000n2_fms_outer_down"
		inner_clockwise= "sim/GPS/g1000n2_fms_inner_up"
		inner_counter_clockwise = "sim/GPS/g1000n2_fms_inner_down"
		button_push = "sim/GPS/g1000n2_cursor"
		button_release = "none"
		play_sync = cursor_sound
	end
	
end

hw_button_add("ARDUINO_NANO_A_A4", button32_pressed)

--Button 3,3   Start 1, start 2, Toggle
local  start_sel = 0
function button33_pressed()
	outer_accel= 1
	inner_accel= 1
	start_sel = math.abs( start_sel -  1)
	if start_sel == 1 then
		n= "1"
		sound_play(ignition_1_sound)
	else
		n= "2"
		sound_play(ignition_2_sound)
	end
	outer_clockwise= "sim/ignition/ignition_up_"..n
	outer_counter_clockwise = "sim/ignition/ignition_down_"..n
	
	inner_clockwise= "sim/ignition/ignition_up_"..n
	inner_counter_clockwise =  "sim/ignition/ignition_down_"..n
	button_push = "starter"..n
	button_release = "stop"..n
	
end

hw_button_add("ARDUINO_NANO_A_A3", button33_pressed)

-- Outer Knob Encoder


-- Callback function which is called when the rotary encoder is turned
-- direction 1 : The dial turned clockwise
-- direciton -1: The dial turned counterclockwise
function outer_dial_change(direction)

  if direction == 1 then

		xpl_command(outer_clockwise)

	

  elseif direction == -1 then
  

		xpl_command(outer_counter_clockwise)

  end

end

-- Bind to Arduino A 
--hw_dial_add("ARDUINO_NANO_A_D3", "ARDUINO_NANO_A_D2", outer_accel, outer_dial_change)
hw_dial_add("OUTER_KNOB", outer_accel, outer_dial_change)
-- Inner Knob Encoder

function inner_dial_change(direction)

  if direction == 1 then

		xpl_command(inner_clockwise)


  elseif direction == -1 then
  

		xpl_command(inner_counter_clockwise)

  end

end

-- Bind to Arduino A 
--hw_dial_add("ARDUINO_NANO_A_D4", "ARDUINO_NANO_A_D5", inner_accel, inner_dial_change)
hw_dial_add("INNER_KNOB", inner_accel, inner_dial_change)

function button_released()
	if button_release == "stop1" then

		xpl_command("sim/ignition/ignition_up_1", 0)
		sound_stop(engage_1_sound)
		sound_play(release_sound)

	elseif button_release == "stop2" then

		xpl_command("sim/ignition/ignition_up_2", 0)
		sound_stop(engage_2_sound)
		sound_play(release_sound)

	end
end

function button_pressed()
	if button_push == "com_toggle1" then
	sound_play(com1xfr_sound)
		xpl_command("sim/radios/com1_standy_flip")
	elseif button_push == "com_toggle2" then
		xpl_command("sim/radios/com2_standy_flip")
		sound_play(com2xfr_sound)
	elseif button_push == "nav_toggle1" then
		xpl_command("sim/radios/nav1_standy_flip")
		sound_play(nav1xfr_sound)
	elseif button_push == "nav_toggle2" then
		xpl_command("sim/radios/nav2_standy_flip")
		sound_play(nav2xfr_sound)
	elseif button_push == "direct1" then 
		sound_play(dirv1_sound)
		xpl_dataref_write("sim/cockpit2/radios/actuators/nav1_obs_deg_mag_pilot","FLOAT", dir_bear1)
	elseif button_push == "direct2" then
		sound_play(dirv2_sound)
		xpl_dataref_write("sim/cockpit2/radios/actuators/nav2_obs_deg_mag_pilot","FLOAT", dir_bear2)
	elseif button_push == "starter1" then
		sound_play(engage_1_sound)
		xpl_command("sim/ignition/ignition_up_1", 1)
	elseif button_push == "starter2" then
		sound_play(engage_2_sound)
		xpl_command("sim/ignition/ignition_up_2", 1)
	else
		xpl_command( button_push )
		if play_sync ~= nil then  sound_play(play_sync) end
	end


end

hw_button_add("ARDUINO_NANO_A_D6", button_pressed, button_released)


function set_values( dir1, dir2, hdg, alt, obs1, obs2 )
dir_bear1= dir1
dir_bear2 = dir2
heading_bug = hdg
alt_bug = alt
obs_1 = obs1
obs_2= obs2
end

xpl_dataref_subscribe("sim/cockpit2/radios/indicators/nav1_bearing_deg_mag", "FLOAT","sim/cockpit2/radios/indicators/nav2_bearing_deg_mag", "FLOAT", "sim/cockpit/autopilot/heading_mag", "FLOAT", "sim/cockpit/autopilot/altitude", "FLOAT","sim/cockpit/radios/nav1_obs_degm", "FLOAT", "sim/cockpit/radios/nav2_obs_degm", "FLOAT", set_values)
Last edited by Gixxar on Thu Feb 11, 2021 4:00 pm, edited 1 time in total.

User avatar
Ralph
Posts: 7924
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: Multiple functions with 1 button

#2 Post by Ralph »

I'm not really sure how this setup will work. The button selects the function/mode and then you rotate the dial(s)?
It reminds me a bit of this old video.


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

Re: Multiple functions with 1 button

#3 Post by jph »

Ralph wrote: Thu Feb 11, 2021 3:17 pm I'm not really sure how this setup will work. The button selects the function/mode and then you rotate the dial(s)?
It reminds me a bit of this old video.

@Ralph
Nice one, thanks for posting that.. I had almost forgotten that one 8-) ;) :D
That was 100% a Steve Jobs wind up ---- :mrgreen:

predictive text ? roflmfao ... :lol:

'The Abortion went well'
'The Absinthe arrived by Airmail'
'The Aardvark asked for a dagger'

Got to admit ol' Steve was a bloody good windup merchant.

I think it was absolutely one of the best 'piss takes' / wind ups EVER... :lol:
Hilarious. :lol:
Image1.jpg
Joe. CISSP, MSc.

Post Reply