I am trying to make an Arduino project based on the Sparkfun DS1307 RTC (real-time clock).
There are a few issues with how Arduino libraries don't support checking if the Arduino board has been powered down and check for that.
Then using that as a conditional as to when the program should fall back to the compiled time if the RTC isn't set to a time yet because of something else.
When I power down the clock, it will automatic fall back to the compiled time every single time, regardless of what is currently on the RTC. Clearly this is not something you would want.
Edited - This is my current code. Keep in mind there are a few more changes I could do like take Byte zero declare out of the loop so it is still rough.
#include <Wire.h>
#include <SparkFunDS1307RTC.h>
#define DS1307_ADDRESS 0x68
void setup() {
Wire.begin();
Serial.begin(9600);
rtc.begin();
if(false){
//Should check RTC somehow to determine if should fallback to compiled time here.
Serial.println("The DS1307 clock has lost power and reset to compiler's time");
rtc.autoTime();
}
}
void loop() {
if(digitalRead(7)==LOW)
printDate();
delay(1000);
}
byte bcdToDec(byte val){
return ((val/16*10) + (val%16));
}
void printDate(){
rtc.update();
Wire.beginTransmission(DS1307_ADDRESS);
byte zero = 0x00;
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> Sunday - Saturday
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());
//Print the date like 3/1/1/11 23:59:59
Serial.print(month);
Serial.print("/");
Serial.print(monthDay);
Serial.print("/");
Serial.print(year);
Serial.print(" ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);
}