Run loop for fixed duration

Hi all. I am very new to the arduino environment. Have done a sketch but I want the loop to run once only for a fixed period of time. The loop must only run again after reset. It would be much appreciated if someone could please explain how to do this (with a code sample if possible). Thanks.

Look at the blink without delay example for how to use millis() to time stuff.

Basically in loop()

  1. If trigger is not set, return
  2. starttime = millis()
  3. while ((millis() - starttime) < running time of the loop) do the code in the loop

llxiv: I want the loop to run once only for a fixed period of time. The loop must only run again after reset.

What you're describing is a solution, rather than the underlying problem.

If you only want your sketch to do something once after a reset, then put the logic in setup() rather than loop().

If you want a piece of your code to run repeatedly until some specified time has elapsed, execute it in a loop that terminates when the value of millis() exceeds some value.