Possible to print millis in [HH:MM:SS] format?

Hi I’m trying to insert arduino runtime into a tweet like this:

sprintf(tweet,"%d Sensor Reading S1=%e S2=%e", millis, (float)temp_f, (float)real_humidity); //Your tweet message

But millis of course is just the ms it’s been running in raw form which quickly becomes a huge string. How can I format millis into a 24 hour display, [HH:MM:SS] that resets every 24 hours, or better yet, insert an authentic time value? I need a time stamp of some sort to get around Twitter’s spam filters.

Thanks!

Look at the Time Library. It provides all the functions you want for this.

Korman

You'll need an RTC if you don't want to have to reset the real time every time the Arduino restarts.

Thanks! A RTC would be nice to have but not necessary as I just need some sort of counter not to exceed 6 digits to get around Twitter filtering.

I'll experiment with the time library.

If that's all you need, just generate a random number. Or, only use the last 6 characters of millis()'s value as a string.

Is there no way to easily format millis as HHMMSS? I'm digging through the Time library and it seems like everything syncs to some external time source be it my computer or RTC.

If that's all you need...

...or an unsigned incremented with each message.

I'm digging through the Time library and it seems like everything syncs to some external time source be it my computer or RTC.

Don't get mislead, the important thing with the Time library is that everything can sync with an external timer source, but doesn't need to. You also can set the time manually or decide times based on the 1.1.1970 (or the number of seconds since the the Arduino was turned on) are good enough. Just ignore the syncing part you don't care about.

Korman

Hey can someone give me some guidance please on the time library? Which example should I turn to? I just want millis to print as HH:MM:SS.

Thanks much.

You stated earlier this is what you need...

I just need some sort of counter not to exceed 6 digits to get around Twitter filtering

Have you changed your mind?

I just want millis to print as HH:MM:SS.

try this

// t is time in seconds = millis()/1000;
char * TimeToString(unsigned long t)
{
  static char str[12];
  long h = t / 3600;
  t = t % 3600;
  int m = t / 60;
  int s = t % 60;
  sprintf(str, "%04ld:%02d:%02d", h, m, s);
  return str;
}

@robtillaart: Won't the string exceed 6 digits?

Won't the string exceed 6 digits?

Yes, definitely! The format jacob wanted "HH:MM:SS" is 8 characters long (I quoted that), and the code I provide will generate a string in HHHH:MM:SS format so at least 10 characters long (not counting the '\0' charcters needed). Imho the code provides enough insight to alter it to generate a string of six chars.

I’m getting an error with this:

#include <Time.h>


void setup()
{
   Serial.begin(9600); // Open serial connection to report values to host
   Serial.println("Starting up");
}

void loop()
{
  // t is time in seconds = millis()/1000;
char * TimeToString(unsigned long t)

  static char str[12];
  long h = t / 3600;
  t = t % 3600;
  int m = t / 60;
  int s = t % 60;
  Serial.sprintf(str, "%04ld:%02d:%02d", h, m, s);
  return str;


  delay(10000);
}

"Expected initialized before “static.”

I’m sorry if this is extremely simple for some but I just want a simple counter to display in my tweet.

  Serial.sprintf(str, "%04ld:%02d:%02d", h, m, s);

The Serial class does not have a sprintf method.

  delay(10000);

After the return statement, this code is unreachable. Waste of space and time having it here.

void loop()
  // t is time in seconds = millis()/1000;
char * TimeToString(unsigned long t)

What happened to the body of loop()?

So how can I test this? I'm trying to test it in serial before inserting into my tweeting sketch.

This seems so basic to have some sort of run time clock on an arduino device. Why is this so difficult?

This seems so basic to have some sort of run time clock on an arduino device. Why is this so difficult?

Because you mess around. Back to your original code sample, when applying the hint you got about the Time library, you might get something like this:

sprintf(tweet,"%2.2d:%2.2d:%2.2d Sensor Reading S1=%e S2=%e",
 hour(), minute(), second(),
 (float)temp_f, (float)real_humidity);

And don’t forget to include the Time library at the top of your sketch with this: #include <Time.h>

Does that look like a start? If you want to have time match the real time closer, you can set it anywhere in your program with:

setTime(hr,min,sec,day,month,yr);

This leaves only the small detail of getting good values for the variables hr, min, sec, day, month and yr. This can be achieved in various ways, but that we can leave for later. For experimenting you always can put that line in your setup() function:setTime(10,55,0,22,11,2010);

Korman

This seems so basic to have some sort of run time clock on an arduino device. Why is this so difficult?

You are starting from a false assumption. For 99% of embedded projects, the current time as relayed by your wall clock is completely irrelevant.

Think of a washing machine. The controller needs to turn the motor on and off at specific times, and to open and close water valves at specific times. Those times are not based on the wall clock, though, They are relative to the time you push the start button.

Does the dishwasher or microwave need to know what time it is. No. Everything they need to do is based on the time you press the go button.

You are the one trying to force a device that doesn't know about wall clocks to pretend that it does.

If you read this page: http://arduino.cc/en/Reference/HomePage you will see what each function does, and how they can, and can not, be combined to produce useful results.

Reaction on your code feew postings back :

You should declare functions outside the loop(). Try this code

void setup()
{
   Serial.begin(9600); // Open serial connection to report values to host
   Serial.println("Starting up");
}

void loop()
{
  char *s;
  s = TimeToString(millis()/1000);
  Serial.println(s);
  delay(456);
}


// t is time in seconds = millis()/1000;
char * TimeToString(unsigned long t)
{
  static char str[12];
  long h = t / 3600;
  t = t % 3600;
  int m = t / 60;
  int s = t % 60;
  sprintf(str, "%04ld:%02d:%02d", h, m, s);
  return str;
}
  delay(456);

456? What significance does that value have?