Pages: [1] 2   Go Down
Author Topic: Possible to print millis in [HH:MM:SS] format?  (Read 4251 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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!

« Last Edit: November 15, 2010, 06:33:38 pm by .jacob » Logged

Central Europe
Offline Offline
Edison Member
*
Karma: 7
Posts: 1220
Use the Source, Luke.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Korman
« Last Edit: November 15, 2010, 06:43:27 pm by Korman » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 199
Posts: 12768
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If that's all you need...
...or an unsigned incremented with each message.
« Last Edit: November 15, 2010, 10:16:44 pm by bcook » Logged

Central Europe
Offline Offline
Edison Member
*
Karma: 7
Posts: 1220
Use the Source, Luke.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 199
Posts: 12768
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


You stated earlier this is what you need...
Quote
I just need some sort of counter not to exceed 6 digits to get around Twitter filtering
Have you changed your mind?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

try this
Code:
// 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;
}
« Last Edit: November 20, 2010, 02:58:05 am by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 199
Posts: 12768
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
« Last Edit: November 20, 2010, 03:58:27 am by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm getting an error with this:

Code:
#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.
« Last Edit: November 21, 2010, 06:52:09 pm by .jacob » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
 Serial.sprintf(str, "%04ld:%02d:%02d", h, m, s);
The Serial class does not have a sprintf method.

Code:
 delay(10000);
After the return statement, this code is unreachable. Waste of space and time having it here.

Code:
void loop()
  // t is time in seconds = millis()/1000;
char * TimeToString(unsigned long t)
What happened to the body of loop()?
Logged

Pages: [1] 2   Go Up
Jump to: