sync arduino rtc with system clock

Hi

I am working for the first time with an rtc. I made the following code to syncronize my rtc with my laptop time. I have a problem with the millis. I tried to fix all the possible errors that I found similar in the forum but I still continue to get the same error. the error is:
libraries\DateTime\DateTime.cpp: In member function 'void DateTimeClass::setTime(time_t)':
libraries\DateTime\DateTime.cpp:28: error: 'millis' was not declared in this scope
libraries\DateTime\DateTime.cpp: In member function 'time_t DateTimeClass::now()':
libraries\DateTime\DateTime.cpp:43: error: 'millis' was not declared in this scope

Can someone help me with this problem as it seems to be in the datetime library

Show us your code:
See # 7
http://forum.arduino.cc/index.php/topic,148850.msg1118324.html#post_codetags

Where did you put the Time library?

Thanksfor the reply
Here is my code:

#include <DateTime.h>
#include <DateTimeStrings.h>
#include <Time.h>

#define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER  255   // Header tag for serial time sync message

void setup(){
  Serial.begin(19200);
  pinMode(13,OUTPUT); // we flash the LED each second
}

void  loop(){
  unsigned long  prevtime;
  if( getPCtime()) {  // try to get time sync from pc
    Serial.print("Clock synced at: ");
    Serial.println(DateTime.now(),DEC);
  }
  if(DateTime.available()) { // update clocks if time has been synced
    digitalWrite(13,LOW);  // first flash the LED
    prevtime = DateTime.now();
    while( prevtime == DateTime.now() )  // wait for the second to rollover
        ;
    DateTime.available(); //refresh the Date and time properties
    digitalClockDisplay( );   // update digital clock

    // send our time to any app at the other end of the serial port
    Serial.print( TIME_HEADER); // this is the header for the current time
    Serial.println(DateTime.now());
    digitalWrite(13,HIGH);
  }
  delay(100); 
}

boolean getPCtime() {
  // if time sync available from serial port, update time and return true
  while(Serial.available() >=  TIME_MSG_LEN ){  // time message consists of a header and ten ascii digits
    if( Serial.read() == TIME_HEADER ) {        
      time_t pctime = 0;
      for(int i=0; i < TIME_MSG_LEN -1; i++){   
        char c= Serial.read();          
        if( c >= '0' && c <= '9'){   
          pctime = (10 * pctime) + (c - '0') ; // convert digits to a number    
        }
      }   
      DateTime.sync(pctime);   // Sync Arduino clock to the time received on the serial port
      return true;   // return true if time message received on the serial port
    }  
  }
  return false;  //if no message return false
}

void digitalClockDisplay(){
  // digital clock display of current date and time
  Serial.print(DateTime.Hour,DEC);
  printDigits(DateTime.Minute);
  printDigits(DateTime.Second);
  Serial.print(" ");
  Serial.print(DateTimeStrings.dayStr(DateTime.DayofWeek));
  Serial.print(" ");
  Serial.print(DateTimeStrings.monthStr(DateTime.Month));
  Serial.print(" ");
  Serial.println(DateTime.Day,DEC); 
}

void printDigits(byte digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits,DEC);
}

The DateTime library is out dated, has been superseded by a newer version that is available here.

I m still not receiving the date and time from the laptop. I am still getting
"waiting for sync message" when using the program you showed me :frowning:

What OS your laptop used?

You can try to sends these messages T1389106357 using the serial monitor or you can look up for current time

If you use Linux, try this shell command:

TZ_adjust=-8; echo T$(($(date +%s)+6060$TZ_adjust)) > /dev/tty.usbserial-A8008pym

or

TZ_adjust=5.5;d=$(date +%s);t=$(echo "6060$TZ_adjust/1" | bc);echo T$(echo $d+$t | bc ) > /dev/ttyACM0

If you using Mac, try this command

To set the variable to EST
TZ_adjust=-5;

sudo echo "T$(($(date +%s)+6060$TZ_adjust))" >/dev/tty.usbmodemfa131

No i m on windows 8

Stefan_28:
No i m on windows 8

What software are you running on the PC to provide the time service via the serial port?

I dunno... is there a way how i can find out?

Besides now i have found a new solution. my main problem is that i only need to sync the rtc once. the main problem is that once i disconnect the rtc from the power it looses the time and date. Is there a way where it keeps the time and date? i think this is possible since it comes with a battery

Stefan_28:
I dunno... is there a way how i can find out?

Unless you've run it there isn't anything, which probably explains why the Arduino is timing out waiting for a response from it.

once i disconnect the rtc from the power it looses the time and date. Is there a way where it keeps the time and date? i think this is possible since it comes with a battery

If you have the so-called "tiny RTC" see this thread regarding backup battery problems: http://forum.arduino.cc/index.php?topic=209057.msg1536642#msg1536642