Making Priority code

ZOR

Senior Member
I am looking at using multitasking on a 14M2.

I will have a main chunk of code in Main: which is within a Do-Loop line of code,

main:
Do
Code progressivly working down to Loop to start again
Loop

I want an instant as possible instruction to drop where the code is to go back to main: and restart again. Obviously I will reset any variable values.

As it trundles through the code, do I put my instruction to return to start if a port goes high in multiple places down the code or can I tell it from another loop running in start1: to do that.

ie:
Start1:
Do
If pinC.0 = 1 then
say something in here to make the other code go back to main?
end if
Loop

Sorry if I am not so clear what I mean, I am just trying to find how I can reset my main code asap against an event.

Code not written yet, just pre thinking
 

eggdweather

Senior Member
Wouldn't it be better to consider the use of interrupts to provide the best solution to fast responses? That way the structure of your main body of code would be unimportant.

"A polled interrupt is a quicker way of reacting to a particular input combination. It is the only type of interrupt available in the PICAXE system. The inputs port is checked between execution of each command line in the program, between each note of a tune command, and continuously during any pause command. If the particular inputs condition is true, a 'gosub' to the interrupt sub-procedure is executed immediately. When the sub-procedure has been carried out, program execution continues from the main program."
 

ZOR

Senior Member
Many thanks, I have never used interrupts before, will go to manual 2 and hope it's there.
 

hippy

Technical Support
Staff member
With a bit of hoop-jumping (*) you can use a SELECT CASE as a priority handler ...

Code:
b27 = 1
Do
  Select Case b27
    Case pinC.0: ...
    Case pinC.1: ...
    Case pinC.2: ...
  End Select
Loop
You can loop before the SELECT CASE until any of the pins you want to act on are set and this will also prevent the DO-(SELECT-CASE)-LOOP being halfway through the loop when an earlier and later pin are set causing the later pin to get handled first.

You can also use SELECT-CASE in the same way within an interrupt routine.

(*) The hoop-jumping is that SELECT CASE cannot have a number specified, so the 1 bit you want to match with a pin input has to be put in a variable. Only once if you never alter that variable.
 

ZOR

Senior Member
Thanks hippy, I remember the select case from my visual basic days, used to use for selecting options/buttons. So not so sure how to use in do/loop progressive code.

Do I break up my code in some way and put in select choice sections?
At the moment I am trying to think where I can intercept lines of code with repetitive If/Then/Else statements, to catch the command to restart the program back to beginning. I thought it might be possible to give a command from one sub program to another. I did try setting a port high in a sub program, and then looked for it being high in the main program to react to. I thought in multitasking I would pick up an event quickly outside the main program to then tell the main program what to do.
 

hippy

Technical Support
Staff member
Do I break up my code in some way and put in select choice sections?
No idea. You are going to have to explain in more detail what you are actually trying to achieve, what you want your program to do, what data it is using and manipulating, to be able advise on the best way to do it.
 

ZOR

Senior Member
Sorry if I was not clear, having done no real code at time, just planning.
Basically I had a main program on a do/loop having a few pauses used. I wanted to stop the main code and make it start/go to the beginning again if a hall switch went high in a parallel task bit of code. In the parallel task code, the hall switch made a port go high, which I wanted the main program to see and go back to the beginning of its code.

Because I had pauses in do/loop configuration in main code, it delayed the instruction of the hall effect being seen. I then re did my main code using For/Next loops without pauses using word variable so it could be a long For/Next and before each next it looked for the port being made high and was then able to act on it more quickly.

Probably better ways of doing it but it now works okay. Sorry for delay hippy getting back. Hope this makes it clearer what I was trying to do.
 

hippy

Technical Support
Staff member
You can use the RESTART command to force a task to restart from its beginning.

For example; this will restart the Start0 counting loop whenever input C.1 is taken high, monitored by the Start1 task -

Code:
#Picaxe 08M2

Start0:
  SerTxd ( "Start" )
  w0 = 0
  Do
    Pause 100
    SerTxd( " ", #w0 )
    w0 = w0 + 1
  Loop

Start1:
  Do
    Do : Loop Until pinC.3 = 0
    Do : Loop Until pinC.3 = 1
    Restart 0
  Loop
 

ZOR

Senior Member
Thank you hippy, thats exactly what I wanted. I will redo things to incorporate that example. Sorry I was not clear earlier. Best regards
 
Top