Pages: [1] 2 3   Go Down
Author Topic: Reset millis  (Read 1894 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 128
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am using millis() to perform some time-based calculations, and want to be able to reset millis automatically every 24hrs back to 0.
I am using a long integrer to count millis, and was thinging along the lines of;

If millis() >= 86400000
set timer0_overflow_count to 0

Would this sort of approach work, or is there a better approach?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Get a servo. Mount it over the Arduino. Once a day, rotate the servo arm so it presses the reset button.

Or, re-think why you think you need to reset millis(). You don't. Really.
Logged

London
Offline Offline
Full Member
***
Karma: 0
Posts: 168
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

lol - good idea with the servo.

I was going to suggest resetting the board every 40 days or whatever, to ensure the millis() never overflow, but was going to be boring and do it with wires and pins stuff smiley
Logged

Current Projects: Rodentometer - Hamster Endurance Moni

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for getting us know about it..
Logged

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

Quote
Thanks for getting us know about it..
My spidey spam senses are tingling.
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.

New Jersey
Offline Offline
Faraday Member
**
Karma: 48
Posts: 3407
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was going to suggest resetting the board every 40 days or whatever, to ensure the millis() never overflow, but was going to be boring and do it with wires and pins stuff smiley

Overflow need not be a problem - check johnwasser's analysis in this thread: http://arduino.cc/forum/index.php/topic,60215.0.html
Logged

London
Offline Offline
Full Member
***
Karma: 0
Posts: 168
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry yes, forgot to mention that it's really not a big problem - easy to fix in code.  Maybe just use the servo-button-reset-bot as a backup smiley
Logged

Current Projects: Rodentometer - Hamster Endurance Moni

Offline Offline
Full Member
***
Karma: 0
Posts: 128
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


I would prefer a software solution, so would my original idea work?
I am not worried about the overflow, but resetting millis gives me a clean sheet every day, and resets the counters linked to millis.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but resetting millis gives me a clean sheet every day, and resets the counters linked to millis.
Do you reset your watch every day? Or, does it just keep on ticking?

Since millis() is used for relative timing (did this happen before that, or after? How long has it been since...?), expecting absolute values is not advisable.
Logged

London
Offline Offline
Full Member
***
Karma: 0
Posts: 168
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would prefer a software solution, so would my original idea work?
I am not worried about the overflow, but resetting millis gives me a clean sheet every day, and resets the counters linked to millis.
To the first, I think not.  It may appear to, but from what I've read, interferring with certain elements such as timing will affect all sorts of other libraries that use it.  So you can expect issues with anything that uses those elements.

For the second, the problem is that you'll start by asking for zero to mean "midnight", but then tomorrow you'll be trying to work out how to deal with an Arduino resetting during the day.  Will you then set the clock to 355322 to change it?

If you need real time, get an RTC.  If you need durations, calculate them.

"duration = end - start" is easier than fiddling with underlying low-level calls.
Logged

Current Projects: Rodentometer - Hamster Endurance Moni

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


If the functionality of millis() is not good enough you could add a wrapper around it with new functionality, lets call it MyMillis or MM.

To implement it you need one unsigned long and two simple functions.

Code:
unsigned long MMdelta = 0L;      // aka MMoffset, holds the offset between MyMIllis() and the system millis().

unsigned long MyMillis()             // returns the nr of millis since MMReset(); if MMReset is not called its value equals millis()
{
  return millis()- MMdelta;
}

void MMReset(unsigned long val = 0L)     // resets MyMillis, default to 0, optionally to any positive value.
{
  MMdelta = millis() - val; 
}

////////////////////////////////////////////
void setup()
{
  Serial.begin(115200);
  Serial.println("MyMillis demo");
 
  for(int i=0; i< 10; i++)
  {
    delay(10);
    Serial.println(MyMillis());
  }
  delay(1000);
  MMReset();
  for(int i=0; i< 10; i++)
  {
    delay(10);
    Serial.println(MyMillis());
  }
  MMReset(1000);
  for(int i=0; i< 10; i++)
  {
    delay(10);
    Serial.println(MyMillis());
  }
}

void loop(){}

Of course you can make a class of it if you need more mili-counters.
Logged

Rob Tillaart

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

Offline Offline
Full Member
***
Karma: 0
Posts: 128
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Rob, the code takes some understanding (by a learner!!), but I'm working my way through it.
I'm stuck with;
for(int i=0; i< 10; i++)
What does this do??
Logged

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


See reference page - http://www.arduino.cc/en/Reference/For -
Logged

Rob Tillaart

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

Offline Offline
Full Member
***
Karma: 0
Posts: 128
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

But i does not appear to be used anywhere?
int i=0; //On first run sets the value of i as 0
i< 10; //checks to see if i is less than 10
i++  //adds one to the value of i

Does it run through 10 times and then execute the next piece of code?  i.e. a delay of 10 cycles?


« Last Edit: July 19, 2011, 01:50:33 pm by pauldreed » Logged

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

Code:
  for(int i=0; i< 10; i++)
  {
    delay(10);
    Serial.println(MyMillis());
  }

you almost got it, the above code does execute the block  between { and } 10 times

you can use the i in side the block too

example:
Code:
  for(int i=0; i< 10; i++)
  {
    delay(i);
    Serial.println(MyMillis());
  }

here the i causes different  (increasing)  delay's

for has a few special tricks:

endless loop
Code:
for ( ; ; )
{
  ...
}
executes the block forever

Code:
int i=0;
for (i=0 ; i<10 ;i++ )
{
  ...
}
for (  ; i>0 ;i-- )
{
  ...
}
two for loops, the int i is declared outside the for loops so it can be reused, the first loop counts to 10 and the second loop counts back to 0.
This is possible as the second for does not (re)initialize the var i.







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 3   Go Up
Jump to: