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.
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.
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.
// 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;
}
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.
#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.
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);
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:
you will see what each function does, and how they can, and can not, be combined to produce useful results.
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;
}