8

I've run into an issue trying to combine 2 different Arduino timer projects I've found online.

For both projects I'm using a DS3231 RTC, but have been able use the DS1307 library just fine in my code.

The first set of code I picked up started with:

#include <DS1307RTC.h>

The second set of code I tried to combine did not have this 'include', but instead used

RTC_DS1307 RTC;

as found here:

https://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit/understanding-the-code

As a novice coder, I'm not really sure what the difference between the above two ways to call this library. But I do know the first way using "include", if I want to access the current time on the RTC I use

RTC.get();

as opposed to the second method which requires:

RTC.now();

And while it was easy to find documentation to sync the RTC to computer time over the USB cable using this handy bit of code:

// Notify if the RTC isn't running
    if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running");
  }



   // Get time from RTC
  DateTime current = RTC.get();
  DateTime compiled = DateTime(__DATE__, __TIME__);
  if (current.unixtime() < compiled.unixtime()) { //CHECKS AGAINST COMPUTERTIME
    Serial.println("RTC is older than compile time! Updating");
    RTC.adjust(DateTime(__DATE__, __TIME__));  //UPDATES FROM COMPUTER IF OLD TIME IS OFF
  } 

I can find no easy alternative using "#include DS1307RTC"

I first thought that the difference between RTC.now() and RTC.get() was just a matter of word substitution. But later on when I want to use a "time_t" call after initializing with "RTC_DS1307 RTC;"

 time_t timeNOW = RTC.now();

I get the error "cannot convert 'DateTime' to 'time_t {aka long unsigned int}' in initialization"

So I'm assuming one method uses "DateTime" and one method uses "time_t". But I'm not sure if this is the case, and if so, how to convert a "DateTime" value to a "time_t" value.

I have two goals in writing this post.

1- Can someone please explain to me the difference between "#include DS1307RTC.h" and "RTC_DS1307 RTC;" and how it impacts my code.

2- I'd like to stick with using ""RTC_DS1307 RTC;", only because it seems easy to sync my RTC to my computer. But if I do, I get the above error. So can someone please tell me how to convert a "RTC now()" call to a "time_t" value? Or is the issue more complicated than just a mere conversion between data types?

1 Answer 1

12

A DateTime is a full class with lots of methods to it - a time_t is just an unsigned long.

time_t is used to store the number of seconds since the epoch (normally 01/01/1970)

The Arduino Time library returns a time_t to the now() function - but RTCLib return s a DateTime object.

The DateTime object, though, has a unixtime() method which will return a time_t representation of the time stored in the DateTime object.

So you can do:

DateTime dt = RTC.now();
time_t time = dt.unixtime();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.