Pages: 1 [2]   Go Down
Author Topic: Resetting millis() using software  (Read 2318 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Online Online
Shannon Member
Karma: 212
Posts: 13085
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

    unsigned long reading1;
    unsigned long reading2;
    boolean readingOK = false;
    int k=0;
    while (k<1000 && !readingOK) {
reading1 = timer0_millis;
reading2 = timer0_millis;
if (reading1 == reading2) { readingOK = true; }

...serves no useful purpose.

    timer0_millis = 2000 + reading1; // e.g. 2000: time elapsed while sleep wrong.  Interrupts have to be disabled when reading or writing shared multi-byte data.  The previous code snippet protects the read but you failed to protect the write.

The (mostly) correct way to bump millis...

  uint8_t SaveSREG;
  SaveSREG = SREG;
  timer0_millis = 2000 + timer0_millis; // e.g. 2000: time elapsed while sleep
  SREG = SaveSREG;

None of the code presented adjusts micros.

Global Moderator
Offline Offline
Shannon Member
Karma: 227
Posts: 14048
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

instead of millis() you can use a millis() based stopwatch class to have multiple independant timers ...
see - -


Rob Tillaart

Nederlandse sectie -,77.0.html -
(Please do not PM for private consultancy)

Pages: 1 [2]   Go Up
Jump to: