Hey, looks like you got a lot of help.
Now, if we continue the line of thought and practice from before, you can again use chatGPC and tell it this:
c++ embedded, create a function of this code and show Brian how to call it, using c strings
the response:
To refactor the code into a C++ function and demonstrate how to use C-style strings (char
arrays) rather than String
objects, here's how you can create a function and call it.
1. Function Creation
We'll create a function formatDateTime
that takes two output parameters for the time and date strings. We'll use C-style strings for both inputs and outputs.
#include <Particle.h>
void formatDateTime(char* timeNow, size_t timeSize, char* dateNow, size_t dateSize) {
char timeStr[64]; // Buffer to hold the formatted time
// Format the current time into the buffer using Time.format()
snprintf(timeStr, sizeof(timeStr), "%s", Time.format(Time.now(), "%m/%d %H:%M:%S").c_str());
// Extract the time (substring from index 6 onward)
snprintf(timeNow, timeSize, "%s", timeStr + 6); // Extract the time (HH:MM:SS)
// Extract the date (first 5 characters for MM/DD)
snprintf(dateNow, dateSize, "%.*s", 5, timeStr); // Extract the date (MM/DD)
}
2. How to Call the Function
Here’s how Brian can call this function using C strings:
cpp
Copy code
void setup() {
Serial.begin(9600);
char timeNow[16]; // Buffer to hold the extracted time
char dateNow[16]; // Buffer to hold the extracted date
// Call the function
formatDateTime(timeNow, sizeof(timeNow), dateNow, sizeof(dateNow));
// Display the results
Serial.println("Time: ");
Serial.println(timeNow); // Outputs the time part (HH:MM:SS)
Serial.println("Date: ");
Serial.println(dateNow); // Outputs the date part (MM/DD)
}
void loop() {
// Code to run in loop (if any)
}
Explanation
- Function Parameters:
char* timeNow
and char* dateNow
are the output buffers where the formatted time and date will be stored.
snprintf
: This function is used to format strings into C-style string buffers. We extract the time portion by slicing the string from the 6th character onward and the date portion by selecting the first 5 characters.
- Calling the Function: You create buffers (
timeNow
and dateNow
), pass them to the function, and print the results.
This approach keeps memory usage more efficient for embedded systems by using C strings instead of String
objects.
---- end of response.
So, you can even copy-paste your whole firmware and tell it what you need with it, or simply explain parts of it (explain this code is will be what you would write).
Anyhow, feel free to experiment with it, do not be shy, I believe it's gotten very good and can help you understand the little details you might be shy to ask here. I'm telling you all this not because I do it - of course 
Otherwise, keep asking here! we all learn and have fun in the process.
Best,