# Extracting info from ds1307 for calculations

Hi

Could someone please explain to me how to extract one part of the information from the ds1307 to use in a calculation.

I am trying to make a 2 digit 7 segment display to countdown the days to an event and I want to make a calculation based on the day, month and year that the ds1307 returns and display the result on the 7 segment led display and then countdown 1 day at a time from there.

I have read several tutorials etc on how to create clocks with the unit but nothing that seems to explain clearly how to extract one part of the data and use it.

Hopefully it isn't blindingly obvious!

Thanks Steve

Have you looked at the time library?

http://www.arduino.cc/playground/Code/Time

Hi Yes I have seen that. It gives plenty of examples for setting the time and making adjustments but doesn't seem to specify how to use one part of the time result in a formula.

Although I haven't downloaded the examples and had a good look so I will do so now. Thanks

Huh? It doesn't? The first section of the function overview shows the function calls return the minutes, hours, and seconds:

``````hour();            // the hour now  (0-23)
minute();          // the minute now (0-59)
second();          // the second now (0-59)
day();             // the day now (1-31)
weekday();         // day of the week, Sunday is day 1
month();           // the month now (1-12)
year();
``````

And of course here is code calling Serial.print() with integers returned from the same simple calls.

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

The examples show how to setup the RTC with a ds1307 (made much easier if you use the accompanying Processing sketch.) After that you just call "hour()" to get what the current hour is.

Thanks I nearly wrote in my last reply, is it just as easy as calling hour() to use it in your equation but I got bogged down with some other details.

Thanks for clearing it up for me.

Using the example in the time folder RTC-Set_Time I get back a different time to what I expect.

It should set the time according to the system time but mine comes back saying it is 10:50 am when my system time says it is actually 1:15PM. Is there a setting somewhere there I am missing?

Are you running from the Processing sketch which actually does the synchronization?

The way the examples work is that they initialize the RTC (whether it is an internal counter or a DS1307) and then attempt to synchronize with a PC-client.

Alternatively, you can simply set the clock with one sketch and then run other sketches to check the time.

I used the following code which if I am reading it correctly should set the rtc to the same as the system time.
It doesn’t matter really as I have seen a sketch to manually set the time and I can use that if needed.

``````/*
* 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

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
Serial.print(c);
if( c == TIME_HEADER ) {
time_t pctime = 0;
for(int i=0; i < TIME_MSG_LEN -1; i++){
if( c >= '0' && c <= '9'){
pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
}
}
return pctime;
}
}
return 0;
}
``````

I used the following code which if I am reading it correctly should set the rtc to the same as the system time.

Only if you are using the accompanying Processing sketch.

Yes, I loaded that straight from the examples directory of the arduino folder. All accompanying files are in the library folder so it should have worked.

What version of the Arduino IDE are you using?

What Operating System are you using?

What version of Processing are you using?

Did you change portIndex in the Processing sketch before running it?

Okay
I think I am doing something wrong.
I have little experience with processing.
I am using arduino 0022, processing 1.51, Windows 7.
I have installed processing, created a libraries folder in the processing folder that is in my documents by default, added the arduino folder to the libraries folder that I downloaded.
I then went into sketch, import library and tried to import the arduino library and I got a 1 line response. And running the sketch gave me a little grey window beside the processing ide.

How do the 2 of them work together? I am getting a little lost.
Do I open arduino, upload the standard firmata sketch to my arduino uno then in processing I open the example sketch, in this case TimeRTCSet and run it from processing?

I also got a little lost setting up processing for serial communication.
Sorry for the hassle but thank you for helping me.