Shop OBEX P1 Docs P2 Docs Learn Events
how do I loop for x seconds? — Parallax Forums

how do I loop for x seconds?

hey guys,
how do I loop a function for a certain number of seconds?

Thanks!

Comments

  • Tracy AllenTracy Allen Posts: 6,656
    edited 2018-02-12 17:50
    We probably need more information in order to answer that. One common way to do that refers to the system clock register.
    ticks := clkfreq * Nseconds + cnt  ' the system clock at N seconds in the future
        ' provided the cnt <2^32,  ~(53/2) seconds at clkfreq=80MHz
    repeat
      ' do whatever
      if cnt - ticks > 0   ' wait that number of seconds with uncertainty for the whatever code
        quit
    
  • 1. Put a waitcnt in the function to force it to take a known amount of time.
    2. repeat the loop 1_000 or 1_000_000 divided by the know loop time.
  • In Spin,

    If x < 27, use this:
    t0 := cnt
    repeat until (cnt - t0 => x * clkfreq)
      'Do stuff for x seconds
    

    Otherwise, this:
    t0 := cnt
    repeat x
      repeat until (cnt - t0 => clkfreq)
        'Do stuff for x seconds
      t0 += clkfreq
    

    In both cases, it's assumed that "do stuff" doesn't take very long to perform.

    -Phil
  • What about in C?
  • Someone proficient in C will have to answer. I'm not.

    -Phil
  • mikeologistmikeologist Posts: 337
    edited 2018-02-11 02:09
    .
  • mikeologist,

    Um, if ints are signed, that won't work. Also, even if ints are unsigned, that code is only good for times less than 54 seconds.

    -Phil
  • mikeologistmikeologist Posts: 337
    edited 2018-02-11 02:08
    .
  • AribaAriba Posts: 2,682
    It's an easy translation from Phils Spin code to C:

    The first one with x < 27 seconds:
    int t0 = CNT;
    while (CNT - t0 < x * CLKFREQ)
    {
      //Do stuff for x seconds
    }
    
  • This function if available on Arduino as micros and millis.

    I needed to keep track of the time and then do something and I needed multiple timers to do it so I wrote a library function for it.
    int micros(unsigned long *D)
    {
      long t;
      
      t = CNT + 280 - *D;
      *D = CNT;
      if (t < 0)
        t = UINT32_MAX + t;
    
      *D = *D - t % us;
      t = t/us;
      return t;
    }
    
    int millis(unsigned long *D)
    {
      long t;
      
      t = CNT + 280 - *D;
      *D = CNT;
      if (t < 0)
        t = UINT32_MAX + t;
    
      *D = *D - t % ms;
      t = t/ms;
      return t;
    }
    

    Buy passing in a long variable you can keep track of multiple time instances.
    unsigned long PCNT;
    unsigned long P2CNT;
    
    
    int main()
    {
      int sec, sec2;
      
      int d1, d2;
      micros(&PCNT);
      millis(&P2CNT);
      
      sec = 0;
      sec2 = 0;
      d1 = 0;
      d2 = 0;
      
      while(1)
      {
        d1 += micros(&PCNT);
        d2 += millis(&P2CNT);
        
        if (d1 > 1000000)  // wait a second
        {
          d1 = d1 - 1000000;
          print("Timer: %d, %d\n", d1, sec++);
        }      
        
        if (d2 > 1000) // wait a second
        {
          d2 = d2 - 1000;
          print("Timer2: %d, %d\n", d2, sec2++);
        }      
      }  
    }
    

    You can also use the mstimer.h library that starts a cog with the time in it.

    Mike

Sign In or Register to comment.