How to put current clock ticks in a variable and add 15 seconds to it [SOLVED]
Good morning everybody,
I am trying to establish a cyclic reading of a sensor from an infinite loop. Basically, it's a polling sequence and I would like to read a sensor every 15 seconds or so. Timing doesnt need to be precise and I would like to avoid using a cog or any kind of bloated code. I currently have the clock object loaded, but I can't use PauseSec or pause1s, as I don't want to stop the loop from running.
I think the solution could be in reading the current propeller clock number of ticks into a variable and add 15 seconds worth of ticks. I have no idea how to do this nor how many ticks covers a 1 second period. Could someone help me, please.
Here's what I try to achieve, in pseudocode:
Many thanks for any assistance.
Cheers,
Alex
Post Edited (4Alex) : 9/8/2009 3:34:26 PM GMT
I am trying to establish a cyclic reading of a sensor from an infinite loop. Basically, it's a polling sequence and I would like to read a sensor every 15 seconds or so. Timing doesnt need to be precise and I would like to avoid using a cog or any kind of bloated code. I currently have the clock object loaded, but I can't use PauseSec or pause1s, as I don't want to stop the loop from running.
I think the solution could be in reading the current propeller clock number of ticks into a variable and add 15 seconds worth of ticks. I have no idea how to do this nor how many ticks covers a 1 second period. Could someone help me, please.
Here's what I try to achieve, in pseudocode:
repeat
'check if time to read sensor
if longVarDelayedClock > longVarCurrentClock
'read sensor here
'schedule next reading time in 15 seconds from now
longVarDelayedClock := longVarCurrentClock + (15 seconds * no_of_ticks_per_sec)
'do other stuff here
'and then loop
Many thanks for any assistance.
Cheers,
Alex
Post Edited (4Alex) : 9/8/2009 3:34:26 PM GMT

Comments
make sure you do the compare by sub and compare with 0 otherwise you get problems when cnt wraps. Also only works to ~25 sec, cnt wraps at ~25sec
con READ_INTERVAL = _clkfreq * 15 pub main last_read := cnt ' ' repeat ' check if time to read sensor if cnt - last_read > READ_INTERVAL 'read sensor here 'record time of reading in clock ticks last_read := cnt ' do other stuff here 'and then loopThank you for your assistance. BTW, I use some of your objects: they are well written and very useful. Congrats.
@Ken:
I apologise if the solution was already in the manual. I didn't find it so I guess I was seeing only the one tree instead of the whole forest. I was studying the clock object and coulnd't figure out how to use the various clock commands. I tryied a few things and it turned ugly. I had to implement a variation of your code snippet (because my CON is already defined with _clkmode and _xinfreq) and it works perfectly. For future reference for someone else, here's my code:
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 VAR long last_read long READ_INTERVAL PUB Main READ_INTERVAL := (clkfreq * 15) last_read := cnt . . . repeat if cnt - last_read > READ_INTERVAL 'read sensor here last_read := cntMany thanks again for your help.
Cheers,
Alex
Post Edited (4Alex) : 9/8/2009 3:44:44 PM GMT
Cheers,
Alex