Thanks to @JackZ and @Gilles for looking into it further and apologies to @Ralph for not understanding at first. I did some more testing and found that I can get it to work reliably with v3.3 of the plugin but not v3.4 Beta.
Originally, I had a timer function added in to blink an LED (at a 4 second duty cycle) when the battery voltage dropped below a threshold. That seems to break the whole switch LUA script, which used to work. Also had a user property for engine/battery select that would modify the string to send for either battery 1 or battery 2. I'll save that debugging for another day!
Using this script below, with the v3.3 plugin, I have working Arduino hardware battery switches (and indicator LEDs) working again. Coding with LUA is still weird for me, given the required order of things is not intuitive for me, like "regular" Arduino sketches. That's my problem, not yours. Here's what works for now:
Code: Select all
led1 = hw_output_add("Batt 1 LED", false)
led2 = hw_output_add("Batt 2 LED", false)
function check_val1(val1)
--print(tostring(val1))
if (val1 == true) then
hw_output_set(led1, true)
else
hw_output_set(led1, false)
end
end
function check_val2(val2)
--print(tostring(val2))
if (val2 == true) then
hw_output_set(led2, true)
else
hw_output_set(led2, false)
end
end
fs2020_variable_subscribe("ELECTRICAL MASTER BATTERY:1", "Bool", check_val1)
fs2020_variable_subscribe("ELECTRICAL MASTER BATTERY:2", "Bool", check_val2)
function battery1_switch_callback(pos)
if(pos == 1) then
fs2020_variable_write("ELECTRICAL MASTER BATTERY:1", "Bool", true)
else
fs2020_variable_write("ELECTRICAL MASTER BATTERY:1", "Bool", false)
end
end
function battery2_switch_callback(pos)
if(pos == 1) then
fs2020_variable_write("ELECTRICAL MASTER BATTERY:2", "Bool", true)
else
fs2020_variable_write("ELECTRICAL MASTER BATTERY:2", "Bool", false)
end
end
hw_switch_add("Battery 1 switch", 1, battery1_switch_callback)
hw_switch_add("Battery 2 switch", 1, battery2_switch_callback)