const int segmentPins[] = {2, 3, 4, 5, 6, 7, 8}; // GPIO pins connected to segments a-g
//the GPIO pins should be set to LOW to turn the segment on and HIGH to turn it off, because the display uses common anode
const byte numbers[10][7] = {
{0, 0, 0, 0, 0, 0, 1}, // 0 ABCDEF
{1, 0, 0, 1, 1, 1, 1}, // 1 BC
{0, 0, 1, 0, 0, 1, 0}, // 2 ABDEG
{0, 0, 0, 0, 1, 1, 0}, // 3 ABCDG
{1, 0, 0, 1, 1, 0, 0}, // 4 BCFG
{0, 1, 0, 0, 1, 0, 0}, // 5 ACDFG
{0, 1, 0, 0, 0, 0, 0}, // 6 ACDEFG
{0, 0, 0, 1, 1, 1, 1}, // 7 ABC
{0, 0, 0, 0, 0, 0, 0}, // 8 ABCDEFG
{0, 0, 0, 0, 1, 0, 0} // 9 ABCDFG
};
void setup() {
// Initialize all segment pins as OUTPUT
for (int i = 0; i < 7; i++) {
pinMode(segmentPins[i], OUTPUT);
digitalWrite(segmentPins[i], HIGH); // Ensure all segments are OFF initially
}
}
void loop() {
// Display numbers 0 to 9 on the 7-segment display
for (int num = 0; num < 10; num++) {
displayNumber(num);
delay(1000); // Delay 1 second before the next number
}
}
void displayNumber(int num) {
// Set the segment pins according to the number
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], numbers[num][i] == 0 ? LOW : HIGH); // Common Anode logic
//If the value in numbers[num][i] is 0, it sets the segment pin to LOW (turning it ON).
//If the value is 1, it sets the pin to HIGH (turning it OFF)
}
}