John,but then when time rolls over to 0, time would then be less than w0 and my code would not be true
symbol ElapsedTime=w0
symbol LastTime=w1
' do it the first time
LastTime = Time ' Record the time we did it
...
' some time later on
ElapsedTime = Time - LastTime
if ElapsedTime >= 10 then
LastTime = Time ' update the time we did it
' Do something
I do not think that it is a good idea if accuracy is concerned : when the time variable is reset to zero, remaining fractions of second are also reseted (and you cannot know how many are lost). So it is not possible to keep the time variable accuracy.Your program can set it to zero, later check to see when it becomes > N ..... then set it to zero again. No overflow to worry about.
Clever...You can get this code to work if you change to checking the elapsed time since you last did it instead of calculating ahead of time the next time you need to do it.
I have been pleasantly surprised with the accuracy of the internal timer in an 08M2 in a recent project. With the PICAXE running at 32MHz ('Time' ticks twice per second), I was resetting the Time variable every 10 ticks or 5 seconds.....Given the processor overhead of the loop, it still will not be very accurate....
That's about the same accuracy that I have seen. A .3 percent error means a 30ms error in 10 seconds. Not clock accuracy, but should be good enough for many simple applications requiring a 10 second delay....The timer was within +/- 4 minutes per day or less than +/- 0.3% (a day is 1440 minutes long). I used an LDR to determine sunset each day, recording then resetting the minute count (in internal EEPROM) at sunset.
#picaxe 14M2 ' or other M2 picaxe
symbol wEventTime1 = s_w1 ' 0 when off, otherwise number > 'time'
symbol wEventTime2 = s_w2 ' 0 when off
symbol bAMPM = bit0
main:
do
if time > 43199 then
if wEventTime1 > 43199 then
wEventTime1 = wEventTime1 - 43199 min 1 ' don't allow 0--OFF indicator
endif
if wEventTime2 > 43199 then
wEventTime2 = wEventTime2 - 43199 min 1 ' don't allow 0--OFF indicator
endif
endif
time = 0
if bAMPM = 0 then : bAMPM = 1 : else bAMPM = 0 : endif ' toggle bit
' your code testing to see if timer has expired
if wEventTime1 > 0 and timer > wEventTime1 then
' do something
wEventTime1 = 0 ' reset event timer
endif
if wEventTime2 > 0 and timer > wEventTime2 then
' do something
wEventTime2 = 0 ' reset event timer
endif
' your code determining that something should be done later
if [someevent] then
wEventTime1 = 60 * 2 + time ' do something in 2 minutes
endif
if [anotherevent] then
wEventTime2 = 60 * 60 + time ' do something in an hour
endif
' the rest of your code
loop
No, that is not correct for the time variable in M2 devices. Resetting "time" does NOT reset the (20ms interrupt-driven) pre-scaler.when the time variable is reset to zero, remaining fractions of second are also reseted (and you cannot know how many are lost).
Fantastic - I'd thought of this trick a while ago but never got round to trying it. This was my graph of Lux which made me think it was a sharp enough cut-off to be a way of resetting a timer.I have been pleasantly surprised with the accuracy of the internal timer in an 08M2 in a recent project. With the PICAXE running at 32MHz ('Time' ticks twice per second), I was resetting the Time variable every 10 ticks or 5 seconds.
The timer was within +/- 4 minutes per day or less than +/- 0.3% (a day is 1440 minutes long). I used an LDR to determine sunset each day, recording then resetting the minute count (in internal EEPROM) at sunset.
YES it is :No, that is not correct for the time variable in M2 devices. Resetting "time" does NOT reset the (20ms interrupt-driven) pre-scaler..
Flenser #5 seems to me more simple.My preference in using the "time" variable is to wrap it every half day, after 43200 seconds (60 seconds times 60 minutes times 12 hours), and use a bit variable as an am/pm indicator
The "prescaler" is a pure software counter (not accessible to the user) which counts 50 x (20ms) interrupts and then increments the "time" variable. So if it is currently 49 (980+ ms) then "time" will (still) increment on the next interrupt, whatever you do with the (1 second) time value. From the description of the time variable:If real time is 123.980 s reseting Time will lost the 980 ms elapsed