I am learning Air Manager and LUA as I’m going along, so it’s a bit of a steep learning curve. I’ve got a bit stuck on the Clock/Chrono/Elapsed time display in the centre panel of the Toliss A321 I’m coding.
I would like to have the chrono to only display when Volts >= 10, and to disappear when I press the RST button. The clock needs to be on display all the time. I’m having a lot of trouble getting this to work, even though I have watched Tony’s YouTube tutorials and watched a 3-hour crash course on LUA! I seem to either have it on all the time, or it stays invisible, or the timer doesn’t run.
With the current code, it’s just staying on all the time.
Once that is sorted, I will want to start to code the Elapsed time Chrono.
Could someone take a look and show me the error of my ways, please?
Thanks in advance!
Code: Select all
------------------------------------------
--A321 Center Clock --
--Mark B 22122022 --
--Version 1.0 --
------------------------------------------
------------------------------------------
-- Load and display images in Z-order --
------------------------------------------
img_add_fullscreen("Background.png")
img_gpsswch = img_add("switch.png", 393, 215, 50, 85)
img_runswch = img_add("switch.png", 393, 340, 50, 85)
img_datebtn = img_add("large_button.png", 26, 218, 80, 80)
-- variables --
local timer_state = 0 -- 0: Stopped and reset 1:running 2:stopped
local timer_value = 0
local timer_display = 0
-- Fonts --
chrfont = "font:digital-7-mono.ttf; size:60px; color: #FFFAD0; halign: center;"
clkfont_hrs_mins = "font:digital-7-mono.ttf; size:70px; color: #FFFAD0; halign: center;"
clkfont_secs = "font:digital-7-mono.ttf; size:55px; color: #FFFAD0; halign: center;"
-- Texts --
txt_timer = txt_add(" ", chrfont, 162, 102, 158, 60)
txt_clock_hrs_mins = txt_add(" ", clkfont_hrs_mins, 117, 230, 175, 60)
txt_clock_secs = txt_add(" ", clkfont_secs, 288, 237, 100, 60)
-- Buttons and Switches --
function chrbutton_pressed()
timer_state = timer_state + 1
if timer_value == 0 and timer_state == 1 then
timer_value = -1
end
if timer_state ==2 then
timer_state = 0
end
end
function rstbutton_pressed()
timer_state = 0
timer_value = 0
timer_display = 0
end
button_add("button.png", "button_press.png", 392,120,60,60,chrbutton_pressed)
button_add("button.png", "button_press.png", 35,120,60,60,rstbutton_pressed)
-- Timer----------------------------------------------
------------------------------------------------------
function timer_callback()
if timer_state == 1 then
timer_value = timer_value + 1
end
txt_set(txt_timer, string.format("%02.0f:%02.0f", math.floor((timer_value / 60) % 60), (timer_value % 60)) )
function bus_volts(volts)
if volts[1] >= 10 then timer_display = 1
else timer_display = 0
visible(txt_timer, timer_display == 1)
end
xpl_dataref_subscribe ("sim/cockpit2/electrical/bus_volts", "FLOAT[6]", bus_volts)
end
end
-- Clock ----------------------------------------------
-------------------------------------------------------
function new_xpl_data(tZulu_hours, tZulu_minutes, tZulu_seconds)
txt_set(txt_clock_secs, string.format("%02.0f", (tZulu_seconds)))
txt_set(txt_clock_hrs_mins, string.format("%02.0f:%02.0f", (tZulu_hours), math.floor(((tZulu_minutes/60)%1)*60)))
end
-- Data Refs --
xpl_dataref_subscribe
("sim/cockpit2/clock_timer/zulu_time_hours", "INT",
"sim/cockpit2/clock_timer/zulu_time_minutes", "INT",
"sim/cockpit2/clock_timer/zulu_time_seconds", "INT", new_xpl_data)
timer_start(0,1000,timer_callback)