What is the easiest way to run some code each millisecond for a certain amount of time?


I would like to run a some code each millisecond for a certain amount of time, say 120000 milliseconds (2 minutes). What is the easiest ways to achieve that?


Does the code take less than 1 ms to run?

Do you want to run this piece of code 120,000 times in 2 seconds minutes?

I think he wants that in 2 minutes.

Of course. Apologize for the mistyping.

On AVRs, I've seen people enable one of the "Compare Match" interrupts on Timer0. This time is already running at about a 1ms rate (1/1024 s) till wrap-around, so any compare value will also interrupt every 1/1024 s. If you're not using PWM pins, you can set the compare register to an arbitrary value. If you ARE using PWM, whatever value is loaded by PWM will still cause an interrupt every 1/1024 s...

For example: Adafruit_GPS/shield_sdlog.ino at master · adafruit/Adafruit_GPS · GitHub

Actually, I want it to run as many times as possible in 2 minutes.

do this:

uint32_t currTime = millis();
constexpr uint32_t timeDiff = 120000;
while((millis() - currTime) < timeDIff){
//do stuff

However I do not know why you want to do a operation for "as much as possible" over a specific period of time. that could be millions of executions and the core may run hot. Unless you are trying to benchmark different mcus (by comparing the amount of instructions they can do in 2 min).

Many tasks take less than 1ms to run. For example, the 32U4 have a "two clock multiplier" that can do multiplications in two clock cycles. digitalWrite probably take a dozen nanoseconds.

If you want a framerate control (e.g. displaying some graphics on a screen), do this:

unsigned long lastFrameStart;
unsigned long nextFrameStart;
bool justRendered;
uint8_t lastFrameDurationMs;
uint16_t frameCount;
uint16_t frameDiff;

bool setFrameDiff
(uint16_t rate)
  frameDiff = rate;
  return 0;
bool nextFrame
  unsigned long now = millis();
  bool tooSoonForNextFrame = now < nextFrameStart;

  if (justRendered) {
    lastFrameDurationMs = now - lastFrameStart;
    justRendered = false;
    return false;
  else if (tooSoonForNextFrame) {
    if ((uint8_t)(nextFrameStart - now) > 1)
      sleep(1);//delay(1) when the processor does not support "sleep" function
    return false;

  // pre-render
  justRendered = true;
  lastFrameStart = now;
  nextFrameStart = now + frameDiff;
  //if (frameCount > 20000)
    //frameCount = frameCount - 20000;
  return true;

which also tell you the amout of frames that have passed.

Post your sketch well formated and in code "</>" tags as been shown in this editor to see how we can help.
Your sketch needs two timer to do this job at least.

Which is it ?

Look at the ticker.h library.

What do you want to do, "as many times as possible"? What are you trying to achieve? All things take some time, so if you do it repetitively, you are already doing it as many times as possible in a given time frame.

This is PC thinking. Most Arduino code runs the processor at full speed and constantly. It doesn't matter whether the CPU is calculating the figures of PI, or sitting in a loop twiddling its thumbs. The power dissipation doesn't change.

Power management is available but requires programming tricks.

1 Like

EXACTLY. Or, maybe just calls to things like sleep(32);

And it is the case, where, because I did not source a "sleep" function for Arduino Due, the core runs warm, even though the amount of things to do is minimal.

Let's leave the conversation for matters that matter.

While I try not to veer off topic, I will correct statements that are misleading, as some misconceptions multiply and perpetuate. It is true, the Due runs warm. But it always runs warm when running a typical sketch.

1 Like

Yeah, I fried an egg on top of mine just this morning!


1 Like

Post the code, using code tags.

Answer the question in reply #9.