Pages: 1 [2]   Go Down
Author Topic: Resetting millis() using software  (Read 1682 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12285
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    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; }
k++;
    }

...serves no useful purpose.

Code:
    timer0_millis = 2000 + reading1; // e.g. 2000: time elapsed while sleep

...is 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...

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

None of the code presented adjusts micros.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 167
Posts: 12417
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 - http://www.arduino.cc/playground/Code/StopWatchClass -

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: 1 [2]   Go Up
Jump to: