#include "SevSeg.h"
SevSeg sevseg;
const int buttonPin = 10; // Pin connected to the button
int currentNumber = 0; // Current number displayed on the 7-segment
bool lastButtonState = HIGH; // Last state of the button
unsigned long lastDebounceTime = 0; // Last time the button state changed
const unsigned long debounceDelay = 50; // Debounce time in milliseconds
void setup() {
// Initialize serial communication for debugging
Serial.begin(9600);
// Set to 1 for single digit display
byte numDigits = 1;
// Defines common pins while using multi-digit display. Left empty as we have a single digit display
byte digitPins[] = {};
// Defines Arduino pin connections in order: A, B, C, D, E, F, G, DP
byte segmentPins[] = {3, 2, 8, 7, 6, 4, 5, 9};
bool resistorsOnSegments = true;
// Initialize sevseg object
sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins, resistorsOnSegments);
// sevseg.begin(COMMON_ANODE, numDigits, digitPins, segmentPins, resistorsOnSegments);
sevseg.setBrightness(90);
// Initialize button pin
pinMode(buttonPin, INPUT); // No internal pull-up resistor, as we use an external one
// Initialize the display with the first number
sevseg.setNumber(currentNumber);
sevseg.refreshDisplay();
}
bool buttonPressed = false;
void loop() {
// Read the current state of the button
bool reading = digitalRead(buttonPin);
Serial.print("Button state: ");
Serial.println(reading);
// Check if the button state has changed
if (reading!= lastButtonState) {
Serial.println("Button state changed");
// Reset the debounce timer
lastDebounceTime = millis();
buttonPressed = false;
}
// Check if the debounce delay has passed
if ((millis() - lastDebounceTime) > debounceDelay) {
Serial.println("Debounce delay passed");
// If the button state has changed, update the current number
if (reading == LOW &&!buttonPressed) {
Serial.println("Button pressed");
// Increment the number and reset to 0 if it exceeds 9
currentNumber++;
if (currentNumber > 9) {
currentNumber = 0;
}
// Update the 7-segment display
sevseg.setNumber(currentNumber);
Serial.print("Current number: ");
Serial.println(currentNumber);
buttonPressed = true;
} else if (reading == HIGH && buttonPressed) {
buttonPressed = false;
}
}
// Update the last button state
lastButtonState = reading;
// Refresh the display
sevseg.refreshDisplay();
delay(100);
}