Cessna 172 Altimeter Rotary Encoder Problems

Questions about deployment and use of Air Manager Instruments

Moderators: russ, Ralph

Post Reply
Message
Author
Cloudsurfer
Posts: 12
Joined: Wed Oct 14, 2020 7:27 pm

Cessna 172 Altimeter Rotary Encoder Problems

#1 Post by Cloudsurfer »

Hey guys, I have nearly completed my Homecockpit. The last steps are programming all my rotary encoders for each instrument. I use the Cessna 172 Altimeter and have added the code for the rotary encoder. For the rotary encoder I have the Arduino Mega 2560 and for the monitor I use an Raspberry Pi 4 with Air Player. Everything is correctly shown on the monitor. But when I want to change the QNH via the rotary encoder nothing moves. After changing back to Air Manager and pressing "Play" to test everything it works perfectly (also in the simulator). As soon as I click "Stop" it also stops working in the Simulator. I Have saved the latest version of my instruments on the Air Player. I can show you my code, maybe there is a mistake I don't see.

Thanks for your help,
Jakob

Code: Select all

---------------------------------------------
-- Altimeter --
-- Modification of Jason Tatum's original --
-- Brian McMullan 20180324 --
-- Property for background off/on --
-- Property for dimming overlay --
---------------------------------------------

---------------------------------------------
-- Properties --
---------------------------------------------
prop_BG = user_prop_add_boolean("Background Display",true,"Hide background")
prop_DO = user_prop_add_boolean("Dimming Overlay",false,"Use Dimming overlay")

---------------------------------------------
-- Load and display images in Z-order --
-- Loaded images selectable with prop --
---------------------------------------------
setdisk = img_add("altimeterCard.png", 15, 0, 512, 512)
if user_prop_get(prop_BG) == false then
img_add_fullscreen("altimeter.png")
else
img_add_fullscreen("altimeterwBG.png")
end
needle_10000 = img_add_fullscreen("altimeterdisc.png")
needle_1000 = img_add_fullscreen("needle1000.png")
needle_100 = img_add_fullscreen("needle100.png")
if user_prop_get(prop_DO) == true then
img_add_fullscreen("dimoverlay.png")
end
img_add("altknobshadow.png",31,400,85,85)

---------------------------------------------
-- Functions --
---------------------------------------------
function new_alt(alt)
if alt == -1 then
xpl_command("sim/instruments/barometer_down")
fsx_event("KOHLSMAN_DEC")
elseif alt == 1 then
xpl_command("sim/instruments/barometer_up")
fsx_event("KOHLSMAN_INC")
end
end

function new_altitude(altitude, pressure)
k = (altitude/10000)*36
h = ( (altitude - math.floor(altitude/10000)*10000)/1000 )*36
t = ( altitude - math.floor(altitude/10000)*10000 )*0.36

rotate(needle_10000, k)
rotate(needle_1000, h)
rotate(needle_100, t)

kk = k/3.6
hh = h/36
tt = t/0.36-hh*1000

barset = var_cap(pressure, 29.1, 30.7)

rotate(setdisk, (((barset - 29.1) * 160 / 1.6) * -1)+0.6)
end

---------------------------------------------
-- Controls Add --
---------------------------------------------
dial_alt = dial_add("altknob.png", 31, 400, 85, 85, new_alt)
dial_click_rotate(dial_alt,1)
hw_dial_add("Barometer dial", 3, new_alt)
---------------------------------------------
-- Simulator Subscriptions --
---------------------------------------------

xpl_dataref_subscribe("sim/cockpit2/gauges/indicators/altitude_ft_pilot", "FLOAT",
"sim/cockpit/misc/barometer_setting", "FLOAT", new_altitude)
fsx_variable_subscribe("INDICATED ALTITUDE", "Feet",
"KOHLSMAN SETTING HG", "inHg", new_altitude)

---------------------------------------------
-- END Altimeter --
---------------------------------------------

local button_state = false

function button_pressed(pressed)
button_state = true
--print(button_state)
end

function button_released(released)
button_state = false
--print(button_state)
end

function dial_callback(dir)
if button_state and dir == 1 then
print("clockwise while pushed")
end
if button_state and dir == -1 then
print ("counter-closckwise while pushed")
end
if not button_state and dir == 1 then
print("clockwise")
end
if not button_state and dir == -1 then
print("counter-clockwise")
end
end

hw_dial_id = hw_dial_add("Barometer dial", "TYPE_1_DETENT_PER_PULSE", dial_callback)
hw_button_id = hw_button_add("Barometer button", button_pressed, button_released)

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

Re: Cessna 172 Altimeter Rotary Encoder Problems

#2 Post by Sling »

I assume you are plugging the Arduino into the Rpi. It won’t work if you don’t do that. As far as the code goes you have 2 hardware dials added. There is no need for your one at the end. You can just add whatever extra code you need for the button held scenario into the existing hardware dial callback function.
Last edited by Sling on Mon Jan 11, 2021 2:33 pm, edited 2 times in total.

Cloudsurfer
Posts: 12
Joined: Wed Oct 14, 2020 7:27 pm

Re: Cessna 172 Altimeter Rotary Encoder Problems

#3 Post by Cloudsurfer »

Thanks for your advice, now everything works in the Sim!

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

Re: Cessna 172 Altimeter Rotary Encoder Problems

#4 Post by Sling »

No worries. I’m glad it helped.

Post Reply