an error in delay function

Hello,

I have write a code for delay function for different uses. Every one"void loop()" function I call a function in a library class with “for loop” from 0 to 100millisecods with 4++ at tow time delay 2ms, as shown below.
however every main void loop () I print the time [ whish is 100ms ] for test, when I opened the serial monitor and start a chronometer on laptop or phone , I encountered an error of 5 seconds after 4 min (which is different between serial monitor and chronometer) and 12s error after 8 min and so on the more the time pass the more the difference error … however this is huge error I dont know why their is such error in delay, I want a maximum error of 1 second after 5 hours. kindly advice

The functions I writted in the test below

////////// IN MAIN CLASS

void loop()
{
//Start_tracking_siderealRate();
times+=1;// Equal to 100 millisecond

Serial.println(“times”);
Serial.println(times);

Astro.SiderealRate(); // THIS IS A LIBRARRY
}

/////////

A FUNCTION IN THE LIBRARY

void Astrolink::SiderealRate()
{

for(t=0; t<100;t+=4)
{

delay(2);
digitalWrite(_step,HIGH);
delay(2);
digitalWrite(_step,LOW);

}

}

I think what you want is impossible with arduino. Even your $2k PC cannot track accurate real time.

Beside, digitalWrite() takes time too.

@op

  1. get rid of the delay()'s

  2. do not use delay

  3. do not ever use delay

  4. never sit in a loop in this way waiting for something to happen

  5. Just use an RTC

@arduino_new

I want a maximum error of 1 second after 5 hours. kindly advice

Even your $2k PC cannot track accurate real time.

Any cheap PC, plamtop, phone, etc - can do this!

Mark

holmes4: @op

  1. get rid of the delay()'s

  2. do not use delay

  3. do not ever use delay

  4. never sit in a loop in this way waiting for something to happen

  5. Just use an RTC

@arduino_new

Any cheap PC, plamtop, phone, etc - can do this!

Mark

Those devices themself need to sync to an external server.

holmes4: @op

  1. get rid of the delay()'s

  2. do not use delay

  3. do not ever use delay

  4. never sit in a loop in this way waiting for something to happen

  5. Just use an RTC

Mark

How to use RTC instead of delay, and have the same function for digitalwrite()??

arduino_new:
I think what you want is impossible with arduino. Even your $2k PC cannot track accurate real time.

Beside, digitalWrite() takes time too.

1 second per 5 hours is about 55 ppm accuracy. That’s well within the tolerance of a good watch crystal. You won’t get it from an Uno board alone, but you can easily get an accessory that will work.

For a specific recommendation, a DS3231 has a temperature-compensated crystal oscillator in it and the datasheet says it has 2 ppm accuracy, over 20 times better than what is needed here.

Jiggy-Ninja: 1 second per 5 hours is about 55 ppm accuracy. That's well within the tolerance of a good watch crystal. You won't get it from an Uno board alone, but you can easily get an accessory that will work.

For a specific recommendation, a DS3231 has a temperature-compensated crystal oscillator in it and the datasheet says it has 2 ppm accuracy, over 20 times better than what is needed here.

Ok buut the error I have for now is 5 seconds after 4 min, I think arduino uno can get more accuracy than that 5 seconds 1 hour?? what other solution????

No realistic "other".

Mark