How to do a particular operation for 1 ms using arduino. How to set the timer

How to do a particular operation for 1 ms using arduino. How to set the timer values.

How to do a particular operation for 1 ms using arduino.

unsigned long then = micros();
while(millis() - then > 1000)
{
   // Do the one ms thing
}

How to set the timer values.

Which timer? What values? What for?

PaulS:

How to do a particular operation for 1 ms using arduino.

unsigned long then = micros();

while(millis() - then > 1000)
{
   // Do the one ms thing
}






> How to set the timer values.


Which timer? What values? What for?

That’d be 1,000ms - 1 second :stuck_out_tongue:

To do something for 1ms you need to do the same thing but with micros().

You’d think at first you could do:

unsigned long now = millis();
while (millis() == now) {
  // do something
}

but that would only do the “something” for the remainder of the current millisecond. If you start it half way through a millisecond you’ll only get half a millisecond’s worth of something.

So use a loop that executes until the number of microseconds exceeds 1000 more than the start time.

unsigned long now = micros();
while (micros() - now <= 1000) {
    // do something
}
startOperation();
delay(1);
stopOperation();

Psst, Paul- You mixed your millis() and micros() in your example...

PaulS:

How to do a particular operation for 1 ms using arduino.

unsigned long then = micros();

while(millis() - then > 1000)
{
  // Do the one ms thing
}

What PaulS undoubtedly means is:

unsigned long then = micros();
while(micros() - then <= 1000)
{
   // Do the one ms thing
}

… as majenko pointed out.