Skip to main content
Arduino

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Use of "static" with "RTClib"

I've got a question concerning the Arduino library "RTClib" by Adafruit and the use of the word "static".

Here you can see an excerpt from the example provided for the pcf8523 real-time clock:

#include "RTClib.h"
RTC_PCF8523 rtc;
void setup () {
 while (!Serial) {
 delay(1); // for Leonardo/Micro/Zero
 }
 Serial.begin(57600);
 if (! rtc.begin()) {
 Serial.println("Couldn't find RTC");
 while (1);
 }
 if (! rtc.initialized()) {
 Serial.println("RTC is NOT running!");
 }
}
void loop () {
 DateTime now = rtc.now();
 Serial.print(now.year(), DEC);
 /*
 do some more stuff
 */
}

In the beginning of loop() they wrote:

DateTime now = rtc.now();

Thus, with every iteration of loop() "new" gets defined anew (on the heap?!), right? This appears inefficient to me.

I thought about rewriting it this way:

static DateTime now; // static declaration, executed only once
now = rtc.now(); // assignment to "now" every time loop() starts over

In this case "new" should be allocated on the stack like a global variable, right?

I want to use "static" to increase overall performance by reducing work for the processor and the heap (thereby avoiding heap fragmentation). Does this make sense or will it cause more problems than it solves? Keep in mind that I want to learn both proper programming for Arduino and proper coding in general.

Link to the library: https://github.com/adafruit/RTClib

Thank you! :-)

EDIT: For some reason I CAN NOT declare DateTime now; in global scope or else the program won't run. Don't know why though. This is why I want to use static in the first place.

Answer*

Draft saved
Draft discarded
Cancel
5
  • 1
    Forgot to mention: Defining it outside of loop() in the global scope DOES NOT WORK! Don't ask me why, I spent hours just to find this problem. This is the reason why I wanted to use static in the first place. Will edit my post. And concerning the name "now": It's just what they used in the example, I won't use it, but good point! Commented Apr 22, 2020 at 19:54
  • I updated my answer, just try if it helps. Commented Apr 22, 2020 at 20:10
  • 1
    Re "I suspect, the constructor calls a function that uses variables or a function that is not initialized at that point": I can't see anything wrong in this constructor. Commented Apr 22, 2020 at 20:34
  • @EdgarBonet ... me neither, I'm wondering why creating it as global variable wouldn't work. Commented Apr 22, 2020 at 20:36
  • See the comments above beneath @EdgarBonet 's solution. It compiles and uploads but doesn't execute. Commented Apr 23, 2020 at 17:16

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /