const int dataPin = 25;
const int clockPin = 27;
const int latchPin = 26;
// Segment states for digits 0-9
byte segmentStates[] = {
0b11000000, // 0: A, B, C, D, E, F
0b11111001, // 1: B, C
0b10100100, // 2: A, B, D, E, G
0b10110000, // 3: A, B, C, D, G
0b10011001, // 4: B, C, F, G
0b10010010, // 5: A, C, D, F, G
0b10000010, // 6: A, C, D, E, F, G
0b11111000, // 7: A, B, C
0b10000000, // 8: A, B, C, D, E, F, G
0b10010000 // 9: A, B, C, D, F, G
};
// Control states for the digits (anode or cathode selection)
byte digitStates[] = {
0b00000001, // First digit
0b00000010, // Second digit
0b00000100 // Third digit
};
void setup() {
Serial.begin(115200);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
}
void loop() {
// Test the display with a number
displayNumber(1.5); // Example: Display "123"
delay(1000);
displayNumber(456); // Example: Display "456"
delay(1000);
displayNumber(789); // Example: Display "789"
delay(1000);
}
// Function to convert an integer to 7-segment display
void displayNumber(int number) {
number = constrain(number, 0, 999); // Ensure the number is within range (0-999)
// Split the number into individual digits
int hundreds = number / 100; // Hundreds place
int tens = (number / 10) % 10; // Tens place
int units = number % 10; // Units place
// Display each digit for a short period (multiplexing)
for (int i = 0; i < 50; i++) { // Repeat to create a stable display
// Display hundreds place (digit 0)
shiftData(segmentStates[hundreds], digitStates[0]);
delay(5);
// Display tens place (digit 1)
shiftData(segmentStates[tens], digitStates[1]);
delay(5);
// Display units place (digit 2)
shiftData(segmentStates[units], digitStates[2]);
delay(5);
}
}
// Function to send data to the shift register
void shiftData(byte segmentState, byte digitState) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, segmentState);
shiftOut(dataPin, clockPin, MSBFIRST, digitState);
digitalWrite(latchPin, HIGH);
}