// Pin definitions for 74HC595
const int latchPin = 5; // Latch pin (RCLK)
const int clockPin = 6; // Clock pin (SCLK)
const int dataPin = 4; // Data pin (DS)
// Segment patterns for digits 0-9 for common cathode
const byte digitSegments[11] = {
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B10111110, // 6
B11100000, // 7
B11111110, // 8
B11110110 // 9
};
// Digit control patterns
const byte digitControl1[5] = {
B11111110, // Digit 1 (Q0)
B11111101, // Digit 2 (Q1)
B11111011, // Digit 3 (Q2)
B11110111 // Digit 4 (Q4)
};
// Digit control patterns
const byte digitControl2[5] = {
B11101111, // Digit 1 (Q0)
B11011111, // Digit 2 (Q1)
B10111111, // Digit 3 (Q2)
B01111111 // Digit 4 (Q4)
};
#define upButton A5
#define downButton A4
#define startButton A3
int i=0;
int j=0;
bool f;
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(upButton, INPUT);
pinMode(downButton , INPUT );
pinMode(startButton , INPUT );
}
void loop() {
if (digitalRead(upButton) == HIGH ){
i++;
j=0;
f=true;
delay(10);
if(i > 9999){i=0;delay(1000);}
}
else if (digitalRead(downButton) == HIGH ) {
i--;
j=0;
f=true;
delay(10);
if(i < 0){i=9999; delay(1000);}
}
else if (digitalRead(downButton) == HIGH ) {
j=i;
f=false;
}
displayNumber(i,j,f);
}
void displayNumber(int number1, int number2, bool led) {
byte digits1[4] = {0};
byte digits2[4] = {0};
// Split number into individual digits
for (int i = 0; i < 4; i++) {
digits1[i] = number1 % 10;
number1 /= 10;
}
for (int i = 0; i < 4; i++) {
digits2[i] = number2 % 10;
number2 /= 10;
}
// Display each digit
for (int digit = 0; digit < 4; digit++) {
digitalWrite(latchPin, LOW);
// Send the segment data for the current digit
shiftOut(dataPin, clockPin, LSBFIRST, digitSegments[digits1[digit]]);
// Send the segment data for the current digit
shiftOut(dataPin, clockPin, LSBFIRST, digitSegments[digits2[digit]]);
// Send the digit control data
shiftOut(dataPin, clockPin, MSBFIRST, digitControl2[digit]);
// Send the digit control data
shiftOut(dataPin, clockPin, MSBFIRST, digitControl1[digit]);
digitalWrite(latchPin, HIGH);
// Small delay to avoid flickering
delay(5);
}
}