Your code and reply definitely show that you don’t (yet) understand how a variable_subscribe() works.
https://siminnovations.com/wiki/index.p ... _subscribe
And also how Lua works.
Each time a data ref/value in a variable_suscribe() changes in the sim, the AM plugin informs AM and the corresponding function is called, with the value passed as an argument to the fonction called.
Code: Select all
ed_alt1 = hw_led_add("low amps left", 0.0)
if electrical_genalt_bus_amps[2] < 20 then
hw_led_set(led_alt1, 1)
else
hw_led_set(led_alt, 0)
end
fs2020_variable_subscribe("ELECTRICAL GENALT BUS AMPS", "amps", new_data_fs2020)
In the code you posted the function to be called should be
new_data_fs2020. It is not defined anywhere.
And in Lua, any function should be defined BEFORE any call.
Moreover you are using
electrical_genalt_bus_amps[2] which is not defined anywhere and hence Lua consider it as a nil value and throws an error.
So your code should be instead:
Code: Select all
led_alt1 = hw_led_add("low amps left", 0.0)
function new_data_fs2020(electrical_genalt2_bus_amps)
if electrical_genalt2_bus_amps < 20 then
hw_led_set(led_alt1, 1)
print("Led ON")
else
hw_led_set(led_alt1, 0)
print("Led OFF")
end
end
fs2020_variable_subscribe("ELECTRICAL GENALT BUS AMPS:2", "Amperes", new_data_fs2020)
Provided you are watching generator two (hence the :2 index) amps value only.
Note that we used ELECTRICAL GENALT BUS AMPS:2 with the index here, and not in the variable itself. So if you wanted to watch both generator values, you should subscribe to two different values and your code would be like.
Code: Select all
led_alt1 = hw_led_add("low amps left", 0.0)
function new_data_fs2020(electrical_genalt1_bus_amps,electrical_genalt2_bus_amps)
if (electrical_genalt1_bus_amps < 20) or (electrical_genalt2_bus_amps < 20) then
hw_led_set(led_alt1, 1)
print("Led ON")
else
hw_led_set(led_alt1, 0)
print("Led OFF")
end
end
fs2020_variable_subscribe("ELECTRICAL GENALT BUS AMPS:1", "Amperes","ELECTRICAL GENALT BUS AMPS:2", "Amperes", new_data_fs2020)
And to be more accurate, you should only light the warning light when some voltage is available on the MAIN ELEC BUS (ie the MAster is ON and battery voltage is above 24volts)
Code: Select all
led_alt1 = hw_led_add("low amps left", 0.0)
function new_data_fs2020(electrical_genalt1_bus_amps,electrical_genalt2_bus_amps,electrical_mainbus_volts)
if ((electrical_genalt1_bus_amps < 20) or (electrical_genalt2_bus_amps < 20))and electrical_mainbus_volts>24 then
hw_led_set(led_alt1, 1)
print("Led ON")
else
hw_led_set(led_alt1, 0)
print("Led OFF")
end
end
fs2020_variable_subscribe("ELECTRICAL GENALT BUS AMPS:1", "Amperes","ELECTRICAL GENALT BUS AMPS:2", "Amperes","ELECTRICAL MAIN BUS VOLTAGE","Volts", new_data_fs2020)