#include <SevSegShift.h>
#include <Bounce2.h>
#include <FastShiftIn.h>
SevSeg sevseg;
FastShiftIn buttonShiftRegister(7, 8, MSBFIRST);
const int DISPLAY_LATCH_PIN = A1; // 74HC595 pin 12
const int DISPLAY_DATA_PIN = A0; // 74HC595 pin 14
const int DISPLAY_CLOCK_PIN = A2; // 74HC595 pin 11
const int BUTTON_LATCH_PIN = 9;
const int BUTTON_DATA_PIN = 7; // Replace with the actual pin number
const int BUTTON_CLOCK_PIN = 8; // Replace with the actual pin number
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();
int buttonPushCounter = 0;
void setup() {
// Display setup
byte numDigits = 1;
byte digitPins[] = {2};
byte segmentPins[] = {6, 5, 2, 3, 4, 7, 8, 9};
bool resistorsOnSegments = true;
byte hardwareConfig = COMMON_ANODE;
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
sevseg.setBrightness(90);
pinMode(DISPLAY_LATCH_PIN, OUTPUT);
pinMode(DISPLAY_CLOCK_PIN, OUTPUT);
pinMode(DISPLAY_DATA_PIN, OUTPUT);
pinMode(BUTTON_LATCH_PIN, OUTPUT);
pinMode(BUTTON_CLOCK_PIN, OUTPUT);
pinMode(BUTTON_DATA_PIN, INPUT);
increaseButton.attach(BUTTON_LATCH_PIN, BUTTON_DATA_PIN);
decreaseButton.attach(BUTTON_LATCH_PIN, BUTTON_DATA_PIN);
resetButton.attach(BUTTON_LATCH_PIN, BUTTON_DATA_PIN);
increaseButton.interval(50);
decreaseButton.interval(50);
resetButton.interval(50);
buttonPushCounter = 0;
Serial.begin(9600);
}
void loop() {
byte buttonsState = buttonShiftRegister.read();
increaseButton.update();
decreaseButton.update();
resetButton.update();
if (increaseButton.fell()) {
buttonPushCounter++;
if (buttonPushCounter >= 10) {
buttonPushCounter = 0;
}
updateDisplay();
}
if (decreaseButton.fell()) {
buttonPushCounter--;
if (buttonPushCounter < 0) {
buttonPushCounter = 9;
}
updateDisplay();
}
if (resetButton.fell()) {
buttonPushCounter = 0;
updateDisplay();
}
}
void updateDisplay() {
shiftOut(DISPLAY_DATA_PIN, DISPLAY_CLOCK_PIN, MSBFIRST, segmentPatterns[buttonPushCounter]);
digitalWrite(DISPLAY_LATCH_PIN, HIGH);
digitalWrite(DISPLAY_LATCH_PIN, LOW);
}