Time Library

Hi Folks! First of all I am very new to Arduino and C/C++. I have a short question. I got the Time library from here http://arduino.cc/playground/Code/Time.

I want to make a Method(I am a Java Guy) which generates a new String where the current Time is stored and seperated with ":" with the 3 Functions hour(), minute() and second(). Which Data Type do I need? And is it possible to Seperate them with a ":" ?

Regards Luke

Which Data Type do I need?

For input or output?

which generates a new String

Pretty much defines what the output type should be.

with the 3 Functions hour(), minute() and second()

You should look at the documentation for the Time library to see what types these functions return. Hint: its int.

And is it possible to Seperate them with a ":" ?

Sure.

Be careful trying to return a String instance from a function, though, You are generally a lot better of passing the object to store the data in as a reference argument.

void makeTimeString(int hour, int minute, int second, String &timeString)
{
   timeString = String(hour) + ":" + String(minute) + ":" + String(second);
}

Why do you want the time as a String? Strings are not well suited to the Arduino. A char array would, in my opinion, be a much better choice.

void makeTimeString(int hour, int minute, int second, char *timeString)
{
   sprintf(timeString, "%2d:%02d:%02d", hour, minute, second);
}

void setup()
{
  char timeString[20];
  makeTimeString(9,5,7,timeString);
  Serial.begin(9600);
  Serial.println(timeString);
}

void loop()
{
}

Thank you so much! Works like a charm! The char array did not cross my mind. The next Question I have is , how can I actually get Time from the Function? I set the Time Once in the Setu Loop but how can I get the passed Time Since I set it up?

For Example.The Arduino is booting. I set the time to 12:10:11. An event Occurs at 12:12:11. Now I want to print this time to my LCD. I don't have a clue how to do this with the function you wrote. Can you give me any advice how to proceed with this problem?

Regards Luke

It would be helpful to see your code…

The Time library has a now() function that returns the current “time” as a time_t variable.

  // Oops, an error occurred
  time_t oops = now();
  char timeString[20];
  makeTimeString(hour(oops), minute(oops), second(oops), timeString);