How accurate is delay();

Let's say I want delay of a week it's 6.048e+8 milliseconds,how much would my delay be wrong,hope you understand my question

You should be able to delay up to just under 50 days (the number of milliseconds represented by a 32 bit number). Some Arduinos have a ceramic resonator and the timekeeping is not very accurate (minutes or more per day). The ones with a crystal are better. If you want to handle power failures etc., it would be better to use a Real Time Clock with battery backup, and use the alarm function or code a similar function yourself.

First off, don't use delay() at all. Use millis() to manage timing without blocking as in Several Things at a Time

Compared to a proper clock the Arduino is not a good time-keeper. You could test that by writing a program to print the value of millis() every minute and compare it with a stopwatch (most phones have stopwatches) for, say, 30 minutes or an hour.

If you want timing that is comparable with a clock you need an RTC module.

...R

So,it would be better to get a real time clock...and can you guys give me example program that counts numbers every second 1...2...3...4 and prints it to serial,because I have no idea how to write that and I would like to test arduino as a RTC to see how bad it really is :D

Install the Time library. I think there is an example sketch that comes with it that you can use to test.

To see how good (or bad) the Arduino is, you could use such a sketch (untested) to count seconds to the serial monitor :

void setup() {
   Serial.begin( 9600 ) ;
}

loop() {
   static unsigned long secondsElapsed = 0 ;
   static unsigned long countMs = 0 ;
   if ( millis() - countMs > 1000UL ) {
      countMs = millis() ;
      Serial.print ( "Second = " ) ;
      Serial.println ( secondsElapsed ) ; 
      secondsElapsed ++ ;
   }
}
  if ( millis() - countMs >= 1000UL ) {
      countMs += 1000UL;

Take 0.5% accuracy as a reasonable estimate of the Uno’s clock’s accuracy. After a week, the clock will have drifted by 50 minutes.

Use an RTC. A DS3231 has an internal, temperature compensated crystal with a maximum accuracy of 2 ppm. That’s a maximum of 1 minute of drift per year, 2,500 times better than a resonator.

Datasheet - DS3231 RTC.pdf (824 KB)

unsigned long currentMicros;
unsigned long previousMicros;
unsigned long elapsedTime;

byte hundredths;
byte tenths;
byte secondsOnes;
byte oldSecondsOnes;
byte secondsTens;
byte minutesOnes = 0;
byte minutesTens = 4;
byte hoursOnes = 1;
byte hoursTens = 1;

void setup(){

  Serial.begin(115200); // make serial monitor match
  currentMicros = micros();
  previousMicros = currentMicros;
  Serial.println ("Setup Done");
}

void loop(){

  currentMicros = micros();

  // how long's it been?
  elapsedTime = currentMicros - previousMicros;
  // Serial.print ("Elapsed: ");   <------ comment out
  //  Serial.println (elapsedTime); <------ comment out
  if ( elapsedTime >=10000UL){  // 0.01 second passed? Update the timers
    elapsedTime = 0;
    previousMicros  = previousMicros + 10000UL;
    hundredths = hundredths+1;
    if (hundredths == 10){
      hundredths = 0;
      tenths = tenths +1;
      if (tenths == 10){
        tenths = 0;
        secondsOnes = secondsOnes + 1;
        if (secondsOnes == 10){
          secondsOnes = 0;
          secondsTens = secondsTens +1;
          if (secondsTens == 6){ 
            secondsTens = 0;
            minutesOnes =  minutesOnes + 1;
            if (minutesOnes == 10){
              minutesOnes = 0;
              minutesTens = minutesTens +1;
              if (minutesTens == 6){
                minutesTens = 0;
                hoursOnes = hoursOnes +1;
                if (hoursOnes == 10){
                  hoursOnes = 0;
                  hoursTens = hoursTens =1;
                  if (hoursOnes == 4 && hoursTens ==2){
                    hoursOnes = 0;
                    hoursTens = 0;
                  }
                }
              } // minutesTens rollover check
            } // minutesOnes rollover check
          } // secondsTens rollover check
        } // secondsOnes rollover check
      } // tenths rollover check
    } // hundredths rollover check
  } // hundredths passing check



  if (oldSecondsOnes != secondsOnes){  // show the elapsed time
    oldSecondsOnes = secondsOnes;
    Serial.print ("Time: ");
    Serial.print (hoursTens);
    Serial.print(hoursOnes);
    Serial.print(":");
    Serial.print(minutesTens);
    Serial.print(minutesOnes);
    Serial.print(":");
    Serial.print(secondsTens);
    Serial.println(secondsOnes);

/*
 if ( hoursTens == 0 && hoursOnes == 6 ** minutesTens == 0 && minutesOnes == 0 && secondsTens == 0 && secondsOnes == 0){
// alarm time!
}
*/

  } // end one second check
} // end loop

? Sketch only produces random numbers, and locks up my IDE after some time. Leo..

comment out this two lines of code will not produces random numbers

  // how long's it been?
  elapsedTime = currentMicros - previousMicros;
  // Serial.print ("Elapsed: ");    // <---comment out
  // Serial.println (elapsedTime);  // <-- comment out
  if ( elapsedTime >=10000UL){  // 0.01 second passed? Update the timers

AWOL:   if ( millis() - countMs >= 1000UL ) {

Well spotted.

aarg:       countMs += 1000UL;

I'm not so sure about that ?

You all missed:

void loop() {

:)

6v6gt: I'm not so sure about that ?

I am. Ad nauseam.

Good:

   if ( millis() - countMs >= 1000UL ) 
   {
      countMs += 1000UL;

      Serial.print ( "Second = " ) ;
      Serial.println ( secondsElapsed ) ; 
      secondsElapsed ++ ;
   }

Better:

   if ( millis() - countMs >= 1000UL ) 
   {
      while(millis() - countMs >= 1000UL)
         {
             countMs += 1000UL ;
         }
      Serial.print ( "Second = " ) ;
      Serial.println ( secondsElapsed ) ; 
      secondsElapsed ++ ;
   }

If the while loops more than once, secondsElapsed is ... ? ;)

[quote author=Coding Badly date=1495951822 link=msg=3278103] If the while loops more than once, secondsElapsed is ... ? ;) [/quote]

You are always thinking.

   while(millis() - countMs >= 1000UL)
         {
             countMs += 1000UL ;
             secondsElapsed ++ ;
         }

.

larryd: You are always thinking.

Ugh. I wish that were true. I just accidentally dumped a glass of milk on my keyboard.

[quote author=Coding Badly date=1495953473 link=msg=3278117] Ugh. I wish that were true. I just accidentally dumped a glass of milk on my keyboard. [/quote]

|500x201

.

For that, good sir, you get some karma!