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)
undefined reference to 'timer0_millis". There was an error compiling. i did declare extern volatile unsigned long timer0_millis, timer0_overflow_count;
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.
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
1.234.001
1.234.002
1.234.003
...
1.235.000
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
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.