Go Down

Topic: Resetting millis() using software (Read 2495 times) previous topic - next topic

Coding Badly

Code: [Select]
    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: [Select]
    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: [Select]
  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.

robtillaart


instead of millis() you can use a millis() based stopwatch class to have multiple independant timers ...
see - http://www.arduino.cc/playground/Code/StopWatchClass -

Rob Tillaart

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

Go Up