Skip to content

Instantly share code, notes, and snippets.

@rooreynolds
Created November 22, 2020 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rooreynolds/65dae244f7367c03c17e7065b058ad60 to your computer and use it in GitHub Desktop.
Save rooreynolds/65dae244f7367c03c17e7065b058ad60 to your computer and use it in GitHub Desktop.
//inspired by https://twitter.com/paulpod/status/1330091111889690628
#include <Arduino.h>
#include <TM1637Display.h>
#include <Bounce2.h>
#define CLK 2
#define DIO 3
#define pin_button A0
int duration = 3000;
long timeLastPressed = duration;
boolean stopped = false;
Bounce button = Bounce(pin_button, 50); // ms debounce for button
TM1637Display display(CLK, DIO); // four digit seven segment display
void setup()
{
display.clear();
pinMode(pin_button, INPUT_PULLUP);
display.setBrightness(0); // 0 to 7
}
void maintainDisplay() {
static const unsigned long REFRESH_INTERVAL = 1; // how often in ms to update the display
static unsigned long lastRefreshTime = -1;
if(millis() - lastRefreshTime >= REFRESH_INTERVAL) { //should we update the display?
lastRefreshTime += REFRESH_INTERVAL;
updateDisplay();
}
}
void updateDisplay() {
long now = millis();
int displaytime = now - timeLastPressed;
if (displaytime >= 0) { // has the countdown has run out?
displaytime = 0;
stopped = true;
}
display.showNumberDec(abs(displaytime), true); // show displaytime without center colon, with leading 0s
}
void loop() {
if (button.update()) {
if (button.fallingEdge()) {
stopped = ! stopped;
timeLastPressed = millis() + duration;
}
}
if (! stopped) {
maintainDisplay();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment