Adjusting the time on DS3231 with encoder, TimeLib.h and DS1307RTC.h

Hello,

I'm working on a project mod of this great instructable: http://www.instructables.com/id/Kids-Kitchen-That-Says-BEEP/

The original project uses 2 buttons to set the timer - I've switched this out with an encoder. This work fine. Now I want to add the ability to adjust the time of the DS3231. For this I've added a new state: adjustTime, which is entered if the encoder button is held down. So my issue is that I do not understand the TimeLib and DS1307RTC libraries well enough to be able to adjust the time.

I'm using 3 tmElements_t objects (one for time two for timer):

#include <DS1307RTC.h>
#include <TimeLib.h>
#include <Wire.h>


// Create RTC time object
tmElements_t RTC_time;

// Create RTC timer objects to hold the timer
// prevTimes is used to determine whether the timer has changed
tmElements_t Timer, prevTimer;

The idea is to have a function that adds one minute to the current time for each encoder 'tick'. So something like this:

void adjustTimeEnc(int deltaTime){ // deltaTime is an integer from -4(negative 4) to 4(positive 4) (excl. 0) depending on the acceleration of the encoder
  
  adjustTime(60*deltaTime); 

}

I am in doubt about the following:

  1. The 'adjustTime' function above - should that somehow be pointed to the RTC_Time object? If so how do I do that? RTC_Time.adjustTime(), throws compile error: 'struct tmElements_t' has no member named 'adjustTime'. The documentation and examples for this function seems very limited.

  2. How do I make sure the RTC actually updates to the adjusted time? My understanding is that I should use: RTC.set(), however I'm not sure what the argument should be in this case?

I'm sorry if I've completely misunderstood how this works, but I must admit the TimeLib and DS1307RTC libraries are a bit difficult for me to understand. Let me know if you need any more info about my code or setup. And please let me know if I'm going a completely wrong direction. :slight_smile:

Thanks in advance
mathies

I think that your rtc adjust time function should look like this.

void adjustTimeEnc(int deltaTime){ // deltaTime is an integer from -4(negative 4) to 4(positive 4) (excl. 0) depending on the acceleration of the encoder
  
 unsigned long newTime = RTC.get() + deltaTime*60000L;
 RTC.set(newTime); 

}

So simple!

I knew I was missing some super obvious solution. It works like a charm, thanks!

Oh, except one thing:

The time_t returned by RTC.get() is epoch time ( seconds since Jan 1. 1970 ), so I'm only multiplying by 60 as opposed to 60000.. The final function looks like this:

void adjustTimeEnc(int deltaTime){ 
  unsigned long newTime = RTC.get() + deltaTime*60L;
  RTC.set(newTime); 
  ShowTimeOnDisplay();
}

The last call ShowTimeOnDisplay() makes sure the display updates every time the time is changed.

Any ways! Thanks again!

I'm only multiplying by 60 as opposed to 60000.

Yeah, sorry. I got confused. I don't know why I was thinking millis() instead of seconds. Not enough coffee before posting. :slight_smile:

With deltaTime*60 you don't need the specifier "L" for the constant. It won't overflow an integer.