Can someone post how to do the same with the sprintf function?
I`m curious to see how much memory (if any) using the sprintf saves, instead of doing several serial.prints.
I agree, it's better to print the parts of the date independently, rather than use precious RAM up by declaring a buffer for sprintf to write to. RAM typically runs out before program memory on the atmega328. The only thing I would do differently from your example is to write single characters using Serial.write, e.g. Serial.write('/') instead of Serial.print("/"), and when writing string that are more than 1 character long, use flash strings, e.g. Serial.print(F("a string")).
If you really want to use sprintf, use something like:
dc42:
I agree, it's better to print the parts of the date independently, rather than use precious RAM up by declaring a buffer for sprintf to write to. RAM typically runs out before program memory on the atmega328. The only thing I would do differently from your example is to write single characters using Serial.write, e.g. Serial.write('/') instead of Serial.print("/"), and when writing string that are more than 1 character long, use flash strings, e.g. Serial.print(F("a string")).
If you really want to use sprintf, use something like:
where buffer is a char array that is at least 13 characters long.
Thanks for that, I need to check with some more demo code that the Serial.print(F("string") works when the the time is updated, but using serial.write for single chars is a good idea.
Eventually the Date and time will be stored as arrays of strings (or chars) for later retrieval using PROGMEM.
I need to look into the best way of doing that (more demo code!)
Lakes:
Can someone post how to do the same with the sprintf function?
I`m curious to see how much memory (if any) using the sprintf saves, instead of doing several serial.prints.
Thanks.
If you're worried about using up RAM, you've got to see THIS library: Flash | Arduiniana
I recently wrote a 4 channel solenoid valve controller program that has an extensive user menu (LOTS of text). I ran out of memory in a "flash" (pun intended).
By using the Flash library, most of my SRAM is now unused and available.
Here's a piece of the code to show how the Flash library is used:
FLASH_STRING(menu,
"\r\n"
" Four Valve Independent Controller - Main Menu\r\n"
"\r\n"
" Please select an option\r\n"
"\r\n"
" (1) Run controller\r\n\r\n"
" (2) Configure valve timing\r\n\r\n"
" (3) Review all valve timings\r\n\r\n"
" (4) Change serial baud rate\r\n\r\n"
" (5) Display free memory (debug)\r\n\r\n"
" (6) View license information\r\n\r\n"
" Option: "
);
menu.print(Serial);
Arduino 1.0 and later provide the F() macro, and F() strings are supported by all the standard print() functions. So you don't need the flash library to put simple string literals in PROGMEM. It may still be useful for more complicated data types.