Format Date and Time using sprintf?

This is the demo code I have

#include <SoftwareSerial.h>

SoftwareSerial mySerial(7,8);   // 7 = Rx  8 = Tx


char timeBuffer[] = ("10041245");

void setup()
{
  Serial.begin(57600);
  Serial.println("Serial Connected @ 57600");

  mySerial.begin(1200);
}

void loop()
{
  Serial.print("");
  Serial.print("timeBuffer printed output is  ");  
  Serial.println(timeBuffer);
  Serial.println("");

  char DateTimeBuffer[] = ("00-00 00:00"); 
  //       **  Format Time and Date String **

  char Month [3];
  char Day[3];
  char Hour[3];
  char Min[3];
  for (  int x= 0; x < 2 ; x++)
  {
    Month[x] = timeBuffer[x];
    DateTimeBuffer[x] = Month[x];
    Month[2] = 0;                         //Null char
    Day[x] = timeBuffer[x+2];
    DateTimeBuffer[x+3] = Day[x];
    Day[2] = 0;
    Hour[x] = timeBuffer[x+4];
    DateTimeBuffer[x+6] = Hour[x];          
    Hour[2] = 0;
    Min[x] = timeBuffer[x+6];
    DateTimeBuffer[x+9] = Min[x];           
    Min[2] = 0;          
  }


  Serial.print(Day);
  Serial.print("/");  

  Serial.print(Month);
  Serial.print("  ");

  Serial.print(Hour);
  Serial.print(":");

  Serial.println(Min); 
  delay(20000);
}

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.

I`m curious to see how much memory (if any) using the sprintf saves, instead of doing several serial.prints.

One Serial.print() or a dozen Serial.print()s take the same amount of code space. The code is only copied in once.

Using sprintf() in addition will increase the code size, since another function needs to be linked in.

It will not decrease the time needed to output serial data, either.

The only advantage to using sprintf() is for formatting, which is hardly an advantage, since you are only dealing with char arrays.

ok, thanks Paul.

Something like this:

char str[12];
sprintf( str, "%s/%s %s:%s", Day, Month, Hour, Min );
Serial.print( str );

But as PaulS said, it won't be more efficient at all :)

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:

sprintf(buffer, "%02d/%02d %02d:%02d", Day, Month, Hour, Minute); Serial.print(buffer);

where buffer is a char array that is at least 13 characters long.

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:

sprintf(buffer, "%02d/%02d %02d:%02d", Day, Month, Hour, Minute); Serial.print(buffer);

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: http://arduiniana.org/libraries/flash

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);

SRAM used for this code: zero!

Hope this helps......

-- Roger

I`ll deffo check that out, thanks!

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.

Yup, using F()strings saved quite a few bytes!