// Pin definitions for segments (common to both displays)
int segPins[7] = {2, 3, 4, 5, 6, 7, 8}; // Pins connected to segments a-g
// Pin definitions for display control
int displayPins[2] = {9, 10}; // Pins for controlling common anode of the displays
// Lookup table for digits 0-9 using hexadecimal values for common anode displays
// Each entry in the array represents a digit with segment states a to g
byte digitLUT[10] =
{
0x3F, // 0: 00111111 - segments a, b, c, d, e, f are on (LOW to turn on)
0x06, // 1: 00000110 - segments b, c are on
0x5B, // 2: 01011011 - segments a, b, d, e, g are on
0x4F, // 3: 01001111 - segments a, b, c, d, g are on
0x66, // 4: 01100110 - segments b, c, f, g are on
0x6D, // 5: 01101101 - segments a, c, d, f, g are on
0x7D, // 6: 01111101 - segments a, c, d, e, f, g are on
0x07, // 7: 00000111 - segments a, b, c are on
0x7F, // 8: 01111111 - all segments are on
0x6F // 9: 01101111 - segments a, b, c, d, f, g are on
};
// Function to display a digit using the hexadecimal lookup table
void displayDigit(int number, int displayIndex) {
// Retrieve the hexadecimal pattern from the lookup table
byte pattern = digitLUT[number];
// Set the segment pins according to the binary pattern
for (int i = 0; i < 7; i++) {
digitalWrite(segPins[i], (pattern >> i) & 0x01 ? LOW : HIGH);
}
// Enable the selected display
digitalWrite(displayPins[displayIndex], LOW);
delay(5); // Small delay to allow the display to show the number
digitalWrite(displayPins[displayIndex], HIGH); // Disable the display
}
void setup()
{
// Set segment pins as output
for (int i = 0; i < 7; i++) {
pinMode(segPins[i], OUTPUT);
}
// Set display control pins as output
for (int i = 0; i < 2; i++) {
pinMode(displayPins[i], OUTPUT);
digitalWrite(displayPins[i], HIGH); // Initially turn off all displays
}
}
void loop() {
// Loop through digits 0 to 9
for (int i = 0; i < 10; i++) {
// Display the digit on the first display
displayDigit(i, 0);
// Display the digit on the second display (same for demonstration)
displayDigit(i, 1);
delay(1000); // Delay before the next cycle
}
}