Shop OBEX P1 Docs P2 Docs Learn Events
How to put current clock ticks in a variable and add 15 seconds to it [SOLVED] — Parallax Forums

How to put current clock ticks in a variable and add 15 seconds to it [SOLVED]

4Alex4Alex Posts: 119
edited 2009-09-08 15:44 in Propeller 1
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:

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

  • TimmooreTimmoore Posts: 1,031
    edited 2009-09-08 14:50
    nexttime := cnt + 15*clkfreq
    
    ...
    
    if (cnt - nexttime) > 0
       do sometime
    
    



    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
  • Ken PetersonKen Peterson Posts: 806
    edited 2009-09-08 14:53
    4Alex: I think there are examples in the Propeller Manual, but here's some simple code:

    
    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 loop
    
    
    
  • 4Alex4Alex Posts: 119
    edited 2009-09-08 15:31
    @Timmoore:
    Thank 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 := cnt
    



    Many thanks again for your help.

    Cheers,

    Alex

    Post Edited (4Alex) : 9/8/2009 3:44:44 PM GMT
  • Ken PetersonKen Peterson Posts: 806
    edited 2009-09-08 15:40
    4Alex: You may be missing a repeat statement in your code
  • 4Alex4Alex Posts: 119
    edited 2009-09-08 15:44
    @Ken: You don't miss much, do you! Yup, I edited the unnecessary code and clipped the repeat. I've corrected the previous posting to reflect this. Thank you so much.

    Cheers,

    Alex
Sign In or Register to comment.