Skip to content

Instantly share code, notes, and snippets.

@bradmartin333
Last active October 30, 2025 11:51
Show Gist options
  • Select an option

  • Save bradmartin333/ab4e19ae32752e1a90b7e322ee9e58ad to your computer and use it in GitHub Desktop.

Select an option

Save bradmartin333/ab4e19ae32752e1a90b7e322ee9e58ad to your computer and use it in GitHub Desktop.
use a 4 digit 7 segment display for FPS testing
#include <TM1637Display.h>
#define CLK_PIN 2
#define DIO_PIN 3
const int SPEED_PINS[] = { 4, 5, 6, 7 };
const int NUM_SPEED_PINS = sizeof(SPEED_PINS) / sizeof(SPEED_PINS[0]);
const int REFRESH_RATES_HZ[] = { 30, 60, 90, 120 };
const int RATE_DEFAULT_HZ = 1;
TM1637Display display(CLK_PIN, DIO_PIN);
long currentCount = 0;
unsigned long previousMicros = 0;
int previousRateHz = 0;
unsigned long effectiveRefreshRateUs;
int getEffectiveRefreshRate() {
for (int i = 0; i < NUM_SPEED_PINS; i++) {
if (digitalRead(SPEED_PINS[i]) == LOW) {
return REFRESH_RATES_HZ[i];
}
}
return RATE_DEFAULT_HZ;
}
void setup() {
for (int i = 0; i < NUM_SPEED_PINS; i++) {
pinMode(SPEED_PINS[i], INPUT_PULLUP);
}
display.setBrightness(0x0F);
display.clear();
}
void loop() {
int effectiveRefreshRateHz = getEffectiveRefreshRate();
if (previousRateHz != effectiveRefreshRateHz) {
previousRateHz = effectiveRefreshRateHz;
effectiveRefreshRateUs = 1000000UL / (unsigned long)effectiveRefreshRateHz;
display.showNumberDec(effectiveRefreshRateHz, false);
for (int i = 0; i < 2000; i++) {
if (getEffectiveRefreshRate() != effectiveRefreshRateHz) {
return;
}
delay(1);
}
}
unsigned long currentMicros = micros();
if (currentMicros - previousMicros >= effectiveRefreshRateUs) {
previousMicros = currentMicros;
currentCount++;
if (currentCount > 9999) {
currentCount = 0;
}
display.showNumberDec(currentCount, false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment