Ways to make things happen with a RTC?

Hi all,

I'm pondering how best to make things happen based on time from an RTC.

The DS1302 library that I use (Henning Karlsen's) has a Time structure and getTime() function. I can compare the got time with a target time and do something as in this pseudo code. Use a flag to check we haven't done it yet (else it'll repeat the action every pass through loop while the time is correct).

// PSEUDO CODE
DS1302 myRTC();
Time t;

loop()
{
t =myRTCgetTime();

if myRTC.hour == target hour &&   stuff not done    
    { 
            //do stuff... 
            //set the stuff is done flag
            //    or, perhaps increment target hour so it does this at the top of every hour
   }

}

Is this a sensible approach? Are there libraries that allow one to use RTC time in a sort of alarm thing?

Use your RTC in-conjunction with the time and timealarm libraries. http://playground.arduino.cc/Code/Time

An often used technique is to convert the year/day/hour ..etc to a uint32_t seconds since a certain date Two often used "epochs" are . 1-1-2000 (NTP) or 1-1-1970 (UNIX)

Thanks Gents

You have a reasonable approach. Also if you only doing events on a granularity of minutes or hours you can save slowing down loop (by accessing the I2C bus all the time) thus:

unsigned long check_time = 0L ;

void loop ()
{
  if (millis () - check_time > 10000)  // only check RTC every 10 seconds
  {
    check_time += 10000L ;
    check_rtc () ;
  }
  ...
}

void check_rtc ()
{
  t =myRTC.getTime();   // talk I2C and access times
  if (myRTC.hour == target_hour && !done)
    { 
        do_stuff () ;
        done = true ;
    }
  if (myRTC.hour == target_hour+1)   // not quite correct!
    done = false ; // prime for the following day
}