Go Down

Topic: Reset millis (Read 2857 times) previous topic - next topic

pauldreed

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?

PaulS

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.

Cylindric

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 :)
Current Projects: Rodentometer - Hamster Endurance Moni

James22

Thanks for getting us know about it..

AWOL

Quote
Thanks for getting us know about it..

My spidey spam senses are tingling.
"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.

wildbill


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 :)


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

Cylindric

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 :)
Current Projects: Rodentometer - Hamster Endurance Moni

pauldreed


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.

PaulS

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.

Cylindric

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.
Current Projects: Rodentometer - Hamster Endurance Moni

robtillaart


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

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.
Rob Tillaart

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

pauldreed

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??

robtillaart


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

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

pauldreed

#13
Jul 19, 2011, 08:38 pm Last Edit: Jul 19, 2011, 08:50 pm by pauldreed Reason: 1


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

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?



robtillaart

Code: [Select]

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

  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: [Select]
for ( ; ; )
{
  ...
}
executes the block forever

Code: [Select]

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.







Rob Tillaart

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

Go Up