#include "SevSegShift.h"
#include <Bounce2.h>
SevSeg sevseg;
const int increaseButtonPin = 10; // the pin that the increaseButtonPin is attached to
const int decreaseButtonPin = 11; // the pin that the decreaseButtonPin is attached to
const int resetButtonPin = 12; // the pin that the resetButtonPin is attached to
int buttonPushCounter = 0; // counter for the number of button presses
const int LATCH_PIN = A1; // 74HC595 pin 12
const int DATA_PIN = A0; // 74HC595 pin 14
const int CLOCK_PIN = A2; // 74HC595 pin 11
// Define segment patterns for 7-segment display (assuming COMMON_ANODE)
byte segmentPatterns[] = {
0b11000000, // Digit 0: 0
0b11111001, // Digit 1: 1
0b10100100, // Digit 2: 2
0b10110000, // Digit 3: 3
0b10011001, // Digit 4: 4
0b10010010, // Digit 5: 5
0b10000010, // Digit 6: 6
0b11111000, // Digit 7: 7
0b10000000, // Digit 8: 8
0b10010000 // Digit 9: 9
};
Bounce increaseButton = Bounce();
Bounce decreaseButton = Bounce();
Bounce resetButton = Bounce();
void setup() {
byte numDigits = 1;
byte digitPins[] = {2}; // At least one element needed
byte segmentPins[] = {6, 5, 2, 3, 4, 7, 8, 9}; // A to G
bool resistorsOnSegments = true;
byte hardwareConfig = COMMON_ANODE; // Assuming COMMON_ANODE configuration
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
sevseg.setBrightness(90);
pinMode(increaseButtonPin, INPUT_PULLUP);
pinMode(decreaseButtonPin, INPUT_PULLUP);
pinMode(resetButtonPin, INPUT_PULLUP);
pinMode(LATCH_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(DATA_PIN, OUTPUT);
increaseButton.attach(increaseButtonPin); // Attach the increaseButton to the button pin
increaseButton.interval(50); // Set debounce time in milliseconds
decreaseButton.attach(decreaseButtonPin); // Change to the actual pin number
decreaseButton.interval(50); // Set debounce time in milliseconds
resetButton.attach(resetButtonPin); // Change to the actual pin number
resetButton.interval(50); // Set debounce time in milliseconds
buttonPushCounter = 0; // Initialize the counter to 0
Serial.begin(9600);
}
void loop() {
// Update the Buttons
increaseButton.update();
decreaseButton.update();
resetButton.update();
if (increaseButton.fell()) {
// Button pressed, increment the displayed number by 1
buttonPushCounter++;
Serial.println(buttonPushCounter);
// If the counter goes beyond the last digit, reset it to 0
if (buttonPushCounter >= 10) {
buttonPushCounter = 0;
}
// Shift out segment pattern to the shift register
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, segmentPatterns[buttonPushCounter]);
// Latch the data to update the display
digitalWrite(LATCH_PIN, HIGH);
digitalWrite(LATCH_PIN, LOW);
}
if (decreaseButton.fell()) {
// Decrease button pressed, decrement the displayed number by 1
buttonPushCounter--;
Serial.println(buttonPushCounter);
// If the counter goes below 0, wrap around to 9
if (buttonPushCounter < 0) {
buttonPushCounter = 9;
}
// Shift out segment pattern to the shift register
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, segmentPatterns[buttonPushCounter]);
// Latch the data to update the display
digitalWrite(LATCH_PIN, HIGH);
digitalWrite(LATCH_PIN, LOW);
}
if (resetButton.fell()) {
// Reset button pressed, set the displayed number to 0
buttonPushCounter=0;
// Shift out segment pattern to the shift register
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, segmentPatterns[buttonPushCounter]);
// Latch the data to update the display
digitalWrite(LATCH_PIN, HIGH);
digitalWrite(LATCH_PIN, LOW);
}
// Rest of your loop code...
}