Skip to content

Instantly share code, notes, and snippets.

@Crysknife007
Created February 1, 2024 23:58
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 Crysknife007/33c203ab117ebfd25e0e5b8585d5584e to your computer and use it in GitHub Desktop.
Save Crysknife007/33c203ab117ebfd25e0e5b8585d5584e to your computer and use it in GitHub Desktop.
A simple counter for a tm1637 display.
// .---------------------------------------------------------.
// | Spike Snell - 2024 |
// | A simple counter for a tm1637 display. |
// | |
// | - The count is reset after a long press of the button. |
// `---------------------------------------------------------'
// Import the tm1637Display library from - https://github.com/avishorp/TM1637
#include <TM1637Display.h>
// Define the clock pin
#define CLK 2
// Define the data in-out pin
#define DIO 3
// Define the button pin
#define BTN 4
// Define the long press duration
#define LONG_PRESS 100
// Initialize count to 0
int count = 0 ;
// Initialize firstPress boolean
int firstPress = true;
// Initialize held count to 0
int heldCount = 0;
// Set up the display with the clock and data in-out pin
TM1637Display display( CLK, DIO );
// Set up the display and input pin
void setup() {
// Set the input pin to be the button pin
pinMode( BTN, INPUT );
// Set the display brightness
display.setBrightness( 0x03 );
}
// Loop through and check for button presses
void loop() {
// If the button is being pressed down
if ( digitalRead(4) == 1 ) {
// Increment the held count
heldCount++;
// If this is the first time we are checking to see if the button is pressed down
if ( firstPress == true ) {
// Increment the current count
count++;
// Set firstPress flag to false
firstPress = false;
}
}
// Else if the button is not being pressed down
else {
// Set firstPress back to true
firstPress = true;
// Set held count back to 0
heldCount = 0;
}
// If the held count is more than LONG_PRESS then set the count back to 0
if ( heldCount > LONG_PRESS ) {
// Set count back to zero
count = 0;
}
// Show the current count on the display
display.showNumberDec( count, false );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment