Setting time on the Ds 1307 RTC

Hi guys. I am building a digital Guage pressure meter for a design project at college.
My sensor and display is working well, I am also logging data to my PC.
Which is where my problem comes in, I am using the DS1307 RTC and I cannot set the time.
I have all the libaries from arduino for time. I just dont know how to set the time.
I really need some help to finally complete my design.

The code below is the code i am using from the library. I just dont know how to alter the code to set the time. I've tried so many ways and is really stressed out. Please help me. Thanks

/*

  • TimeRTCSet.pde
  • example code illustrating Time library with Real Time Clock.
  • RTC clock is set in response to serial port time message
  • A Processing example sketch to set the time is inclided in the download
    */

#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t

void setup() {
Serial.begin(9600);
setSyncProvider(RTC.get); // the function to get the time from the RTC
if(timeStatus()!= timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
}

void loop()
{
if(Serial.available())
{
time_t t = processSyncMessage();
if(t >0)
{
RTC.set(t); // set the RTC and the system time to the received value
setTime(t);
}
}
digitalClockDisplay();
delay(1000);
}

void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}

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

/* code to process time sync messages from the serial port */
#define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER 'T' // Header tag for serial time sync message

time_t processSyncMessage() {
// return the time if a valid sync message is received on the serial port.
while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of a header and ten ascii digits
char c = Serial.read() ;
Serial.print(c);
if( c == TIME_HEADER ) {
time_t pctime = 0;
for(int i=0; i < TIME_MSG_LEN -1; i++){
c = Serial.read();
if( c >= '0' && c <= '9'){
pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
}
}
return pctime;
}
}
return 0;
}

The PC has to send 'T' followed by 10 digits. Those 10 digits are the value of a time_t variable: the number of seconds since January 1, 1970.

Thank you for your reply.

I undrestand what you are talking about by the PC sending the Unix time. The thing is i dont know how to let the PC send the unix time. I have tried adding the seconds in places of "t" but i think im putting it in the wrong place. I just dont understand the setting code for time

dorain:
The thing is i dont know how to let the PC send the unix time.

't' is not the same as 'T'.

The PC has to send 'T' followed by 10 digits. Those 10 digits are the value of a time_t variable: the number of seconds since January 1, 1970.

I'm not sure it gets much clearer than that.

  1. Open Serial Monitor
  2. Type the letter "T" then type (or paste) the number of seconds since January 1, 1970. e.g.: "T1337445012"

I believe that the "Unix Time" required for this purpose is not ordinary Unix Time.
The difference? Time zones.

First, let me give you an illustration of what Unix Time means.
Imagine a clock with no date, hours, or minutes. It only counts seconds... but it can count very high, even into the billions.
Again, it only counts seconds, and it knows nothing of time zones. And there is only this one clock, with its count of seconds, to serve the whole world.

At some point in history, naturally, this clock started counting up from zero.
When, exactly, did the clock start counting? It depends who you ask.
As we on the East Coast of the United States reckon time, this happened on Wednesday, December 31, 1969, at 7:00 pm.
A person on the West Coast of the United States would report this as Wednesday, December 31, 1969, at 4:00 pm.
A person in Italy would report it as Thursday, January 1, 1970, at 1:00 am (late Wednesday night, that is).
A person in Japan would report it as Thursday, January 1, 1970, at 9:00 am.
An astronomer or navigator using Greenwich Mean Time would report it as the midnight between Dec. 31, 1969 and Jan. 1, 1970.

Since I am on the East Coast of the United States, I will use my reckoning.
On Dec. 31, 1969, at 7:00:00 pm, the clock showed 0 seconds.
On Dec. 31, 1969, at 7:00:01 pm, the clock showed 1 second.
On Dec. 31, 1969, at 7:00:02 pm, the clock showed 2 seconds.
... ... ...
On Dec. 31, 1969, at 7:01:00 pm, the clock showed 60 seconds.
On Dec. 31, 1969, at 7:01:01 pm, the clock showed 61 seconds.
... ... ...
When the ball in Times Square dropped to signal the New Year 1970, the clock showed 18000 seconds. I reckon as follows:
1 minute = 60 seconds
1 hour = 60 minutes = 60 * 60 seconds = 3600 seconds
time from 7 pm to 12 midnight = 5 hours = 5 * 3600 seconds = 18000 seconds

Let's suppose that on Friday, January 2, 1970, at 9:00 am, I go to check the clock. It shows 136800 seconds. Reckoning:
7 pm Wednesday to 7 pm Thursday = 24 hours
7 pm Thursday to 9 am Friday = 14 hours
24 hours + 14 hours = 38 hours = 136800 seconds

Now, suppose my friend in Japan goes to check the clock, again on Friday, January 2, 1970, at 9:00 am. For him the clock will show only 86400 seconds. That, after all, is the number of seconds in 24 hours, which is the length of time since the clock showed zero.

136800 seconds != 86400 seconds. How is this possible?
Because the time on the USA East Coast != the time in Japan
(that is, my "9:00 am" is NOT the same as his "9:00 am")

All right. Enough playing around. For me (again, on the East Coast of the USA) on Saturday, May 19, 2012, at 6:00 pm, what time will the clock show?

Dec. 31, 1969 to Dec. 31, 2009 = 40 years, of which 10 are leap years
(40 * 365) + 10 = 14610, so 14610 days so far
Dec. 31, 2009 to Dec. 31, 2011 = 2 * 365 days = 730 days
14610 + 730 = 15340 days so far
Dec. 31, 2011 to May 18, 2012 = (31 + 29 + 31 + 30 + 18) days = 139 days
15340 + 139 = 15479 days so far
Check: start = Wed, end = Fri, therefore some number of weeks plus 2 days
15479 = (2211 * 7) + 2, so it seems we're OK
How many seconds in these 15479 days?
1 day = 24 hours = 24 * 3600 seconds = 86400 seconds
15479 days = 15479 * 86400 seconds = 1337385600 seconds... right? Not quite.
One of those "days" has only 23 hours. This is because of a Daylight Saving Time adjustment.
I'm not playing word games here. If you were to start a stopwatch at 12:00 midnight on the night of March 10-11, 2012, and stop it at midnight the next night, it would only show 23 hours, not 24.
So... we really have 1337385600 minus 3600 = 1337382000 seconds so far.
But we are only up to 7 pm on the day before the day we care about. Let's add in another 23 hours = 82800 seconds, to get our final answer:
1337464800 seconds
A look at unixtime.info confirms this figure.

But if I remember correctly, the Arduino time libraries use a flawed implementation of Unix Time. Their version has the clock start at midnight local time at the beginning of the year 1970, and it also has the clock shifted forward or backward 3600 seconds at each and every Daylight Saving Time change. Certainly not the simple, straightforward, and uniform count of seconds I have described above.

Do you want the clock to keep local time or Greenwich Mean Time? If local time, what do you want to do at Daylight Savings Time changes?

Maybe I should put in timezone handling (and Daylight Saving Time handling) as a feature request for the Arduino.

I got this from the library example:

#include "RTClib.h"

RTC_DS1307 RTC;

void setup (){ 

 // following line sets the RTC to the date & time this sketch was compiled
   RTC.adjust(DateTime(__DATE__, __TIME__));
}

make sure you compile and upload (if this takes to long your arduino is A few seconds behind).

edit: I see you use another library, maybe you can run this to set the time and than upload your program.

I have used the above said library and it functions well for me

hey guys thanks for all the replies. I used the RTClib.h library and the date can be updated from my pc but not the time. Thats the problem. the time doesnt wanna set. I am from south africa, lol my time zone is different