#include "SevSeg.h" SevSeg sevseg; // Initiate a seven-segment controller object int score = 0; int buttonState = 0; int lastButtonState = 1; // Initial state when the button is not pressed const int button = A0; // Button pin unsigned long lastDebounceTime = 0; // the last time the output pin was toggled unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers void setup() { Serial.begin(9600); byte numDigits = 4; byte digitPins[] = {2, 3, 4, 5}; byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13}; bool resistorsOnSegments = false; // If resistors are on segment pins, set this to true sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins, resistorsOnSegments); sevseg.setBrightness(90); pinMode(button, INPUT_PULLUP); // Configure the button pin as input with internal pull-up resistor } void loop() { int reading = digitalRead(button); // Read the state of the button // Check if the button state has changed if (reading != lastButtonState) { lastDebounceTime = millis(); // Reset the debounce timer } if ((millis() - lastDebounceTime) > debounceDelay) { // If the button state has been stable for longer than the debounce delay if (reading == LOW && buttonState == HIGH) { // Button is pressed score += 1; } buttonState = reading; // Update the button state } lastButtonState = reading; // Update the last button state sevseg.setNumber(score); // Display the updated score sevseg.refreshDisplay(); // Must run repeatedly }
@yzhmoeh3 ай бұрын
i think the first initialization is incorrect. It should be: int score = 0;
@Waiduino3 ай бұрын
@@yzhmoeh Thank you so much for catching that! I just changed it on the comment
@quangmi34022 ай бұрын
Can you guide and write code on how to move forward and backward, with a button that returns to 0?
@Waiduino2 ай бұрын
@@quangmi3402 Does it need to be with a 4 digit seven segment or can you use a 1 digit?