Time and Time again

ZOR

Senior Member
Can you have more than 1 instance of TIME working at the same time?

ie TIME1, TIME2. So I can have TIME1 running and while running reset TIME2 to zero, TIME2 being used to time another procedure.

Hope this is understood. Thanks
 

hippy

Ex-Staff (retired)
Staff member
There is only one 'time' variable.

There is no way I can see to have 'time1' and 'time2' variables separately and automatically keeping track of their own elapsed time but it can perhaps be done programmatically.

This code will toggle B.1 every second, toggle B.2 every two seconds -

Code:
#Picaxe 20M2

Symbol lastTime = w0
Symbol time1    = w1
Symbol time2    = w2

MainLoop:
  Do
    Gosub UpdateTimers
    If time1 >= 1 Then
      time1 = 0
      Toggle B.1
    End If
    If time2 >= 2 Then
      time2 = 0
      Toggle B.2
    End If
  Loop

UpdateTimers:
    Do While time <> lastTime
      inc lastTime
      inc time1
      inc time2
    Loop
    Return
You could split that out for a multi-tasking program if you desired that format -

Code:
#Picaxe 20M2

Symbol lastTime = w10
Symbol time1    = w11
Symbol time2    = w12

Start0:
  Do
    Gosub UpdateTimers
  Loop

Start1:
  Do
    If time1 >= 1 Then
      time1 = 0
      Toggle B.1
    End If
  Loop

Start2:
  Do
    If time2 >= 2 Then
      time2 = 0
      Toggle B.2
    End If
  Loop

UpdateTimers:
    Do while time <> lastTime
      inc lastTime
      inc time1
      inc time2
    Loop
    Return
 

lbenson

Senior Member
You can look at it the other way around if you're counting seconds.
Code:
Time1Event = 10
Time2Event = 15
Time = 0 
do
  if time >= Time1Event then
    do something
    Time1Event = time + 10 ; or $FFFF to turn off
  endif
  if time >= Time2Event then ; do something else
    ...
  endif
  if time >= 43200 then  ' half a day, 12 hours, 60*60*12
    if time1Event <> $ffff then
      time1Event = time1Event - time ' remaining time to event--may wish to confirm that time1Event not < time
    endif
    if time2Event <> $ffff then
      time2Event = time2Event - time ' remaining time to event2
    endif
    time = 0 ' reset for another 12 hours
  endif
loop
And so on for as many events as you would want.
 
Last edited:

ZOR

Senior Member
Many thanks everyone, that answers the problem properly with useable code. Thanks again for the work put in.
 

Jeremy Harris

Senior Member
Bear in mind that the internal time variable stops whenever there is a blocking instruction. This caught me out recently, when I was using a serin instruction and discovered that time just stops incrementing whilst serial data is being received. I believe that any blocking instruction has the same effect.
 

ZOR

Senior Member
Thanks Jeremy, I did not know that. Fortunately I have moved this part of the code to the serout side prior to sending serial data. I will remember that in future, thanks again
 
Top