Pages: 1 [2]   Go Down
Author Topic: Is it possible to reset the millis() function  (Read 2962 times)
0 Members and 1 Guest are viewing this topic.
Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I turn my egg-timer over.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 200
Posts: 12779
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The answer is "Yes, it can very easily be done", with the rider "...but why would you want to?".

Two riders.  Resetting millis can cause serious problems that are difficult to debug and impossible to fix (except by letting millis run free).
Logged

Offline Offline
Sr. Member
****
Karma: 1
Posts: 462
I am a amateur.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Resetting millis()? That is very cool, but you don't need it, as other people said. It may also cause bad effects to some functions. Most people offset millis() when they want to execute some statements at intervals or time how long an action takes, etc. That is the same as boiling an egg for 10 minutes; if you start at 11:00 a.m., you would check back at 11:10 a.m., right?
Quote from: AWOL
You don't reset your watch to midnight every time you want to boil an egg.
I have devoloped a crude and somewhat flexible library class for that purpose: http://arduino.cc/forum/index.php/topic,107716.msg813967.html#msg813967. There is also http://arduino.cc/forum/index.php/topic,93959.0.html and many other examples.
Quote from: JimEli
Maybe add:
      TCNT0 = 0;
Add it where?

elapsed = finished - start
Logged


USA
Offline Offline
Jr. Member
**
Karma: 4
Posts: 89
If you can't fix it with a hammer, it must be an electrical problem.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote from: JimEli
Maybe add:
      TCNT0 = 0;
Add it where?

elapsed = finished - start

Code:
void millis_reset(void) {
  cli(); // Disable interrupts
  timer0_millis = timer0_fract = 0;
  TCNT0 = 0;
  sei(); // Re-enable interrupts
}
Logged


Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 200
Posts: 12779
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


@JimEli (et al): You failed to reset the internal state of the clock divisor.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I appear to have stirred a bit of a hornets nest, to which I apologise.

The reason that I wanted to be able to reset millis() is that I the circuit is running long term, it needs to have an awareness of time but not of "when" it is, just that time has happened. A bit like a stop watch, it is able to tell you the duration of an event, but not the time it started and finished.

Quote
You don't reset your watch to midnight every time you want to boil an egg.
No, but you do reset an egg timer every time you want to boil an egg. There is a difference between telling the time and recording what time has passed. A watch is good for telling you when you are in relation to our passage round the Sun (or the Earth's rotation for a 24 hour clock), and ensuring everyone is synced up. A timer is more about how much time has passed since it was arbitrarily started/reset, the actual time it was started and stopped is irrelevant. To continue that analogy; you don't wait for a stopwatch to roll over to zero before starting the next 100 meter race.

That is the sort of functionality I feel the millis() should be providing. If I want a clock I'll connect up to an IC that gives me time and date (and to a greater accuracy then millis() can provide), all millis() needs to do is provide a reasonable sense of time, not the time. When you only need to worry about small durations of time (minutes at best) having to program in extra to deal with roll-overs is just a PITA, when all I really want is to reset my stopwatch.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25852
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
, all millis() needs to do is provide a reasonable sense of time, not the time
But that is exactly what it does do.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 200
Posts: 12779
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

When you only need to worry about small durations of time (minutes at best) having to program in extra to deal with roll-overs is just a PITA, when all I really want is to reset my stopwatch.

First entry...
http://lmgtfy.com/?q=arduino+stopwatch+library
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6137
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

When you only need to worry about small durations of time (minutes at best) having to program in extra to deal with roll-overs is just a PITA, when all I really want is to reset my stopwatch.
You're making handling rollover out to be a big deal, when it isn't.

This code handles the millis() roll over:
Code:
if ((long)(millis() - waitMillis) >= 0) {
    // do whatever...
    waitMillis += 1000;  // Increment the timer.
}

That is all you need to do.  This is the method I use in all my projects, most recently a Binary Clock, which has been running for a couple of months with no problem.

For detailed explanation on why this works, read this article:
http://arduino.cc/playground/Code/TimingRollover
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

USA
Offline Offline
Jr. Member
**
Karma: 4
Posts: 89
If you can't fix it with a hammer, it must be an electrical problem.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


@JimEli (et al): You failed to reset the internal state of the clock divisor.


maybe add some more...

Code:
void millis_reset(void) {
  cli(); // Disable interrupts
  timer0_millis = timer0_fract = 0;
  CLKPR = B10000000;
  CLKPR = 0;
  TCNT0 = 0;
  sei(); // Re-enable interrupts
}
Logged


Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 200
Posts: 12779
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

maybe add some more...

Congratulations.  There's just one thing left.

And, before you waste any more time trying to figure out what's missing, bear in mind that resetting millis wreaks havoc with some libraries; something you will not be able to overcome.  Resetting millis is unnecessary and can cause serious difficult to debug problems.
 
Logged

Pages: 1 [2]   Go Up
Jump to: