Simple Timer for Serial Monitor - no hardware needed

Hello, after taking so much from this forum in my ongoing learning process, I wanted to give something back. It is probably not much, but I feel like sharing.

so the following is a sketch that prints on the serial monitor a timer, in the format HH:MM:SS.
There is literally nothing more to this, but the web seemed a bit spread out on this simple subject, so I thought it could be relevant and useful anyway.
It is coarse, dumb-proof (written by one), so much that it lacks comments, it is really self-explanatory.
I am probably devouring memory with this, so any improvement is more than welcome, as it always should be.

unsigned long timer = 0;
static int sec1 = 0;
static int sec10 = 0;
static int min1 = 0;
static int min10 = 0;
static int hrs1 = 0;
static int hrs10 = 0;


void setup() {
  Serial.begin(9600);
}

void loop() {

// *** TIMER ***

timer = millis();
if (timer >= 1000) {
  sec1 = sec1 + 1;
  timer = 0;
}
if (sec1 == 10) {
  sec10 = sec10 + 1 ;
  sec1 = 0;
}
if (sec10 == 6) {
  min1 = min1 + 1;
  sec10 = 0;
}
if (min1 == 10) {
  min10 = min10 + 1;
  min1 = 0;
}
if (min10 == 6) {
  hrs1 = hrs1 + 1;
  min10 = 0;
}
if (hrs1 == 10) {
  hrs10 = hrs10 + 1;
  hrs1 = 0;
}


Serial.print(hrs10); Serial.print(hrs1); Serial.print(":");
Serial.print(min10); Serial.print(min1); Serial.print(":");
Serial.print(sec10); Serial.println(sec1);
delay(1000);

}
1 Like

Do not use delay.
The delay(1000) means that it wait in inaction 1s, then other commands take some time so the print hardly will be in second interval. You have already test for 1s. Utilize this.

Could you explain why?

I don't get it, sorry. This just works as a timer, I actually need the delay so that the printed timer refreshes every 1s... And I am working on it to measure cooling times of an insulated enclosure, so I wouldn't mind if the sensor readings refreshed every second as well. But this is even beyond the scope of my post...

Then just couple it with the seconds update :slight_smile: Although your whole timer thing doesn't make any sense.

Thing is, even if the clock of the Arduino is very accurate, the delay() only delays in that single command, the rest of the code isn't taken into account an although not much, will take time to execute.

And also, the splitting of hours1 and hours2 does not make sense...

All in all, it's a complicated and flawed "simple example" :wink:

const unsigned int UpdateInterval = 1000;

unsigned long lastUpdate;

byte hour;
byte minute;
byte second;

void setup(){
  Serial.begin(115200);
}

void loop(){
  if(millis() - lastUpdate >= UpdateInterval){
    lastUpdate += UpdateInterval;
    
    second++;
    
    if(second == 60){
      second = 0;
      minute++;
    }
    if(minute == 60){
      minute = 0;
      hour++;
    }
    if(hour == 24){
      hour = 0;
    }
    
    Serial.print(hour);
    Serial.print(':');
    if(minute < 10){
      Serial.print('0');
    }
    Serial.print(minute);
    Serial.print(':');
    if(second < 10){
      Serial.print('0');
    }
    Serial.print(second);
  }
}
1 Like

...people forget about the Time library and how easy it can help with something like this.

look Mom, no delay()!

#include <Time.h>
#include <TimeLib.h>

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  static byte lastSecond = 0;
  uint32_t currentSeconds = now();
  if(lastSecond != second(currentSeconds))
  {
    lastSecond = second(currentSeconds);
    char elapsedTime[32] = "";
    int hrs = currentSeconds / 3600;
    int minutes = currentSeconds % 3600 / 60;
    sprintf(elapsedTime, "%3d:%02d:%02d", 
      hrs, 
      minutes, 
      (currentSeconds % 60)
    );
    Serial.println(elapsedTime);
  }
}

Have a look at how millis() is used to manage timing without blocking in Several things at a time

...R

Yes, this part makes no sense:

timer = millis();
if (timer >= 1000) {
  sec1 = sec1 + 1;
  timer = 0;
}

Since millis() will return some large value after 1 second, the if statement will always be executed. That's probably why you gave up and used delay() but you never went back and tried to solve this.

Thanks for the insights people!
Although if I were you I would lose the attitude or jack off more.

For me, that's mainly because of:

smntnz:
It is coarse, dumb-proof (written by one), so much that it lacks comments, it is really self-explanatory.

because I found non of the statements to be true.... ::slight_smile:

But, we are here to learn, even I am :slight_smile: And you're reasonable but the old dogs here have encountered sooooo many stubborn newbies which just don't want to hear what they don't like. For example, not reading the simple forum rules to keep the forum tidy, not posting all the code, making it a XY-problem, don't want to hear there plan of action is flawed etc. That does make you a little bit hard when someone makes a statement like yours above :wink: