Countdown timer with milliseconds

Hello.
Writing this from memory... at work at the moment...

I have a TFT screen and I need to display minutes, seconds and milliseconds counting down.

I started with a test routine that triggered every 100ms if (currentMillis - timerMillis > 100) {}

Each time that was called, then it reduced the milliseconds by 10 on the display, and using the obvious coding, it reduced seconds if required and minutes.

But... it's not keeping time. I believe I have too much in the main loop and that means the updating is occurring well past the 100ms trigger point (if that makes sense). It drifts out and starts to run slow.

I wanted to use the Countdown library, as this seems far more efficient than my checks to see if seconds = 0 then decrease minutes and reset seconds to 59 etc.

But, I can't see how to retrieve the minutes, seconds and milliseconds value from that library (maybe you can't).

Now maybe part of the problem is I am blanking out the changing clock value (only the ones that need changing) using a tft.fillRect(x,y,12,12, BLACK); command. But I thought one line like this was more efficient than drawing the text again in Black.

I realise you need to see my horrific code, but can't do that until back tonight.
I also realise that the other stuff in the main loop needs to be quicker, but I have made that as efficient as I can. It checks a wifi connection every 3 seconds and that needs to stay.

I think what I need is some code that will keep time and allow the milliseconds element to skip values (and therefore displaying them) in order to keep time.

Just wondered if anyone had any ideas or links to useful libraries. I downloaded 3 different countdown libraries and none did what I need (as in, I could not work out how to retrieve the individual min, secs and ms values to display).

We'll wait for it to see what the code is all doing :wink: But my guess is that milliseconds accuracy might be a bit much asked with a TFT; have you checked how long updating of the TFT takes? Just write a dedicated sketch for that.

Use >= for the comparison, then when you update timermillis use:

Timermillis += 100;

Hi @anon64083092,

maybe this sketch can be of assistance:

/*
  Timing test for ILI9341 LCD

  Forum: https://forum.arduino.cc/t/countdown-timer-with-milliseconds/1156553
  Wokwi: https://wokwi.com/projects/372574871197135873


*/

#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"

#define TFT_DC 9
#define TFT_CS 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

// Choose the number of digits to print
constexpr byte digits =  12;

constexpr byte Top    = 120;
constexpr byte Left   =   0;
constexpr byte Width  = 12 * digits;
constexpr byte Height = 16;

// set to true for erasing with fillRect 
// set to false for erasing by printing with black
constexpr bool fillRect = false;

unsigned long startTime;
unsigned long number = 0;
char buf[20] {};
char fmt[20];

void setup() {
  Serial.begin(115200);
  tft.begin();
  tft.setTextColor(ILI9341_GREEN);
  tft.setTextSize(2);
  startTime = millis();
  sprintf(fmt, "%%%dd", digits);
}

void printNumber() {
  if (fillRect) {
    tft.fillRect(Left, Top, Width, Height, ILI9341_BLACK);
  } else {
    tft.setCursor(Left, Top);
    tft.setTextColor(ILI9341_BLACK);
    tft.println(buf);
    tft.setTextColor(ILI9341_GREEN);
  }  
  tft.setCursor(Left, Top);
  sprintf(buf, fmt, number);
  tft.println(buf);
  number++;
  if (number % 100 == 0) {
    unsigned long endTime = millis();
    Serial.print("Time per println [msec]:\t");
    Serial.println((endTime - startTime) / 100);
    startTime = millis();
  }
}

void loop() {
  printNumber();
}

You can check it out on Wokwi:

P S.: To bei clear: The sketch allows to check the time it takes to use fillRect versus printing the old text with black color... If you want us to check your code it would be good if you post it...

Thanks all.... I will have a look as soon as I get in tonight

OK. Problem solved.

The WiFi library I am using has a delay(50); hidden in it. I have removed the delay and it doesn't seem to have affected it's working.

That has cured the timing issue.... surprise surprise!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.