#include <ShiftRegister74HC595.h>
// Shift register Pin definitions
#define DS_PIN 4 // DS - Pin 04
#define STCP_PIN 3 // STCP - PIN 03
#define SHCP_PIN 2 // SHCP - PIN 02
// Create instances of ShiftRegister74HC595 for each shift register with size
ShiftRegister74HC595<24> sr(DS_PIN, SHCP_PIN, STCP_PIN);
// Define segment mapping for each display
int segMapDisplay1[7] = {0, 1, 2, 3, 4, 5, 6}; // A-G Segment mapping for Display 1
int segMapDisplay2[7] = {7, 8, 9, 10, 11, 12, 13}; // A-G Segment mapping for Display 2
int segDecimalPoints[7] = {22, 23}; // Segment mapping for Decimal Point
// Pin Definitions for 4-digit 7-segment display
const int segDataLine1[4] = {14, 15, 16, 17}; // First 4-Digit 7-Segment Display (Display 1). Pins for each digit (D1-D4)
const int segDataLine2[4] = {18, 19, 20, 21}; // Second 4-Digit 7-Segment Display (Display 2). Pins for each digit (D1-D4)
// Digit patterns for common cathode display (with decimal points)
const byte digitPatterns[10] = {
B00111111, // Digit 0
B00000110, // Digit 1
B01011011, // Digit 2
B01001111, // Digit 3
B01100110, // Digit 4
B01101101, // Digit 5
B01111101, // Digit 6
B00000111, // Digit 7
B01111111, // Digit 8
B01101111 // Digit 9
};
void setup() {
// Initialize shift register pins as outputs
for (int i = 0; i < 4; i++) {
pinMode(segDataLine1[i], OUTPUT);
pinMode(segDataLine2[i], OUTPUT);
}
}
void loop() {
// Display numbers 1 to 8 on the two 4-digit displays
for (int i = 1; i <= 9; i++) {
displayNumber(i, segDataLine1); // Display on first 4-digit display
delay(1000); // Delay for 1 second
displayNumber(i, segDataLine2); // Display on second 4-digit display
delay(1000); // Delay for 1 second
}
}
void displayNumber(int number, const int* segDataLine) {
byte pattern = digitPatterns[number];
sr.setAllLow(); // Set all segments to low initially (turn off)
// Set the segments according to the pattern
for (int i = 0; i < 7; i++) {
sr.set(segMapDisplay1[i], bitRead(pattern, i));
sr.set(segMapDisplay2[i], bitRead(pattern, i));
}
// Output the pattern to the corresponding display's segment lines
for (int i = 0; i < 4; i++) {
digitalWrite(segDataLine[i], bitRead(pattern, i));
}
sr.updateRegisters(); // Update the shift registers with the new data
}