// 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[10] = {
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 digitControl[4] = {
B11111110, // Digit 1 (Q0)
B11111101, // Digit 2 (Q1)
B11111011, // Digit 3 (Q2)
B11110111 // Digit 4 (Q4)
};
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop() {
for (int i = 0; i <= 9999; i++) {
displayNumber(i);
delay(40); // Adjust delay as needed
}
}
void displayNumber(int number) {
byte digits[4] = {0};
// Split number into individual digits
for (int i = 0; i < 4; i++) {
digits[i] = number % 10;
number /= 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[digits[digit]]);
// Send the digit control data
shiftOut(dataPin, clockPin, MSBFIRST, digitControl[digit]);
digitalWrite(latchPin, HIGH);
// Small delay to avoid flickering
delay(5);
}
}