08M2 Parallel tasking code help needed please.

jbsmith1

New Member
Hi,
New to picaxe and trying my best to learn the code, but I would like some help solving this code issue please.:)

I have code working in the simulator, but when I try it on the chip it stops parallel tasking.

The objective is:
Press the button, pot value is read and LED0 then lights for pot period and goes out. At the same time in the second task when the button is pressed LED1 lights for 5 seconds and then goes out.

This all works in the simulator, but when on the chip, LED1 lights for its 5 seconds, but LED0 only lights for a fixed minimum time period regardless of the pot value.

Any idea's what the issue is or suggestions for alternative code, any help would be most appreciated.

Code:
08M2
SYMBOL LED1 = 0
SYMBOL LED2 = 1
SYMBOL POT1 = 4
SYMBOL TRIGGER = pin3

SYMBOL Temp = B1

START0:
ReadADC POT1, Time
IF TRIGGER = 1 THEN START0:

HIGH LED1
FOR TEMP = 1 TO Time
PAUSE 1
NEXT TEMP
LOW LED1
GOTO START0:

START1:
IF TRIGGER = 0 THEN HIGH LED2
PAUSE 5000
LOW LED2
ENDIF
GOTO START1:
 

AllyCat

Senior Member
Hi,

I'm a little confused that your text refers to LED0 and LED1, but the program to LED1 and LED2. Also, Pin 0 is the Serial/Programming output which is better not used if others are available, so I'd begin by symbolising LED1 = 1 and LED2 = 2.

However, I suspect that your problems are that "Time" is a "System Variable" (a Reserved Word) which automatically increments once each second. Also, you are using the same variable in both tasks and loading an (ADC) value, of up to 255, which is not compatible with a 5 second timing period (in seconds or in ms). The Simulator has to operate at a much slower speed than a "real" program, so it cannot reliably demonstrate/verify how the program will actually run.

So, I suggest that you that create two new symbols Time1 and Time2 and try those in the program, (although I'm not clear how the two "time"s are intended to inter-react between the two tasks). For example:
Code:
#PICAXE 08M2
SYMBOL LED1   = 1      ; Or preferably the (modern) full Port.Pin declaration  C.1   etc....
SYMBOL LED2   = 2
SYMBOL POT1    = 4
SYMBOL TRIGGER = pin3      ; Or preferably the full Port.Pin declaration  pinC.3
SYMBOL Temp    = B1
SYMBOL Time1 = B2
SYMBOL Time2 = B3
;    etc.
But personally, I've never found the need to use multiple tasks, they often seem to cause more confusion than a simple polling loop.

Cheers, Alan.
 

jbsmith1

New Member
Hi Alan,
Thank you for your reply.

Ok I changed to C.1 and C.2 for LED1 and 2. I used Time1 Time2 and B2 but not sure where to put B3?
I tried the code change below and it worked in sim but not on the board.:(
I have tried to understand and read about looping but I can't get my head round how to change all the code to work that way.
Any help further help to get this code working on the board would really be appreciated as I'm a bit lost at how to start any changes.

The code change I tried, but I think I probably didn't implement it right.:(

08M2
SYMBOL LED1 = C.1
SYMBOL LED2 = C.2
SYMBOL POT1 = C.4
SYMBOL TRIGGER = pinC.3
SYMBOL Temp = B1
SYMBOL Time1 = B2
SYMBOL Time2 = B3

START0:
ReadADC POT1, Time1
IF TRIGGER = 1 THEN START0:

HIGH LED1
FOR TEMP = 1 TO Time
PAUSE 1
NEXT TEMP
LOW LED1
GOTO START0:

START1:
IF TRIGGER = 0 THEN HIGH LED2
PAUSE 5000
LOW LED2
ENDIF
GOTO START1:
 

AllyCat

Senior Member
Hi,

You still have a "Time" in the LED1 loop instead of "Time1", but that loop will give only around 2.5 ms per pass, so a maximum of around 650 ms. You need a longer PAUSE 20 in the loop, or simply calculate the full delay (e.g. Time1 = Time1 * 20 ) and use a simple PAUSE Time1 as in the LED2 loop (In this case "Time1" will need to be a Word variable, e.g. SYMBOL Time1 = W2)..

It's difficult to predict what might happen (or be required to happen) if the button is repeatedly pressed with short(er) intervals than 5 seconds, but I think the following simple loop might do what you want (Symbol declarations not shown again):
Code:
START:
  ReadADC POT1, Time1                        ; Read a value in the range 0 to 255
  IF TRIGGER = 1 THEN GOTO START      ; Wait for button press
  HIGH LED1
  HIGH LED2
  FOR TEMP = 0 TO 255  
    PAUSE 20                                  ; 20 ms * 255 = ~ 5 seconds
    IF TEMP  => Time1 THEN
      LOW LED1
    ENDIF
  NEXT TEMP
  LOW LED2
GOTO START
Cheers, Alan.
 

jbsmith1

New Member
Hi Alan,
Thank you very much for taking the time for that code. I tried it and it is close, but not quite how I need it. I tried playing with it but couldn't get the results I need.
This code runs in a sequence which is what I struggle to work with as I need two separate operations to take place simultaneously with one button push.
Like my first code runs in the sim, this is what I'm trying to achieve. I press the button and LED1 lights for the time set by the pot (0>1sec) and at the same time LED2 lights for 5 seconds but I can still re trigger LED1 with a button press during LED2 on timing period.

Any help with further code suggestions would really be appreciated.

James
 

AllyCat

Senior Member
Hi James,

Yes, that is the type of code where Multi-tasking can be helpful, but I'm not convinced it is essential.

But before I attempt to code it without Multi-tasking (or admit defeat), we need a "full" specification: If you re-trigger LED1 after (say) 2 seconds, then do you want LED2 to switch OFF after another 3 seconds (i.e. a total of 5) or after (another) 5 seconds? And might the situation occur that LED2 switches OFF whilst LED1 remains ON (for the remainder of its set timing period) ?

Cheers, Alan.
 

jbsmith1

New Member
Hi Alan,
Yes Full specification would be:
If you re-trigger LED1 after (say) 2 seconds, then do you want LED2 to switch OFF after another 3 seconds (i.e. a total of 5) Yes.
And might the situation occur that LED2 switches OFF whilst LED1 remains ON (for the remainder of its set timing period) ? Yes.

Most appreciate your help Alan.

James
 

AllyCat

Senior Member
Hi,

That's an example where it's "sensible" to use multi-tasking because the two operations (on the LEDs) are completely independent, so you can almost write two separate "Programs", which just happen to read the same input signal. I believe this example works, but don't find the Simulator very convenient when multi-tasking.
Code:
START1:     
IF TRIGGER = 1 THEN START1    ; Wait (loop) until Trigger = 0 
ReadADC POT1, Time1
HIGH LED1               
FOR TEMP = 0 TO Time1     
    PAUSE 18                 
NEXT TEMP                          ; Total loop time ~ 20ms
LOW LED1
GOTO START1

START2:     
IF TRIGGER = 1 THEN START2    ; Wait (loop) until Trigger = 0 
HIGH LED2
PAUSE 5000               
LOW LED2
GOTO START2
Here's an example using a single loop, although it could probably be neater; I've taken this opportunity to restructure the program to avoid GOTOs (disliked by some programmers) :
Code:
DO
    IF TRIGGER = 0 THEN                    ; Button is pressed
        IF Time1 = 0 THEN
              ReadADC POT1, Time1        ; Reload time delay (0 - 255)
              HIGH LED1
        ENDIF
        IF Time2 = 0 THEN
              Time2 = 250                        ; Reload time delay (~5 seconds)
              HIGH LED2
        ENDIF
     ELSE
          PAUSE 4                       ; Balance the time delays
     ENDIF

     IF Time1 = 0 THEN           ; Time period has ended
           LOW LED1
      ELSE
           DEC Time1                  ; Reduce remaining Time1 by about 20 ms
      ENDIF
     IF Time2 = 0 THEN
           LOW LED2
      ELSE
           DEC Time2
      ENDIF
      PAUSE  10                  ; Approximate delay for total 20ms loop period
LOOP
Cheers, Alan.
 

jbsmith1

New Member
Hi Alan,
I must thank you for your time in solving this and helping me understand more of this code.
Yes both your examples worked in different ways and in the end I have used the parallel task sketch and changed the pause 18 to pause 20 and got almost back to how my single task adc timing sketch was working before trying to add the 5 second output. If I play with some resistor values on each end of the voltage divider I will be there.:)

So many thanks again.

Best regards
James
 
Top