RTC timer help

Hello,

i am learning how to use RTC for different time functions (DS1307, DS3231). I want to make on/off delay timer based on RTC but i don't know how.

I want to have 5 seconds delay before turn led on or off, after switch is pressed. I took code from previous project, based on millis() and change it so i can use RTC seconds but after rollover, it stops working. I understand why but i don't know how to fix it.

I can't find any good tutorials to learn how to use different RTC time functions.

Current code idea..

delayTime = 5;  // 5 seconds

    if ((switch == HIGH) && (now.second() - onTime >= delayTime))
  {
    digitalWrite(led, HIGH);
    offTime = now.second();    
  }
    else if ((switch == LOW) && (now.second() - offTime >= delayTime))
  {
    digitalWrite(led, LOW);
    onTime  = now.second();
  }

I also tried to play a little with futureTime function, which i found but i didn't know how to use it. I would really appreciate any help or turorial link about handling RTC functions.

You want to use an RTC instead of millis ( ) because you cannot get a handle on millis ( ) ?

CrossRoads made a pretty neat millis( ) based clock.
I was playing around with it and reformatted it for 24h display.

(sketch attached)

CrossRoads_clock24.pde (1.19 KB)

I once wrote a sketch to blink a led using a DS1307 rtc. You can find it here: Blink without delay and millis but with a DS1307. - Bajdi electronics Don't just copy/paste it. Please try and figure out how it works.

No, i need RTC for other functions + i also want to learn how it works for other projects. I need to know exact date and accurate time. Thank you for sketch!

Bajdi:
I once wrote a sketch to blink a led using a DS1307 rtc. You can find it here: Blink without delay and millis but with a DS1307. - Bajdi electronics Don't just copy/paste it. Please try and figure out how it works.

Thank you, i will check it. From where did you learn about handling with RTC? Any special tutorial or page recommendation?

If you google Arduino + DS1307 you get over 100000 results.

I had in mind some "timer, based on RTC" examples or something like that (like your blink with DS1307 tutorial), because i didn't found it yet and most of searching results are about how to connect it and set date and time.. Will try searching more.

Btw, is ok to use time functions, based on both, millis() and RTC or is better to use only RTC for all time operations in that case (when RTC is already integrated)? ..or it doesn't really matter?

Ok, i think i found something for me..

Quote: "We can also get a 'timestamp' out of the DateTime object by calling unixtime which counts the number of seconds (not counting leapseconds) since midnight, January 1st 1970. This might be useful when you want to keep track of how much time has passed since the last query."

If i understand it correctly, unixtime() is just like millis(), just without rollover nearby, so i can realize my timer based on this.

However, if i assign unixtime() to unsigned long variable, rollover will occurs but not in my lifetime :slight_smile: If range of unsigned long is from 0 to 4,294,967,295 and unixtime now is 1,371,379,635 seconds.. That means that rollover will appear after approximeatly 92 years.

luxy:
Btw, is ok to use time functions, based on both, millis() and RTC or is better to use only RTC for all time operations in that case (when RTC is already integrated)? ..or it doesn't really matter?

It depends on your requirements. In my mind the main difference between an RTC and millis (actually one of the build-in timers of the microcontroller) is that the RTC keeps track of date & time even if it is not powered up (assuming of course that it is connected to a back-up battery). The internal timers (hence millis) get initialized to 0 after a reset/powerup. So I would use an RTC if I want to date/time stamp events in a log, write an alarm clock or something similar that should not loose the date/time setting even if external power is interrupted. If on the other hand I want to blink an LED at a specific interval but don't care exactly what the time is when the blink starts I would go with millis or an interrupt based on a build-in timer in the Arduino.

Christo:
It depends on your requirements. In my mind the main difference between an RTC and millis (actually one of the build-in timers of the microcontroller) is that the RTC keeps track of date & time even if it is not powered up (assuming of course that it is connected to a back-up battery). The internal timers (hence millis) get initialized to 0 after a reset/powerup. So I would use an RTC if I want to date/time stamp events in a log, write an alarm clock or something similar that should not loose the date/time setting even if external power is interrupted. If on the other hand I want to blink an LED at a specific interval but don't care exactly what the time is when the blink starts I would go with millis or an interrupt based on a build-in timer in the Arduino.

I understand that millis() get initialized to 0 after a reset/powerup. I was wondering that if i use millis() and RTC timer functions together.. if this could cause problems, since crystals are not completely aligned. Well, it was just a thought :slight_smile: