RTC saving the time

Hi there

I have a DS1307 to give me the current time in an alarm clock project. I am trying to adjust the time/date in my project. ie usually the time is taken from the DS1307 but I want the user to be able to change the date/time if desired. I have been looking at - DS1307 RTC tutorial and using this as my template.

In the example the use the line below to update the DS1307 with the time/date at the time the code was compiled -
RTC.adjust(DateTime(DATE, TIME));

My dates and times are stored in the following variables - secondnow, minutenow, hournow, daynow, monthnow and yearnow

I am not the best programmer in the World and I am struggling to see how to use the adjust function in the way I need to. Can anyone make any suggestions? I have tried constructing a DateTime variable called newtime and then using this in the with the .adjust function with no success.

David

In the example the use the line below to update the DS1307 with the time/date at the time the code was compiled -
RTC.adjust(DateTime(DATE, TIME));

Perhaps if you printed DATE and TIME, you'd get a clue.

Then, the sprintf() function might come in handy.

hai
you can change time and date by using push button. For eg. you set 4 push button as read, and each select as fist one for "menu" second "set", third "up" then "down".
then from code edit in the line where we get data getdate something like that
there you use pointer variable for easy access

in your exmple code just avoid the comment lines
ie use as
RTC.adjust(DateTime(DATE, TIME));
only
and that automaticaly collect the details from your compailing computer. If your computer date and time is not correct , change that first..

djm2:
I have a DS1307 to give me the current time in an alarm clock project. I am trying to adjust the time/date in my project. ie usually the time is taken from the DS1307 but I want the user to be able to change the date/time if desired.

If the RTC is your primary time source and you want to adjust it, I can see two ways:

  • Adjust the clock by reading the current time, adding or subtracting your adjustment amount and writing the value back to the clock.

  • Store an offset in non-volatile storage; add this offset to the RTC time in order to get your adjusted time. This approach leaves the RTC unchanged which might be handy if you want the RTC to remain synchronised with an external time source. When the user 'adjusts' the time, all you do is increment or decrement this offset.

Hi there

Thanks for the replies. I had not thought of using an offset to adjust the time. I think I would like to change the actual time if I could to keep things simpler.

I checked the contents of DATE and also of TIME and found it to be - Nov 12 201213:54:01 but I am still unsure of how to send this back to the RTC.

I also found another problem. To get each part of the time/date info I use -

secondnow = now.second();

But this is not correct. The line below does work and sends it out on the Serial port -

Serial.print(now.second(), DEC);

I have everything stored as unsgined chars apart from the year which is an unsigned int. I cant figure out what code to use to store the correct value into the variable. I was looking at sprintf but just got confused.

David

I checked the contents of DATE and also of TIME and found it to be - Nov 12 201213:54:01 but I am still unsure of how to send this back to the RTC.

The example could just as easily have been:

RTC.adjust(DateTime("Nov 12 2012", "13:54:01"));

The DateTime function takes a date string and a time string and makes something that RTC.adjust() likes.

Having month, day, year, hour, min, and sec, it's a three step process to create the necessary input.

  1. Create an array with month names, with 13 elements. Put junk in the 0th position, so you can use month as an index.

  2. Use sprintf() to create the date string, using the month name, looked up using the month value, the day value and the year value.

  3. Use sprintf() to create the time string, using hour, minute, and second values.

Then, call DateTime() with the two strings, and call RTC.adjust() with the DateTime output.

I also found another problem. To get each part of the time/date info I use -

secondnow = now.second();

Without any context, we can't agree or disagree. Post your code.

I have everything stored as unsgined chars apart from the year which is an unsigned int.

Why? hours, minutes, and seconds are ints, too.