/*
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();
}