Time Library and RTC Library: Use of DateTime now = RTC.now

I am using an Arduino Uno.
**Questions: **

  1. Does having DateTime now = RTC.now in the loop create a now object every loop?
  2. Is DateTime now = RTC.now in the loop reading the RTC each loop?
    3 . If so, is there a way to not create this object every loop?

I want to use the Time Library and not update to the RTC every loop.
Use setSyncProvider and setSyncInterval to update to RTC every 5 seconds.

When I do move DateTime now = RTC.now out of the loop I get error:
"request for member 'hour' in 'now', which is of non-class type 'time_t() {aka long unsigned int()}'
Serial.print(now.hour());"

Purpose of code below is to better understand functions and variables of the Time Library and RTCLib.

In setup():
Set RTC to 5:10:30
Set Time Library to 9:27:05
Every 5 seconds Time Library gets sync'd to RTC
Evert 7 seconds Time Library gets set back to 9:27:05

#include "RTClib.h"#include "RTClib.h"
#include <Wire.h>
#include <TimeLib.h>

RTC_DS1307 RTC;
 
unsigned long LastUnSyncTime = 0;

time_t time_provider()
{
    return RTC.now().unixtime();  
}


void setup() {
Serial.begin(9600);
Wire.begin();  //sets up the I2C
RTC.begin();   //initializes the I2C to the RTC

//  Set the RTC Time to 5:10:30 Nov 3 2020
RTC.adjust(DateTime(2020,11,3,5,10,30));

setSyncProvider(time_provider);  //sets Time Library to RTC time
setSyncInterval(5);            //sync Time Library to RTC every 5 seconds

//Set Arduino Time Library different than RTC time 9:27:05 so see how sync works
setTime(9, 27, 05, 4, 07, 2015);

  if(timeStatus()!= timeSet) 
     Serial.println("Unable to sync with the RTC");
   else
     Serial.println("RTC has set the system time");
}


void loop() {

if ((millis() - LastUnSyncTime) > 7000)
  {
  Serial.println("-----------------------------");
  setTime(9, 27, 05, 4, 07, 2015);
  LastUnSyncTime = millis();
   }
 
DateTime now = RTC.now();  //  Creating now object every loop?

//Print Time Lib Times
Serial.print("hour:       ");
Serial.print(hour());
Serial.println();
Serial.print("minute:     ");
Serial.print(minute());
Serial.println();
Serial.print("seconds:    ");
Serial.print(second());
Serial.println();
Serial.println();  


//Print RTC time
Serial.print("now.hour:   ");
Serial.print(now.hour());
Serial.println();
Serial.print("now.minute: ");
Serial.print(now.minute());
Serial.println();
Serial.print("now.second: ");
Serial.print(now.second());
Serial.println();
Serial.println();
Serial.println();

delay(1000);
}

RichKC24:
I am using an Arduino Uno.
**Questions: **

  1. Does having DateTime now = RTC.now in the loop create a now object every loop?

Yes, but it disappears when loop ends so it's not eating through your memory.

  1. Is DateTime now = RTC.now in the loop reading the RTC each loop?

Yes.

3 . If so, is there a way to not create this object every loop?

Yes, make it global if you want. Just do the assignment in loop. There's no benefit to doing so though since all it does as you have it now is occupy some stack space.

wildbill - thanks! That did work. The reason I want to not create the now object each loop is because I have been getting random reboot and odd behavior after running a while. The program I am running is fairly large and this article stack and heap crash talks about the heap crashing into the stack. I do not know anything about these things, but it does say:

Dynamic Allocations
Dynamicaly allocated objects and data cause the heap to grow toward the stack. Unlike Global and Static variables, these variables can be de-allocated to free up space. But this does not necessarily cause the heap to shrink! If there is other dynamic data above it in the heap, the top of the heap will not move. When the heap is full of holes like swiss cheese we call it a "fragmented heap".

So, as. you suggested, modified code as shown below. Had to put DateTime now = RTC.now() under setup(), when I tried to make it Global above setup() got error: 'DateTime now' redeclared as different kind of symbol.

#include "RTClib.h"
#include <Wire.h>
#include <TimeLib.h>

RTC_DS1307 RTC;
 
unsigned long LastUnSyncTime = 0;

time_t time_provider()
{
    return RTC.now().unixtime();  
}

void setup() {
Serial.begin(9600);
Wire.begin();  //sets up the I2C
RTC.begin();   //initializes the I2C to the RTC

//  Set the RTC Time to 5:10:30 Nov 3 2020
RTC.adjust(DateTime(2020,11,3,5,10,30));

setSyncProvider(time_provider);  //sets Time Library to RTC time
setSyncInterval(5);            //sync Time Library to RTC every 5 seconds

//Set Arduino Time Library different than RTC time 9:27:05 so see how sync works
setTime(9, 27, 05, 4, 07, 2015);

  if(timeStatus()!= timeSet) 
     Serial.println("Unable to sync with the RTC");
   else
     Serial.println("RTC has set the system time");

     DateTime now = RTC.now();  //moved this to setup()
}


void loop() {

if ((millis() - LastUnSyncTime) > 7000)
  {
  Serial.println("-----------------------------");
  setTime(9, 27, 05, 4, 07, 2015);
  LastUnSyncTime = millis();
   }
 
//Print Time Lib Times
Serial.print("hour:       ");
Serial.print(hour());
Serial.println();
Serial.print("minute:     ");
Serial.print(minute());
Serial.println();
Serial.print("seconds:    ");
Serial.print(second());
Serial.println();
Serial.println();  

delay(1000);
}

Output: Syncs to RTC every 5 seconds and resets Time Library every 7 seconds.

RTC has set the system time
hour: 9
minute: 27
seconds: 5

RTC has set the system time
hour: 9
minute: 27
seconds: 5

hour: 9
minute: 27
seconds: 6

hour: 9
minute: 27
seconds: 7

hour: 9
minute: 27
seconds: 8

hour: 9
minute: 27
seconds: 9

hour: 5
minute: 10
seconds: 35

hour: 5
minute: 10
seconds: 36


hour: 9
minute: 27
seconds: 5

hour: 9
minute: 27
seconds: 6

hour: 9
minute: 27
seconds: 7

hour: 9
minute: 27
seconds: 8

hour: 9
minute: 27
seconds: 9

hour: 5
minute: 10
seconds: 42

hour: 5
minute: 10
seconds: 43


hour: 9
minute: 27
seconds: 5

hour: 9
minute: 27
seconds: 6

the only place to read the RTC should be the sync function. everywhere else you want to use Time library functions.
reading the RTC is I2C communication

Are you using String objects in your larger program?

Juraj - Yes that is a mistake I have corrected. I was constantly syncing to the RTC every loop. I am now only reading the RTC every few minutes with setSyncProvider and setSyncInterval in setup(). I hope this was the problem. An intermittent issue, so hard to tell quickly.

wildbill - No I am not using strings. Read a lot about how they eat up memory.