Chapstone
New Member
Im having difficulty getting a Chronodot to properly set/read the year. Im using a Arduino Uno + Micro along with a Arduino Chronodot + Sparkfun DS1307, either combination gives me the same problem. Everything in the time/date stamp seems to work correctly (seconds, minutes, etc.) but the year comes back as a seemingly random number. Although it seems to return 62 alot. Clearly its not 1962 or 2062. Unfortunately for my project i need the year to be correct so i can get a correct weekday from the arduino. I've posted all of the code below as i wasnt sure which sections would be relevant.
Thanks for any thoughts or suggestions.
Derek
**Note** Please use code tags...
Thanks for any thoughts or suggestions.
Derek
C:
/*Sets RTC to time given through serial monitor
Derek Chapman 6-21-14
*/
#include <DS1307RTC.h>
#include <Time.h>
#include <Wire.h>
int incoming = 99;
int RTCaddress = 0x68;
void setup()
{
Wire.begin();
Serial.begin(57600);
}
void loop()
{
printmenu();
while(Serial.available() < 1) ;// wait for Arduino Serial Monitor
incoming = Serial.parseInt();
if(incoming == 1)
{setRTCfromSerial();}
else if(incoming == 2)
{
for(int i = 1; i < 6; i++)
{getRTCtime(); delay(1000);}
}
else if(incoming == 3)
{getRTCdate();}
}
void printmenu()
{
Serial.println("\n---What would you like to do?");
Serial.println("1. Update time on RTC");
Serial.println("2. Check time on RTC");
Serial.println("3. Check date on RTC");
}
void setRTCfromSerial() //gets time data from serial input then sets the RTC
{
Wire.beginTransmission(RTCaddress);
Wire.write(0); //start at address # 0, seconds
Wire.endTransmission();
tmElements_t tm;
Serial.println("\n---RTC Time Set---");
Serial.print("Enter military hours: ");
while(Serial.available() < 1) ;// wait for Arduino Serial Monitor
tm.Hour = Serial.parseInt();
Serial.println(tm.Hour);
Serial.print("Enter minutes: ");
while(Serial.available() < 1);
tm.Minute = Serial.parseInt();
Serial.println(tm.Minute);
Serial.print("Enter seconds: ");
while(Serial.available() < 1);
tm.Second = Serial.parseInt();
Serial.println(tm.Second);
Serial.print("Enter month: ");
while(Serial.available() < 1);
tm.Month = Serial.parseInt();
Serial.println(tm.Month);
Serial.print("Enter date: ");
while(Serial.available() < 1);
tm.Day = Serial.parseInt();
Serial.println(tm.Day);
Serial.print("Enter two digit year: ");
while(Serial.available() < 1);
tm.Year = Serial.parseInt();
Serial.println(tm.Year);
Serial.print("Fire it up?");
while(Serial.available() < 1);
Serial.read();
if(RTC.write(tm))
{Serial.println("The following time was successfully written to the RTC.");}
else
{Serial.println("Robot Error! Setting the RTC time failed.");}
Serial.println();
sendTimetoSerial(tm);
}
void getRTCtime()
{
tmElements_t temptm;
if(RTC.read(temptm))
{sendTimetoSerial(temptm);}
else
{Serial.println("\nRobot error! Reading RTC failed!");}
}
void sendTimetoSerial(tmElements_t tmi) //takes a given time and prints to serial monitor
{
Serial.print("Current time according to sent time is: ");
Serial.print(tmi.Hour, DEC); Serial.print(":");
if(tmi.Minute < 10)
{Serial.print("0");}
Serial.print(tmi.Minute, DEC); Serial.print(":");
if(tmi.Second < 10)
{Serial.print("0");}
Serial.println(tmi.Second, DEC);
}
void getRTCdate()
{
tmElements_t temptm;
if(RTC.read(temptm))
{
Serial.println("Current time according to RTC is: ");
Serial.print(temptm.Month, DEC); Serial.print("/");
Serial.print(temptm.Day, DEC); Serial.print("/");
Serial.println(temptm.Year, DEC);
}
else
{Serial.println("\nRobot error! Reading RTC failed!");}
}
void sendDatetoSerial(tmElements_t tmi)
{
Serial.print("Current date according to sent time is: ");
Serial.print(tmi.Month, DEC); Serial.print("/"); Serial.print(tmi.Day, DEC); Serial.print("/"); Serial.println(tmi.Year, DEC);
}
Last edited by a moderator: