Kelly wrote further:
In the example below with a star delta type setup, where there is Pause instruction in the subroutine which is illegal. I guess I would have to move the StarDeltaStart subroutine into the Task1 routine itself.LaunchTask Task1
LaunchTask Task2
Task1
yieldtask
gosub StarDeltaStart
goto Task1
StarDeltaStart
on StarRelay
pause 200
Off StarRelay
pause 50
on DeltaRelay
return
See example at http://splatco.com/skb/3439.htm The secondary function is relegated to a secondary task, with semaphores used to signal commands and completion. It is also possible to launch a task, have it do its job and then kill itself off, perhaps setting a semaphore before it dies. Remember that unlike say Visual Basic, the variables are static and global, and so exist for the life of the program, not just the life of the task. Using this idea, your program would become:
LaunchTask MainI've omitted the equate and defSEM declarations.
RunTasksForever
Main: ClrS sStarted ;Make sure the 'all done' semaphore is clear.
LaunchTask StarDeltaStart
WaitForST sStarted
;continue ....
StarDeltaStart:
On oStarRelay
Pause 200
Off oStarRelay
Pause 50
On oDeltaRelay
SetS sStarted
KillTask ;Rather than return
StarDeltaStart can now be "called" from several places in the program. This may appear slightly inconvenient compared to a simply subroutine call. Think of StarDeltaStart however as a process rather than an action. There is no reason why Main cannot initiate the startup, go off and do a few other things and then later check if the motor is yet running.
Notice that I clear the sStarted semaphore before launching the StarDeltaStart task. I can't clear it in StarDeltaStart because the LaunchTask action only enters the task into a queue of tasks waiting to execute. It does not immediately execute any of the code in StarDeltaStart. StarDeltaStart code will only actually execute sometime after Main has yielded (depending on how many other tasks are also waiting to run).
Hazard alert!
If you have several tasks that can start the motor, you could risk having two copies of StarDeltaStart running at once. That may not be beneficial to the health of the motor or your fuses. In that case StarDeltaStart could have another semaphore to ensure only one copy can be running at once. It is not Best Practice to allow several tasks to control a single resource, for the very reason that those kinds of conflict can arise.
