[youtube]https://www.youtube.com/shorts/XwgmqtrSQyc[/youtube]
I have successfully written code that controls an LED and wav file at the appropriate sequences in response to the switch positions. Though it may not be pretty in the eyes of a programmer . . . it works . . . mostly! Consequently, there are a couple of issues that I am in need of help with. The first question regards the possibility of suppressing the coded functions initially when the ELT hardware loads with the panel.
The functions are programmed to be triggered by the movement of the switch and in the Create/Edit tab in AM everything works exactly as intended. After clicking on Run nothing happens until I click on the lightbulb depicting the input from the switch. This is how it should be, however, when I connect the Arduino (Mega 2560), with the switch (SPST) and the LED (with resister) load the panel it detects the position of the switch and runs through the series of beeps and blinks associated with the present position of the switch. This does not happen IRL.
In the grand scheme of things this is not a HUGE deal but I don't understand why it is happening (although it may be blatantly obvious to you coding wizards!) and I'd like to resolve it if possible. My understanding of using "input_change (state)" is that it would only trigger when AM detects MOVING the switch and thus CHANGING the state but it is also triggering the function when the panel is loaded and the state of the switch is first read. Why does it only happen with the hardware connected but not in Create/Edit and is there a way to keep it from running the function initially on start up?
Thanks, in advance, for any help!
Paul
Code: Select all
-- Load sound
Beep = sound_add("beep.wav")
-- Add LED, extinguished
LED = hw_output_add("ELT LED", false)
function input_change (state)
-- Switch moved to "On"
if state == true then
--Stop any active running timers
print("SWITCH MOVED TO ON")
if timer_running(Long_Blink) then
timer_stop(Long_Blink) end
print("Long_Blink Timer Stopped")
if timer_running(Long_Blink_Out) then
timer_stop(Long_Blink_Out)end
print("Long_Blink_Out Stopped")
if timer_running(Four_Blink) then
timer_stop(Four_Blink) end
print("Four_Blink Stopped")
if timer_running(Four_Blink_Out) then
timer_stop(Four_Blink_Out) end
print("Four_Blink_Out Stopped")
if timer_running(First_Beep) then
timer_stop(First_Beep)end
print("First_Beep Stopped")
if timer_running(First_Beep_End) then
timer_stop(First_Beep_End)end
print("First_Beep_End Stopped")
if timer_running(Second_Beep) then
timer_stop(Second_Beep)end
print("Second_Beep Stopped")
if timer_running(Second_Beep_End) then
timer_stop(Second_Beep_End)end
print("Second_Beep_End Stopped")
if timer_running(Third_Beep) then
timer_stop(Third_Beep) end
print("Third_Beep Stopped")
if timer_running(Third_Beep_End) then
timer_stop(Third_Beep_End) end
print("Third_Beep_End Stopped")
if timer_running(Fourth_Beep) then
timer_stop(Fourth_Beep) end
print("Fourth_Beep Stopped")
if timer_running(Fourth_Beep_End) then
timer_stop(Fourth_Beep_End) end
print("Fourth_Beep_End Stopped")
if timer_running(Fifth_Beep) then
timer_stop(Fifth_Beep) end
print("Fifth_Beep Stopped")
if timer_running(Fifth_Beep_End) then
timer_stop(Fifth_Beep_End) end
print("Fifth_Beep_End Stopped")
-- Fast flash LED, play wav file continuously
sound_play(Beep)
Blink_Timer = timer_start(nil, 190, function(count)
hw_output_set(LED, count%2 == 0)
end)
-- Switch moved to "Arm"
elseif state == false then
print("SWITCH MOVED TO ARM")
-- Stop fast blink timer and stop playing wave file
if timer_running(Blink_Timer) then
timer_stop(Blink_Timer) end
print("Blink_Timer Stopped")
sound_stop(Beep)
print("Continuous Beep Stopped")
-- Timers set for LED and wav file to simulate ELT TEST mode
function long_blink(count, max)
hw_output_set(LED, true)
end
Long_Blink = timer_start(0, 1000, 1, long_blink)
function long_blink_out()
hw_output_set(LED, false)
end
Long_Blink_Out = timer_start(1000,0,1,long_blink_out)
function four_blink(count, max)
hw_output_set(LED, count%2 == 0)
print("Blink " .. count .. " of " .. max)
end
Four_Blink = timer_start(1000, 350, 10, four_blink)
function four_blink_out()
hw_output_set(LED, false)
end
Four_Blink_Out = timer_start(6200,0,1,four_blink_out)
function first_beep()
sound_play(Beep)
end
First_Beep = timer_start(1350,first_beep)
function first_beep_end()
sound_stop(Beep)
end
First_Beep_End = timer_start(1700,first_beep_end)
function second_beep()
sound_play(Beep)
end
Second_Beep = timer_start(2050,second_beep)
function second_beep_end()
sound_stop(Beep)
end
Second_Beep_End = timer_start(2400,second_beep_end)
function third_beep()
sound_play(Beep)
end
Third_Beep = timer_start(2750,third_beep)
function third_beep_end()
sound_stop(Beep)
end
Third_Beep_End = timer_start(3100,third_beep_end)
function fourth_beep()
sound_play(Beep)
end
Fourth_Beep = timer_start(3450,fourth_beep)
function fourth_beep_end()
sound_stop(Beep)
end
Forth_Beep_End = timer_start(3800,fourth_beep_end)
function fifth_beep()
sound_play(Beep)
end
Fifth_Beep = timer_start(4150,fifth_beep)
function fifth_beep_end()
sound_stop(Beep)
end
Fifth_Beep_End = timer_start(6200,fifth_beep_end)
end
end
inp_id = hw_input_add("ELT Switch", input_change)
state = hw_input_read(inp_id)