now() vs millis()

Having used millis() i did not want the accuracy of milli seconds hence i use

unsigned long sec() {return millis()/1000;}

to get the number of seconds that have elapsed since the boot.The returned value is stored in an unsigned long and then used.

1.Can I safely assume that the highest number in unsigned long can be 4294967295.... i.e 4294967295 seconds or 4900~ days.

but when millis() rolls over the value returned will be closer to 0,

2. Can i use now() to return the seconds and then use that rather than use millis() so the roll over would occur every 4900 days.

  1. Yes
  2. Yes, or you can use millis to implement a 32 bit counter that will increment every second. It would be a simpler choice if you are not using any other functions from the Time library.

anirudhashish:

1.Can I safely assume that the highest number in unsigned long can be 4294967295

Yup.

.... i.e 4294967295 seconds or 4900~ days.

Wrong by an order of magnitude.

but when millis() rolls over the value returned will be closer to 0,

millis does rollover to zero.

2. Can i use now() to return the seconds and then use that rather than use millis() so the roll over would occur every 4900 days.

No. The rollover, with or without your division by 1000, is going to be at ~49 days.

You need to blink-without-delay once a second accumulating seconds to get what you want.

[quote author=Coding Badly link=msg=2702947 date=1460191388] No. The rollover, with or without your division by 1000, is going to be at ~49 days. [/quote]

I think the question to that answer was, when does the result from now() roll over? That is many, many years.

unsigned long sec() {return millis()/1000;}

1.Can I safely assume that the highest number in unsigned long can be 4294967295...

Yes, but ...

i.e 4294967295 seconds or 4900~ days.

Since millis() returns a long, the max value that your sec() function will return is going to be something like 4294967. It will never fill all 32bits, and it won't roll over any slower than millis(). There used to be an annoying bug in millis() that caused it to roll over at weird times, because of internal math that it was doing - you need to remember that the 32bit limit is going to apply to ALL the parts of your calculation.

Can i use now()

What "now()" ?? Did you mean you sec() function?

westfw: What "now()" ?? Did you mean you sec() function?

I think it is reasonable to assume that now() from the Time library is the one mentioned, because it is so commonly used.

The way I interpreted part of the original question, was that counting seconds was sufficient. A 32 bit counter counting seconds will obviously be 1000 times slower than one counting milliseconds. So it will obviously overflow 1000 times less frequently.

If you don’t need millisecond resolution, then just don’t use it. I can see no need to create a separate “seconds” function. Example from code I use, no problems with overflow:

const uint32_t SECOND = 1000; // millis per second
...
const uint32_t INTERVAL_updateTemperature = 5 * SECOND;
...
 if (millisNow - millisLastTemperatureUpdate > INTERVAL_updateTemperature) {
...
    millisLastTemperatureUpdate = millisNow;
  }

anirudhashish:
Having used millis() i did not want the accuracy of milli seconds hence i use

unsigned long sec() {return millis()/1000;}

to get the number of seconds that have elapsed since the boot.The returned value is stored in an unsigned long and then used.

That is not the correct way to get a seconds equivalent to millis(). You should do something like this (not tested)

unsigned long sec() {
   static unsigned long secondCounter = 0;
   static unsigned long prevSecMIllis = 0;
   if (millis() - prevSecMillis >= 1000) {
       prevSecMillis += 1000;
       secondCounter ++;
   }
   return secondCounter;
}

This will roll over after a VERY LONG time - 49000 days, I think.

…R

olf2012: If you don't need millisecond resolution, then just don't use it. I can see no need to create a separate "seconds" function. Example from code I use, no problems with overflow:

There is a need to create a separate seconds function, if the time period to be measured exceeds 49 days. For example, the code in the previous reply.

[quote author=Coding Badly link=msg=2702947 date=1460191388] Yup.

Wrong by an order of magnitude.

millis does rollover to zero.

No. The rollover, with or without your division by 1000, is going to be at ~49 days.

You need to blink-without-delay once a second accumulating seconds to get what you want.

[/quote]

CB... wonder why you say so... ??

anirudhashish: CB... wonder why you say so... ??

Read reply #7.