Arduino startable and stoppable background counter.

I am trying to create a simple counter that counts up in seconds, then after 60 seconds becomes a minute, after 60 minutes an hour and so-on.
I was trying millis() but that starts as soon as the program runs, and I want my counter to run when a function is called. And it can be reset by setting some variables to zero or calling a different function. This is what I have so far:

String getCurrentTime() {
  s = millis() / 1000;
  s = 0;
  if (s >= 60) {
    s = 0;
  if (m >= 60) {
    m = 0;

  return (String(h) + " hour(s) : " + String(m) + " minute(s) : " + String(s) + " second(s)");

The problem is that, as mentioned, it starts as soon as the program runs and also, becuase of millis(), it doesn't reset to 0. So once seconds equals 60, the minutes go up every seconds (or really every time the function is called).
The upside to millis() is that I don't have to run the function in the background or with a while loop to make it always count up, as soon as the funciton is called, 's' is equal to the seconds passed.


Your code is defunct, you are setting "s" to zero which will cause the time never to increase. Look at the time library instead or use the modulus operator:

unsigned long sec = millis() / 1000, mins, hours;
mins = secs / 60;
secs = secs % 60;
hours = mins / 60;
mins = mins % 60;

And stop using the "String", it will cause troble.

Thanks for the reply,

I will try out the Time library becuase as I mentioned millis() won't work.


I didn't do in depth research but, a site search of 'stopwatch' turned up quite a few results. Possibly something there might be instructive.