Trouble with RTC time

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
tfunk10258
 
Posts: 5
Joined: Mon Mar 05, 2012 2:36 am

Trouble with RTC time

Post by tfunk10258 »

I'm having an interesting little problem integrating a chronodot into a project I have, and I'm now looking for a little assistance. I have the time and date set correctly, and when I run the DS1307 example sketch, everything prints out nicely and works just fine. I then added some modified code into my existing project to print the unix time at the end of each line of a csv logfile it creates. However, the unix time it logs only updates every 128 seconds, so it prints the same value 128 times (the project logs data once per second) and then switches to a value 128 seconds higher and so on.

Then, to troubleshoot, I ran the RTC code I wrote on its own, using everything hooked up identically, and printed it to the serial instead of a logfile, and it only updates every 64 seconds.... I'm stumped. Here is the code I ran on its own.

Code: Select all

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 RTC;

float time;

void setup () {
    Serial.begin(57600);
    Wire.begin();
    RTC.begin();
}

void gettime(void){
  DateTime now = RTC.now();
  time = now.unixtime();
}

void loop () {
  gettime();
  Serial.println(time);
  delay(1000);
}
Any help would be appreciated.

Thanks,

Thomas

odometer
 
Posts: 98
Joined: Sun Aug 21, 2011 11:01 pm

Re: Trouble with RTC time

Post by odometer »

Personally, I do not like the "float" datatype. One reason I don't like it is that it has so little precision. This lack of precision is what is causing you trouble here.

One thing you should know about this RTC library is that it ignores the existence of time zones and therefore does not handle Unix Time correctly.

Info on Unix Time here: http://www.epochconverter.com

tfunk10258
 
Posts: 5
Joined: Mon Mar 05, 2012 2:36 am

Re: Trouble with RTC time

Post by tfunk10258 »

Thanks for your reply. The time zone is unimportant in my application. Are you saying that the value is jumping by powers of 2 because of a lack of resolution in float datatypes? I never thought of that

User avatar
adafruit_support_bill
 
Posts: 88089
Joined: Sat Feb 07, 2009 10:11 am

Re: Trouble with RTC time

Post by adafruit_support_bill »

Floating point gives you the ability to represent a much larger range of numbers than is possible with integers. The tradeoff is the loss of 100% accuracy.

http://www.cprogramming.com/tutorial/fl ... point.html

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”