Welcome to the Parallax Discussion Forums, sign-up to participate.

# how do I loop for x seconds?

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

Thanks!

• Posts: 6,178
edited February 12
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
```
• Posts: 837
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.
Re-inventing the wheel is not a waste of time if, when you are done, you understand why it is round.
Cool, CA, USA 95614
• Posts: 21,658
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
Perfection is achieved not when there is nothing more to add, but when there is nothing left to take away. -Antoine de Saint-Exupery
• Posts: 29
• Posts: 21,658
Someone proficient in C will have to answer. I'm not.

-Phil
Perfection is achieved not when there is nothing more to add, but when there is nothing left to take away. -Antoine de Saint-Exupery
• Posts: 332
edited February 11
.
Any com port in a storm.
Floating point numbers will be our downfall; count on it.
Imagine a world without hypothetical situations.
• Posts: 21,658
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
Perfection is achieved not when there is nothing more to add, but when there is nothing left to take away. -Antoine de Saint-Exupery
• Posts: 332
edited February 11
.
Any com port in a storm.
Floating point numbers will be our downfall; count on it.
Imagine a world without hypothetical situations.
• Posts: 2,167
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
}
```
• Posts: 104
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