Ralph wrote: ↑Tue May 11, 2021 6:17 pm
Maybe not the best way to do it... it's late and I'm tired, so excuse me if this can be done better, but it seems to work.
This is assuming that a negative battery load means the battery is discharging. You might have to check that, but it sounds correct.
Code: Select all
output_batw = hw_output_add("Battery warning light", false)
local charging_failed = false
local reset = true
fsx_variable_subscribe("ELECTRICAL BATTERY LOAD", "Amperes",
"ELECTRICAL MASTER BATTERY", "Bool",
"GENERAL ENG MASTER ALTERNATOR:1", "Bool", function(load, bat_on, gen_on)
if bat_on and gen_on and load < 0 and not charging_failed and reset then
charging_failed = true
reset = false
elseif charging_failed and not bat_on and not reset then
reset = true
elseif charging_failed and load >= 0 and bat_on and reset then
charging_failed = false
end
hw_output_set(output_batw, charging_failed)
end)
Ralph, when I get to the simulator in person today I will try this out.
My LUA scripting skills have greatly improved over the past week. Of all things I sourced help from MINECRAFT modding community who loved the challenge.
In 6 hours and 5 of us knocking our communal singular brain cell together we managed to make a piece of code that does exactly what I want.
We are in the process of optimizing it since the code was made originally in Minecraft and converted it to the P3D/Air Manager specific functions and variables.
https://www.youtube.com/watch?v=R80Xr0N-_48
Code we used below.
Code: Select all
err_batt = false
gen_1_status = true
gen_2_status = true
switchState = 1
batteryStatus = false
charging = false
restirct = false
switch_onoff = 0
--Led Definitions
--masterwarn_error_led = hw_led_add("Arduino_MEGA_2560_A_D10", 0)
--mastercaution_error_led = hw_led_add("Arduino_MEGA_2560_A_D19", 0)
batt_error_led = hw_led_add("Arduino_MEGA_2560_A_D13", 0)
gen1_error_led = hw_led_add("Arduino_MEGA_2560_A_D12", 0)
gen2_error_led = hw_led_add("Arduino_MEGA_2560_A_D11", 0)
nws_caution_led = hw_led_add("Arduino_MEGA_2560_A_D11", 10)
--Relay Definitions
--Sounds
horn = sound_add ( "horn.wav", 1)
function batt_switch_handler(state)
print("batt_switch_handler - 1: " .. switchState)
if err_batt == true then
if switchState > 2 then
switchState = 1
else
switchState = switchState + 1
end
else
switchState = 1
end
switch_onoff = state
fsx_variable_write("ELECTRICAL MASTER BATTERY", "Number", state)
print("batt_switch_handler - 2: " .. switchState)
checkStatus()
end
function genalt1_switch_handler(position)
print("Alt 1 Position changed " .. position)
if position == 0 then
fsx_variable_write("GENERAL ENG GENERATOR ACTIVE:1", "BOOL", false)
elseif position == 1 then
fsx_variable_write("GENERAL ENG GENERATOR ACTIVE:1", "BOOL", true)
end
end
function genalt2_switch_handler(position)
print("Alt 2 Position changed " .. position)
if position == 0 then
fsx_variable_write("GENERAL ENG GENERATOR ACTIVE:2", "BOOL", false)
elseif position == 1 then
fsx_variable_write("GENERAL ENG GENERATOR ACTIVE:2", "BOOL", true)
end
end
------------------
batt_switch = hw_switch_add("Arduino_MEGA_2560_A_D2", 1, batt_switch_handler)
genalt1_switch = hw_switch_add("Arduino_MEGA_2560_A_D3", 1, genalt1_switch_handler)
genalt2_switch = hw_switch_add("Arduino_MEGA_2560_A_D4", 1, genalt2_switch_handler)
function checkErrorLight()
if err_batt == true then
print("Battery Light Status is true, enabling LED")
hw_led_set(batt_error_led, 1) -- Enable LED
else
print("Battery Light Status is false, disabling LED")
hw_led_set(batt_error_led, 0) -- Disable LED
end
end
function isBatteryCharging()
if batteryStatus == false then
err_batt = true
else
if switchState == 2 then
err_batt = false
switchState = 1
print("Battery Switch has been flipped from off to on, twice")
end
end
end
function checkStatus()
--print("checkStatus() has run!")
if err_batt == true and switch_onoff == 1 then
print("checkStatus 1: " .. switchState)
if err_batt == true then
restirct = false
end
elseif err_batt == true and switch_onoff == 0 then
if switchState < 2 and restirct == false then
--print("checkStatus 2: " .. switchState)
switchState = switchState + 1
--print("checkStatus 3: " .. switchState)
restirct = true
end
end
isBatteryCharging()
checkErrorLight()
end
-----generator functions were here then moved below battery status
function handle_charging(status)
charging = status
print(status)
if status == false then
batteryStatus = false
sound_play ( horn )
else
batteryStatus = true
end
checkStatus()
end
function Master_Battery_Status(status)
end
function setGenerator1(status)
gen_1_status = status
checkStatus()
end
function setGenerator2(status)
gen_2_status = status
checkStatus()
end
-----------Alternator / Generators 1 and 2 Warning Lights BASIC -------------------
--function num(bool)
--if bool then return 1 else return 0 end
--end
--function setGenerator1(charging)
-- hw_led_set(gen1_error_led, (charging))
--end
--function setGenerator2(charging)
-- hw_led_set(gen2_error_led, (charging))
--end
function setGenerator1(charging)
if charging == 28 then
print("Gen 1 Volts " .. charging)
hw_led_set(gen1_error_led, 0)
else
hw_led_set(gen1_error_led, 1)
print("Gen 1 Volts " .. charging)
--sound_play ( horn )
end
end
function setGenerator2(charging)
if charging == 28 then
print("Gen 2 Volts " .. charging)
hw_led_set(gen2_error_led, 0)
else
hw_led_set(gen2_error_led, 1)
print("Gen 2 Volts " .. charging)
--sound_play ( horn )
end
end
fsx_variable_subscribe("ELECTRICAL GENALT BUS VOLTAGE:1", "Volts", setGenerator1)
fsx_variable_subscribe("ELECTRICAL GENALT BUS VOLTAGE:2", "Volts", setGenerator2)
fsx_variable_subscribe("ELECTRICAL BATTERY IS CHARGING", "Bool", handle_charging)
fsx_variable_subscribe("ELECTRICAL MASTER BATTERY", "Bool", Master_Battery_Status)