millis() wrong value

Hi i did a test to see the value of the timer millis(), with my stop watch of my phone. after 2 minute. the value of millis() is not exactly with my stop wath() why ??

Did you check the stop watch of your phone against a known good time source? Don't assume that the app bothers to sync with the phone's network time.

Anyway, what board? Most of them have a resonator instead of a crystal timebase, which accounts for the inaccuracy. It's a well known deficiency.

Because your stopwatch is inaccurate :smiley:

Can you post the code that you used? Please between

[code] and [/code]

so it looks like

your code here

millis() cannot be used for accurate time measurements. It's only good for measurements of a few seconds at best, because error will add up quickly.

If you want an accurate time, use an RTC such as the DS3231 (breakout boards available on ebay for less than 2€)

i tested with my Computer and always the millis() give me a wrong value after 3 minute.

here is the code

loop
{
Serial.print(millis()/1000);

}

How about some actual numbers? How wrong?

jone31:
i tested with my Computer and always the millis() give me a wrong value after 3 minute.

here is the code

loop

{
Serial.print(millis()/1000);

}

Your code hasn't a hope of compiling.
Please stop wasting time. (no pun intended)

here is the code ,

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
Serial.println(millis()/1000);
}

when he arduino shiw me 400 seconds in the real time it's 389 second for exemple

Nobody's arguing with you....

aarg:
It's a well known deficiency.

jone31:
here is the code ,

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
Serial.println(millis()/1000);
}

when he arduino shiw me 400 seconds in the real time it's 389 second for exemple

I think a more sensible test (and some code tags is in order)

Which version Arduino? There are many.

I have the uno r3

jone31:
I have the uno r3

It has the resonator. You need an RTC if you expect to keep better time. I recommend the DS3231.

so we can't do HARD RTOS with arduino due to the resonator :(. and if a add this DS3231. with other sensors , it could be or not. i want to say if i add the DS3231. i can add other i2c devises ?? like imu

If you have an RTC, you can calibrate the offset every second, and observe millisecond time with no more than a few milliseconds error.

That should definitely not stop you from running an RTOS. Another possibility is to get a board that does have a crystal. All the 32U4 based boards have it, and so do some 328 based clones.

I2C is a serial bus, you can have multiple devices on it. I do.

can add other i2c devises

Yes, as long as the addresses don't clash

AWOL:
Yes, as long as the addresses don't clash

Good point. Sometimes you can change the addresses with solder jumpers. Usually it's not a problem.

Try this:

unsigned long start, realSec = 1000UL * 400 / 379;  

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  static unsigned int seconds;
  if(millis() - start > realSec){
    Serial.print(++seconds); Serial.print("\t");
    Serial.println(millis() / 1000);
    start = millis();
  }
}

outsider:
Try this:

Until the temperature changes, then you're way off again.

@aarg:
You're right of course I was just saying you can fudge a little in the short term.