Hey, all, I have a question, in two parts, about whether I should do this another way. I've been a computer guy for decades and have done network engineering (MCSE), programming (VB, VB.NET), built and sold computers, blah blah blah. I've never done IC programming which is so very different. I've spent hours trying things and reading these forums trying to achieve even basic functionality towards a goal.
I'm using Arduino Uno and Nano boards.
Where I am right now is I want to have an RTC module do its job and have the Arduino do functions based on time/date. The first step is just reading/writing the time and then displaying the time. The code below is my attempt to do that. It's working now, the code below compiles and operates as I expect (save for padding single digit numbers with a leading 0). However, the memory usage is at 19% for storage space and 25% for dynamic memory.
"Sketch uses 6084 bytes (19%) of program storage space. Maximum is 30720 bytes.
Global variables use 513 bytes (25%) of dynamic memory, leaving 1535 bytes for local variables. Maximum is 2048 bytes."
I suspect this is due to the use of the DS3232.h which, in turn, uses/relies on the TimeLib.h. As I don't have experience with "libraries", C++ (or any C), and limited memory situations, I need guidance, please. I suspect that the entirety of those libraries is loaded into "program storage".
I'm avoiding use of Strings as they seem to be frowned upon for efficiency/memory "leakage". In this code segment the goal is to read and display the time and do so using functions to try to keep the loop() routine as clean as possible for readability/debugging. As I add functionality to this sketch, the loop() will get hard to follow if I don't break code into subs/functions.
So, my uncertainties are: 1) should I attempt anything to reduce program storage usage (possibly editing libraries to remove functions/code I do not need for my project, if that's even possible), 2) should I be doing anything better to format the time information to make it more efficient? For example, I have no need or desire for the alarm functionality on the DS3231; I could remove all code from the DS3232.h file pertaining to the alarms. Hypothetically.
Thank you. Code below:
#include <DS3232RTC.h> // RTC DS3231 header/functions. This internally needs TimeLib.h
DS3232RTC myRTC; //constructor for the RTC class
void setup()
{
Serial.begin(9600);
myRTC.begin(); //initializes I2C bus
setSyncInterval(60); //synchronizes the Arduino time (TimeLib.h) with the RTC every xx *seconds*, not milliseconds
setSyncProvider(myRTC.get); // setSyncProvider from TimeLib.h to sync Arduino time library to the RTC
if(timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
}
void loop()
{
char DisplayTime[17]; //a string to hold the data we'll display. 16 chars for the display and 1 char for the null terminator \0
//digitalClockDisplay(); //need to replace this with code which will show time on the LCD
strcpy(DisplayTime,FormatTime(DisplayTime));
Serial.println(DisplayTime);
delay(1000); //remove this as this sketch progresses. internal variable milli counter will be checked
}
char * FormatTime(char DT)
{
// digital clock display of the time
char temp[5]; //max 4 chars plus null term
//strcpy(DT, "Hello");
itoa(hour(),temp,DEC);
strcpy(DT, temp);
strcat(DT,":");
itoa(minute(),temp,DEC);
strcat(DT, temp);
strcat(DT,":");
itoa(second(),temp,DEC);
strcat(DT, temp);
return DT;
}