// Define segment values for digits 0-9 on a 7-segment display
const byte segValue[10][7] = {
{0, 0, 0, 0, 0, 0, 1}, // 0
{1, 0, 0, 1, 1, 1, 1}, // 1
{0, 0, 1, 0, 0, 1, 0}, // 2
{0, 0, 0, 0, 1, 1, 0}, // 3
{1, 0, 0, 1, 1, 0, 0}, // 4
{0, 1, 0, 0, 1, 0, 0}, // 5
{0, 1, 0, 0, 0, 0, 0}, // 6
{0, 0, 0, 1, 1, 1, 1}, // 7
{0, 0, 0, 0, 0, 0, 0}, // 8
{0, 0, 0, 0, 1, 0, 0} // 9
};
// Pin numbers for the segments and digits
const byte segmentPins[7] = {2, 3, 4, 5, 6, 7, 8};
const byte digitPins[3] = {10, 11, 12};
void setup() {
// Initialize segment pins
for (int i = 0; i < 7; i++)
{
pinMode(segmentPins[i], OUTPUT);
}
// Clear all segments to be off initially
clearSegments();
// Initialize digit pins
for (int i = 0; i < 3; i++)
{
pinMode(digitPins[i], OUTPUT);
digitalWrite(digitPins[i], LOW); // Start with all digits off
}
}
void loop() {
// Count from 1 to 3
for (int count = 1; count <= 3; count++)
{
displaySingleNumber(count); // Display the current number
delay(300); // Delay for 1 second before moving to the next number
}
}
// Function to display a single digit at a time
void displaySingleNumber(int number)
{
// Loop through each digit position
for (int i = 0; i < 3; i++) {
clearSegments(); // Clear all segments first
digitalWrite(digitPins[i], HIGH); // Activate the current digit
// Display the current number on the digit
if (i == 0 && number == 1) { // Display '1' on left digit
setSegment(number);
}
else if (i == 1 && number == 2) { // Display '2' on middle digit
setSegment(number);
}
else if (i == 2 && number == 3) { // Display '3' on right digit
setSegment(number);
}
delay(300); // Hold the digit on for 1 second
digitalWrite(digitPins[i], LOW); // Turn off the digit
}
}
// Function to set the segment values for the current number
void setSegment(int number)
{
for (int j = 0; j < 7; j++) {
digitalWrite(segmentPins[j], segValue[number][j]); // Set segment values for the number
}
}
// Function to clear all segments
void clearSegments()
{
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], HIGH); // Turn all segments off
}
}