Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

RTC Year not working correctly

Status
Not open for further replies.

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

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);
}
**Note** Please use code tags...
 
Last edited by a moderator:
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);

I dont know...but for YEAR you have Serial.printin and for the other 2 you just have Serial.print
 
Thanks guys. I figured it out this afternoon after much gnashing of teeth. In case anyone else runs into a similar problem:

The problem turned out to be a code issue. The arduino seems to use variable time_t to store/read/send the time. I was using the type t_elements. It is a much easier way to send/store/read the time. Unfortunately they store everything the same except the year. t_elements stores it as the number of years since 1970 wheres as time_t stores it as a two digit year with the 20 assumed. Small but crucial difference.

Thanks for the thoughts though, much appreciated.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top