Unit 11: Digital Numbers
Code for Sketch One:
#include <GyverTM1637.h>
// Define the connections pins for the TM1637 4-digit display
#define CLK 3
#define DIO 2
GyverTM1637 display(CLK, DIO);
// Define the button pin
const int buttonPin = 4;
// Variable to count button presses
int count = 0;
// Variable to store the button state
int lastButtonState = HIGH;
void setup() {
// Initialize the TM1637 display
display.clear();
display.brightness(7); // Set brightness to maximum
// Initialize button pin as input with internal pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
// Display initial count
display.displayInt(count);
}
void loop() {
// Read the current state of the button
int buttonState = digitalRead(buttonPin);
// Check if the button is pressed (LOW state) and was previously not pressed
if (buttonState == LOW && lastButtonState == HIGH) {
delay(50); // Debounce delay
if (digitalRead(buttonPin) == LOW) {
count++; // Increment count
display.displayInt(count); // Display the new count
}
}
// Update the last button state
lastButtonState = buttonState;
}