The RTC in have wired to my arduino has been acting rather strange.
First, it doesn't seem to be keeping the correct time. I had the program ( a simple dim on/off program for LED's, trying to simulate sunrise/sunset) working perfectly last night, then when I wake up this morning they haven't started their dim on cycle like they should have. I plug the usb cable into the computer and open the serial monitor it reads the time as 10:47 a.m. instead of 11:36 a.m. This leads me to my second problem.
Every once in a while when I try to recompile and upload it, when I open the serial monitor it gives a completely different time than the computer, which is what it should be setting itself off of. For example it's 11:36 a.m. right now (correct on my computer) and after recompiling and re-uploading the program still thinks it is 10:47 a.m.
Additionally I watched the serial monitor for a moment and caught this:
2012/10/14 10:49:22
since 1970 = 1350211762s = 15627d
now + 7d + 30s: 2012/10/21 10:49:52
2012/10/14 10:49:24
since 1970 = 1350211764s = 15627d
now + 7d + 30s: 2012/10/21 10:49:54
I set the delay to (1000) so it should print every second, but sometimes it skips one.
Then scrolled along and found something even more disturbing:
2012/10/14 10:49:56
since 1970 = 1350211796s = 15627d
now + 7d + 30s: 2012/10/21 10:50:26
2012/10/14 10:49:57
since 1970 = 1350211797s = 15627d
now + 7d + 30s: 2012/10/21 10:50:27
2012/10/14 10:49:58
since 1970 = 1350211798s = 15627d
now + 7d + 30s: 2012/10/21 10:50:28
2165/165/165 165:165:85
since 1970 = 2028820689s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39
2165/165/165 165:165:85
since 1970 = 2028820689s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39
2165/165/165 165:165:85
since 1970 = 2028820689s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39
2165/165/165 165:165:85
since 1970 = 2028820689s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39
2165/165/165 165:165:85
since 1970 = 2028820689s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39
2165/165/165 165:165:85
since 1970 = 2028820689s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39
2165/165/165 165:165:85
since 1970 = 2028820689s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39
2165/165/165 165:165:85
since 1970 = 2028820689s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39
2165/165/165 165:165:85
since 1970 = 2028820689s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39
2165/165/165 165:165:85
since 1970 = 2028820689s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39
2165/165/165 165:165:15
since 1970 = 2028820619s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39
2012/10/14 10:50:10
since 1970 = 1350211810s = 15627d
now + 7d + 30s: 2012/10/21 10:50:40
2012/10/14 10:50:11
since 1970 = 1350211811s = 15627d
now + 7d + 30s: 2012/10/21 10:50:41
2012/10/14 10:50:12
since 1970 = 1350211812s = 15627d
now + 7d + 30s: 2012/10/21 10:50:42
Originally the code had a delay of 3000 ms, could this perhaps be the problem? Thanks ahead of time to those who read this.
Here's the actual code:
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
// Declarations
int led = 2;
float brightness = 0;
float fadeAmount = 0;
RTC_DS1307 RTC;
void setup () {
Serial.begin(57600);
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
pinMode(led, OUTPUT);
}
void loop () {
DateTime now = RTC.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print(" since 1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days and 30 seconds into the future
DateTime future (now.unixtime() + 7 * 86400L + 30);
Serial.print(" now + 7d + 30s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();
if (future.hour() == 11 && future.minute() == 35)
{
fadeAmount = .14167;
}
else if (future.hour() == 12 && future.minute() == 5)
{
fadeAmount = 0;
}
else if (future.hour() == 21 && future.minute() == 0)
{
fadeAmount = -.14167;
}
else if (future.hour() == 21 && future.minute() == 30)
{
fadeAmount = 0;
}
analogWrite(led, brightness);
brightness = brightness + fadeAmount;
delay(1000);
}