How to use time variable?

abenn

Senior Member
I want to create a simple timer circuit using a Picaxe 08, and I would normally do so by creating a loop, with a pause in it, which increments a variable by 1 on each loop. The size of the pause would be varied by trial-and-error to get something approximating 1 second for each loop.

I see now that there's an enabletime and disabletime command available for the 08M2 chip, but I can't figure how to use it. I've created a loop, as per the example in the manual, but instead of debug I've put a line "if time > 60 then light_on", where light-on: is "high 0" followed by "goto countup" (the start of the main loop). It doesn't do anything when I used the simulator.

So, how do I use the time variable to activate a subroutine, and what's the advantage (if any) of using time rather than just looping and incrementing by 1?
 

eclectic

Moderator
It works on a real chip.

For the simulator, try this, noting the change from
debug to Sertxd

Code:
main: pause 5000

disabletime ; disable time
pause 5000 ; wait 5 seconds

enabletime ; enable time

sertxd (#time) ; *******************

goto main ; loop back to start
e
 

eclectic

Moderator
Added
Also works in simulation:
Code:
main: pause 5000
disabletime ; disable time

pause 5000 ; wait 5 seconds
enabletime ; enable time

if time > 12 then
 high 2
endif

sertxd (#time) ; *******************

goto main ; loop back to start
 

abenn

Senior Member
Thanks for those bits of code, eclectic. I've run both of them on the simulator, but can't see any output. The Serial Output Buffer monitor window shows 'sertxd output - baud 4800,n,8,1' for each cycle of the loop, but output 2 doesn't seem to go high -- it doesn't switch to green on the Simulation monitor. In fact, if I change your line to "if time < 1", it does switch to green on the first cycle, seemingly indicating that the variable time is not incrementing during each loop.

But, that issue aside, what's the advantage of using time? Is it more accurate than simply using the pause and then incrementing a variable during each loop?
 

eclectic

Moderator
Hmm?

Please see the screen capture of my simulation
a couple of minutes ago.

And the Enable time?

Well AFAIK, the advantage is that it runs in the
background, from startup.
You just call the value.

e
 

Attachments

BESQUEUT

Senior Member
what's the advantage of using time? Is it more accurate than simply using the pause and then incrementing a variable during each loop?
The advantage is that the picaxe may do otherthings in the main loop.
It is also more accurate, specialy if there are conditions in the main loop...
For example, i have a timer for a vegetal wall : watering 4 mn every 8 h
At the same time, if the water is too cold, the PICAXE can activate a heater.
 

hippy

Technical Support
Staff member
Thanks for those bits of code, eclectic. I've run both of them on the simulator, but can't see any output. The Serial Output Buffer monitor window shows 'sertxd output - baud 4800,n,8,1' for each cycle of the loop, but output 2 doesn't seem to go high -- it doesn't switch to green on the Simulation monitor.
It works for me so perhaps check you have the latest Programming Editor installed.

But, that issue aside, what's the advantage of using time? Is it more accurate than simply using the pause and then incrementing a variable during each loop?
Its main advantage is that it keeps incrementing while you are doing something else ( unless the command blocks ). Therefore you can tell how much time has elapsed without having to constrain yourself to a pause and increment loop.
 

abenn

Senior Member
Thanks for your patience, eclectic. Just come back from a break, and re-copied and re-pasted your code, and it now runs fine on my simulator, just like your screenshot. I must have hit a wrong key somewhere previously -- ahh, I see what I did, I previously disabled the first pause 5000 because I thought it was redundant. Without that it doesn't work, but why? There's acually a 10-second delay in the loop because of the two instances of pause 5000.
 

hippy

Technical Support
Staff member
ahh, I see what I did, I previously disabled the first pause 5000 because I thought it was redundant. Without that it doesn't work, but why? There's acually a 10-second delay in the loop because of the two instances of pause 5000.
There's only a 5 second delay with the first PAUSE taken out, and during the 5 second PAUSE left the timer is disabled. That leaves an incredibly small time between disabling the time and re-enabling it for 'time' to see the progression of time.

It's like a stop watch; press start/continue then immediately press pause, wait five seconds, then repeat. It will take 'forever' for the stop watch to show one minute has elapsed.
 

abenn

Senior Member
OK, I think I'm getting the hang of it: I've put the enabletime command right at the beginning, outside the loop, and then omitted both the pause commands, and it's now looping on the simulator and outputting time at correct clock speed, and setting 2 and 1 high at 12 and 24 seconds respectively. So I don't understand the need to disable time during every loop, nor to create a pause.

Code:
enabletime

main: 

; pause 5000
; disabletime ; disable time

; pause 5000 ; wait 5 seconds
; enabletime ; enable time

if time > 12 then
 high 2
endif
if time > 24 then
 high 1
endif

sertxd (#time) ; *******************

goto main ; loop back to start
 

eclectic

Moderator
The pauses simulate
doing other tasks with the Picaxe.

A little like being at work, and looking
at the clock every few minutes.

This little program works in simulation or for real.
Code:
#picaxe 08M2
setfreq M4
#terminal 4800

main:
pause 3000 ; work for 3 s

;Look at the clock
Sertxd (#time, " ")

Pause 4000

;Look at the clock
Sertxd (#time, " ")

if time>20 then
Sertxd ("Hometime " )
endif

goto main
e
 

hippy

Technical Support
Staff member
So I don't understand the need to disable time during every loop, nor to create a pause.
You don't need either but you did say you did not understand how to use DISABLETIME and ENABLETIME and the example code showed how those worked :)
 

abenn

Senior Member
Thanks, Hippy, the sample code's done it's job -- I understand ebabletime and disabletime now :)

Thanks too, eclectic, I understand the reason for your pauses now -- they're not affecting the timer, but are simply affecting how often the program checks the time.
 
Top