Arduino time questions

Hi,

I have a few questions about the Time and TimeAlarms library’s.

If I use the setSyncProvider(RTC.get) function in setup, does it sync to the RTC every five mins, or do I have to sync it in the loop?

How would I write a one hour delay using the Alarm.delay() function? 3600000 doesn’t seem rite. And would an hour delay effect the syncing with the RTC?

Hope someone can enlighten me

If I use the setSyncProvider(RTC.get) function in setup, does it sync to the RTC every five mins

It syncs automatically, at whatever interval you set.

How would I write a one hour delay using the Alarm.delay() function?

Why would you? If you have alarms, etc., you don't need to delay. If you have an RTC, you don't NEED the time alarms library.

If I use the setSyncProvider(RTC.get) function in setup, does it sync to the RTC every five mins, or do I have to sync it in the loop?

The library will automatically synch, after the set or default interval, the system clock to the rtc in the background anytime you call now(), to access the current time, at different places in loop().

How would I write a one hour delay using the Alarm.delay() function? 3600000 doesn't seem rite. And would an hour delay effect the syncing with the RTC?

alarm.delay() takes an unsigned long argument so 3600000 and should indeed give a one hour delay. Why you want long delays in your program is another issue.

Unlike the regular delay(), alarm.delay() does not just twiddle its thumbs, it actually goes out and services the alarms. That's why it needs to be used. During the alarm servicing, the library actually calls now(), so the time should be synced with the rtc.

void TimeAlarmsClass::delay(unsigned long ms)
    {
      unsigned long start = millis();
      while( millis() - start  <= ms)
        serviceAlarms();
    }
void TimeAlarmsClass::serviceAlarms()
    {
      if(! isServicing)
      {
        isServicing = true;
        for( servicedAlarmId = 0; servicedAlarmId < dtNBR_ALARMS; servicedAlarmId++)
        {
          if( Alarm[servicedAlarmId].Mode.isEnabled && (now() >= Alarm[servicedAlarmId].nextTrigger)  )
          {
            OnTick_t TickHandler = Alarm[servicedAlarmId].onTickHandler;
            if(Alarm[servicedAlarmId].Mode.isOneShot)
               free(servicedAlarmId);  // free the ID if mode is OnShot 
            else   
               Alarm[servicedAlarmId].updateNextTrigger();
            if( TickHandler != NULL) {        
              (*TickHandler)();     // call the handler  
            }
          }
        }
        isServicing = false;
      }
    }

Hope someone can enlighten me

You will greatly increase the ability to enlighten yourself and deepen your understanding of the Arduino environment if you get in the habit of looking at the library source code when you have questions.

Seedler:
Hi,

I have a few questions about the Time and TimeAlarms library’s.

If I use the setSyncProvider(RTC.get) function in setup, does it sync to the RTC every five mins, or do I have to sync it in the loop?

How would I write a one hour delay using the Alarm.delay() function? 3600000 doesn’t seem rite. And would an hour delay effect the syncing with the RTC?

Hope someone can enlighten me

What is it you are trying to accomplish? What is the main goal of your project?

Why do you want a one-hour delay?

Thanks.

My program just needs to turn things on and off an hour later. I could use an off alarm but it would be simpler just using alarm.delay().

As long as putting setSyncProvider(RTC.get) in setup is all that is needed to keep the Arduino synced with RTC I’m happy.

Thanks

Seedler:
Thanks.

My program just needs to turn things on and off an hour later. I could use an off alarm but it would be simpler just using alarm.delay().

As long as putting setSyncProvider(RTC.get) in setup is all that is needed to keep the Arduino synced with RTC I’m happy.

Thanks

Alarm.delay() does not affect timekeeping with regard to Time.h or TimeAlarms.h. It is in the library to replace delay(), which would affect the alarms library.
It does block the rest of your code from executing for the period of the delay. It does not conserve power, nor does it have any real benefit.
It is better, if only for practice, to form the habit of one alarm to turn it on, and one to turn it off.