#include <SPI.h>
// Define pin numbers for buttons and LEDs
const int decrementButtonPin = 2; // Pin connected to the decrement button
const int resetButtonPin = 3; // Pin connected to the reset button
const int greenLEDPin = 4; // Pin connected to the green LED
const int redLEDPin = 5; // Pin connected to the red LED
// Define pins for shift register control
const int latchPin = 6; // Connects to RCLK on the first 74HC595
const int clockPin = 7; // Connects to SRCLK on both 74HC595
const int dataPin = 8; // Connects to SER on the first 74HC595
// Define starting number for the counter
int counter = 24;
// Variables to store the last time the buttons were pressed
unsigned long lastDecrementTime = 0;
unsigned long lastResetTime = 0;
// Minimum time between button presses (milliseconds)
unsigned long debounceDelay = 200; // Adjusted debounce delay
// Variables to store the previous state of the buttons
bool lastDecrementButtonState = HIGH;
bool lastResetButtonState = HIGH;
// Define inverted patterns for displaying digits 0-9 on 7-segment displays
const byte digitPatterns[] = {
B00000011, // 0
B10011111, // 1
B00100101, // 2
B00001100, // 3
B10011000, // 4
B01001001, // 5
B01000000, // 6
B00011110, // 7
B00000001, // 8
B00011001, // 9
};
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize inputs and outputs
pinMode(decrementButtonPin, INPUT_PULLUP);
pinMode(resetButtonPin, INPUT_PULLUP);
pinMode(greenLEDPin, OUTPUT);
pinMode(redLEDPin, OUTPUT);
// Initialize shift register control pins
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
// Initially turn on the green LED
digitalWrite(greenLEDPin, HIGH);
digitalWrite(redLEDPin, LOW);
// Initialize display
updateDisplay(counter);
}
void loop() {
handleDecrementButton();
handleResetButton();
}
void handleDecrementButton() {
bool currentDecrementButtonState = digitalRead(decrementButtonPin);
// Check for the transition from HIGH to LOW
if (currentDecrementButtonState == LOW && lastDecrementButtonState == HIGH) {
if (millis() - lastDecrementTime >= debounceDelay) {
lastDecrementTime = millis();
if (counter > 0) {
counter--;
if (counter == 0) {
// Turn off green LED and turn on red LED
digitalWrite(greenLEDPin, LOW);
digitalWrite(redLEDPin, HIGH);
}
// Update the display
updateDisplay(counter);
}
}
}
// Update the last state
lastDecrementButtonState = currentDecrementButtonState;
}
void handleResetButton() {
bool currentResetButtonState = digitalRead(resetButtonPin);
// Check for the transition from HIGH to LOW
if (currentResetButtonState == LOW && lastResetButtonState == HIGH) {
if (millis() - lastResetTime >= debounceDelay) {
lastResetTime = millis();
counter = 24;
// Turn on green LED and turn off red LED
digitalWrite(greenLEDPin, HIGH);
digitalWrite(redLEDPin, LOW);
// Update the display
updateDisplay(counter);
}
}
// Update the last state
lastResetButtonState = currentResetButtonState;
}
void updateDisplay(int num) {
// Extract digits from the number
int tens = num / 10;
int ones = num % 10;
// Update the display using shift registers
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, digitPatterns[ones]); // Shift out ones first
shiftOut(dataPin, clockPin, LSBFIRST, digitPatterns[tens]); // Shift out tens second
digitalWrite(latchPin, HIGH);
}