You are creating the thread inside of a function.
Variables created inside of a function have a local scope and are not automatically accessible outside of that function.
You can fix this by adding the keyword global before warning_1 when you run the thread (inside def start_flash())
def start_flash():
global warning_1 = run flash_yellow()
end
warning_1 will then be accessible inside all other functions.
I just double checked because I thought I ran into a similar thing with my CAP, and it looks like I just set it equal to 0 at the start lol. So in your beforeStart sequence you could just say warning_1 = 0.
I’ve tried setting it to zero and setting it to False - neither seems to work.
If I just start the flasher - I get a quick flash and all is good.
However, the reason I’m doing this is to cancel the flashing light if it’s already on (rack full warning).
In that case, when I press the button it keeps flashing - the flashing changes slightly when the button is pressed, but then continues on flashing.
I haven’t found a way to actually kill the flasher thread from within the Event.
I also tried using the Event to set a variable, then used a separate thread to watch that variable & try to kill the thread. The only thing that would accomplish was, depending on where in the flash cycle the button was pressed, was to change the frequency of the flash - making it strobe instead of .5s on/ .5s off.
Summary - If I start and kill a flasher thread within the Event (button press), it works. If the flasher thread is already running, I can’t seem to kill it with either an Event or another Thread.
Instead of running a thread when its needed I would always run a thread and set a variable.
It could look like this:
global WarningLevel = 0
thread Signal_Light():
while True:
if WarningLevel == 1:
# Flash DO5 - Yellow
set_configurable_digital_out(5, True)
sleep(0.5)
set_configurable_digital_out(5, False)
sleep(0.5)
elif WarningLevel == 2:
# Flash DO6 - Red
set_configurable_digital_out(6, True)
sleep(0.5)
set_configurable_digital_out(6, False)
sleep(0.5)
end
sync()
end
signallamp = run Signal_Light()
The Thread runs always.
It checks what the warning level is and flashes the corresponding light.
You dont have to kill and rerun it.
If you want the light to start or stop flashing you simply change the warning level.
When I run the program, the green light comes on for the first 1.5s pulse, then goes out and nothing else lights up – even though I see it scrolling through each of the wait commands.
I tried breaking it up into separate threads with no change.
Wrap the whole thread’s executing code in a “while(true):” loop. Your thread is executing all the way through to the final “End” command, at which point it’s terminating.
I typically let all my threads run all the time (unless you start pushing the 50 thread limit) and do so like this:
thread myThread():
while(true):
if(runThreadVariable == False):
sync()
else:
#put your executing code here
end
end
end
This lets you use “runThreadVariable” anywhere in your main program to, essentially, “turn on/off” the thread without actually worrying about initializing and killing. And as you can see, the “while true” loop is what keeps it always checking.