Resetting Micros() to 0

I am using Arduino Zero board and I want to hold the Analog output for 100us.

unsigned long previousMicros = 0;
const long interval = 100;
unsigned long currentMicros = micros();

if (currentMicros - previousMicros >= interval){
previousMicros = currentMicros;

However I need to reset micros() to 0 for this function to work, any idea on how to do it?
I tried using:
noInterrupts ();
timer0_millis = 0;
interrupts ();
but it couldn't compile and showed: "undefined reference to `timer0_millis'"

How often do you reset the wall clock to midnight ?

millis / micros work the same way… you don’t reset them.

‘time’ is relative. It doesn’t stop.
Just keep track, subtract and compare whatever time values you’re using.
The clock on the wall keeps ticking (with millis, it only rolls over every 49 days, and there’s ways around that)

Is there a way i can hold my analog signal for 100us while waiting for pulses to be triggered? I can't use delay because it delays the whole program.

These tutorials should help you to understand how to use millis() and micros() for timing:
Several things at a time.
Beginner's guide to millis().
Blink without delay().
There is no setting micros() or millis() to zero.

1 Like

What’s wrong with the method you used in the 1st post?

undefined reference to 'timer0_millis". There was an error compiling. i did declare extern volatile unsigned long timer0_millis, timer0_overflow_count;

I mean, why do you need to reset it instead of checking the difference of previous and current time and waiting until it is >= 100us?


Make previousMicros a static variable so it doesn't get reinitialized to 0 every time, then your code will work.

static unsigned long previousMicros = 0;

There is no need to reset micros().

The trick is to have a function that turns on your analog output, then have another to turn it off. Between the two? You can go out have have a dink or whatever.

-jim lee

1 Like

first of all
You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

to show you with easy to calculate numbers what was explained in general words above

let's assume micros has counted up to 1.234.000
micros() keeps counting up

at this point in time you store a snapshot of micros()
SnapShotOfTimeAtStart = micros();

SnapShotOfTimeAtStart contains 1.235.000

your loop continues to loop while micros() is counting up continiously

CurrentTime 1.235.001
CurrentTime 1.235.002
CurrentTime 1.235.003
while it is counting up you continously calculate a Time-DIFFERENCE
CurrentTime - SnapShotOfTimeAtStart

CurrentTime  -  SnapShotOfTimeAtStart 
 1.235.001   -  1.235.000              = 1 microsecond
CurrentTime  -  SnapShotOfTimeAtStart 
 1.235.002   -  1.235.000              = 2 microseconds
CurrentTime  -  SnapShotOfTimeAtStart 
 1.235.003   -  1.235.000              = 3 microseconds
CurrentTime  -  SnapShotOfTimeAtStart 
 1.235.099   -  1.235.000              = 99 microseconds
CurrentTime  -  SnapShotOfTimeAtStart 
 1.235.100   -  1.235.000              = 100 microseconds

and "TATA!" there are your 100 microseconds

You can change the numbers to any value you want
with calculating a difference the absolute values are meaningless

trying to reset micros() does so much disturbing a lot of internal processes that it really is NOT worth the hassle

best regards Stefan

I don’t disagree. You can reset, but there is no need.

But I am curious about this lot of internal processes that would be disturbed. I am blissfully unawares of any. I suppose some libraries might malfunction… more often for me, the library disturbs millis()!

I sorta depend on knowing what is happening on such small machines as these. On my desktop, there are 100s of things going on. Not so many with the Arduini. :wink:


Well, my timeObj library would fail if someone messed with the micro & mili stuff. Suddenly all the values its looking at and comparing to change.

-jim lee

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.