Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

RTC Year not working correctly
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

RTC Year not working correctly

by chapstone on Fri Jun 27, 2014 3:43 am

Im having difficulty getting a Chronodot to properly set/read the year. 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. Thanks for any thoughts or suggestions. I've posted all of the code below as i wasnt sure which sections would be relevant.

Dereki

Code: Select all | TOGGLE FULL SIZE
/*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);
}
 
chapstone
 
Posts: 3
Joined: Mon Nov 04, 2013 9:49 pm

Re: RTC Year not working correctly

by adafruit_support_rick on Fri Jun 27, 2014 10:00 am

I don't know why the year isn't coming out right, but I don't think you want to do this:
Code: Select all | TOGGLE FULL SIZE
  while(Serial.available() < 1);

That loop will exit when there is one character in the serial buffer, and you're expecting at least two characters in many of the places where you do this test, including the year.

adafruit_support_rick
 
Posts: 35095
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: RTC Year not working correctly

by chapstone on Sat Jun 28, 2014 5:22 pm

Thanks for the reply Rick. I see what your saying on the Serial.available thing but so far doesn't seem to be a problem. I've ran the program alot and much of the time input 2 digit times/months/dates/etc. Ive tried inputting the year as 4 digit. It always seems to wait patiently until i hit return then does its thing. Any thoughts on the year problem? Ive googled the problem a lot the past few days without any luck.

Derek
chapstone
 
Posts: 3
Joined: Mon Nov 04, 2013 9:49 pm

Re: RTC Year not working correctly

by chapstone on Sun Jun 29, 2014 1:33 am

Never mind, figured it out. Heres what I figured out in case anyone else was having a similar problem with their RTC:

The Arduino gets time in a time_t type variable. I was using the tmElements_t type variable in my code because it makes snycing with the RTC a million times easier. Instead of having to read or send individual bytes in a specific order you can merely use RTC.write(tm) This also seems to negate the necessity of using wire.begintransmission(0x68) etc etc

time_t stores the year as just the year. tmElements_t stores the year as the number of years since 1970. Adding this into my code seemed to solve the problem:
tm.Year = Serial.parseInt() - 1970;

Once you set the Arduino from the RTC the Arduino figures out what it means and sets the year correctly.

Derek
chapstone
 
Posts: 3
Joined: Mon Nov 04, 2013 9:49 pm

Please be positive and constructive with your questions and comments.