Go Down

Topic: Time and TimeAlarms Libraries – Ask here for help or suggestions (Read 78 times) previous topic - next topic

Nick Gammon


I am working with DS1307, and I noticed that if the module is not connected, the library return time value instead of reporting error. I think this should be corrected. I noticed this with weak battery in the DS1307 module, then I played with disconnecting module at all. I am programmer and am able to correct this bug myself, but I think better way is 1) not having modified versions of "standard" libraries and 2) fixing this would be good for entire community. Several ways of fixing is for example: Check device on the I2C bus, writing some "magic" bytes when setting time to the DS1307 memory and then checking them, and most paranoid, perhaps checking all bits in the time information which _should_ be 0, if they really _are_ 0.


Hi ladinek,

I would be happy to add this feature to the library after it is well tested with the various chips compatible  with DS1307 that the library currently works with.
I suggest you create a new thread for your DS1307 enhancements when you are ready to post some code, and add a link to that thread here.

Michael Margolis



Thanks a lot, I had the same problem. It seems to be proper solution.

Thanks again


One more question, I have noticed you declare member functions of the DS1307 class as _static_, I tried to add one private variable and had problems compiling. So I googled a lot, and static class member function is not the same as the static function in "classic" C. It has something to do with "this". In other libraries member function are not declared as static. Can you explain reasons for this?

Nick Gammon

Static class variables do not "belong" to instances of the class but the class itself. Typically you might use a static variable to count how many class members have been instantiated.


I tried to add one private variable and had problems compiling.

Perhaps show the code and the error.


In the class definition in DS1307RTC.h I added variable present:
Code: [Select]
static uint8_t dec2bcd(uint8_t num);
    static uint8_t bcd2dec(uint8_t num);
uint8_t present;

Then in constructor I try to access this variable:
Code: [Select]
  if (Wire.endTransmission() == 0) present = 1;
  else present = 0;

Attemting to compile, I get errors like this:
Code: [Select]
D:\arduino-1.0.5\libraries\DS1307RTC/DS1307RTC.cpp:35: undefined reference to `DS1307RTC::present'
I tried declare variable present as either static or not, same result.

Nick Gammon


I solved it. Problem is, that you cannot access non-static member variable from static function. What I tried to do is add member variable present, which is true or false depending of success in communication with module. And if you declare static member variable, you must alse _define_ it, so I added
Code: [Select]
uint8_t DS1307RTC::present = 0;
just before contructor of the RTC object at the end of ds1307rtc.cpp.


Here is my modification of the library. I added member flag present, and set its value to true (1) or false (0) depending on success or failure in communication with the hardware. I modified the library as little as possible, you can compare my modification with original version. I modified only the read access to hardware. The RTC.get() function returns 0 (i.e. 1.1.1970) if the hardware is not present. It even correctly set timeStatus, if you for instance connect the DS1307 and then disconnect it, time continues to run, but the variable is set to timeNeedsSync after sync interval expires.

Nick Gammon

I solved it. Problem is, that you cannot access non-static member variable from static function.

Of course. Non-static variables belong to each instance, but a static function belongs to the class (not an instance).


Dec 02, 2013, 10:37 pm Last Edit: Dec 04, 2013, 08:04 am by fpoto Reason: 1
I wrote two simple functions for getting the time from the Internet.

The first one is an NTP date function. Unlike the NTP functions I have seen out there, this one does not require allocating either a transmit or a receive buffer, neither statically nor on the stack. It does not require any ancillary library either.

The second one is an HTTP date function. This one builds on a simple concept: just make an invalid HTTP 1.1 request to a web server out there, and parse its answer looking for the Date: header. This function is very small too and has the advantage of requiring very little code and memory space if you already use the EthernetClient or WiFiClient class in your sketch.


Dec 12, 2013, 02:13 am Last Edit: Dec 12, 2013, 04:00 am by russo_mic Reason: 1
Hi everybody, I could use some help with TimeAlarms.

I'm building a clock that uses a GPS Shield to constantly reset the time through the setTime() function in the arduino main loop, like this:

 if (age < 500) {
       // set the Time to the latest GPS reading
       setTime(Hour, Minute, Second, Day, Month, Year);
       adjustTime(offset * SECS_PER_HOUR);

This is to ensure that GPS data isn't stale, and that the clock's time is always getting updated by gps.  the age is a function of the TinyGPS++ library.

This is unlike most examples that I've seen that uses setTime() once in the setup of the sketch.  I'm trying to set alarms through timeAlarms library and cannot get any alarms to trigger.  I've got a feeling it is because I'm using setTime() in my loop as opposed to setup.  

Could anybody shed some light on what is going on?  Thanks in advance.


This is to ensure that GPS data isn't stale,

Resetting what time the Arduino thinks it is will have no impact on the time that the GPS knows that it is, and will have no impact on the freshness (or staleness) of the GPS data.

I've got a feeling it is because I'm using setTime() in my loop as opposed to setup. 

Perhaps you should consider printing age. It looks to me like you are resetting the time FAR more often than you need to. If something does happen, and you get an old reading, you don't reset the time. And that makes no sense.

I think that the real problem is that you are trying to address the wrong issue.


Thanks for the reply.  However, I already thought of this, and did an experiment and had it check only every 10 minutes, and still no alarms would trigger.   The only time my alarms trigger is if setTime() is in my setup. 

This is the way I made the clock portion of the code because in the GPS example in Time library this is how it is done.

Go Up