/*
Wokwi | questions
beans OP — 9/28/24 at 6:17 AM
need help from the program
https://wokwi.com/projects/410261587790308353
*/
const unsigned long INTERVAL = 1000;
// Define the pin connections for the 7-segment display
const int digitPins[] = {8, 11, 12, 2}; // Pins for digit 1, 2, 3, and 4
const int segmentPins[] = {9, 13, 4, 6, 7, 10, 3, 5}; // Pins for segments A, B, C, D, E, F, G, and DP
// Define the bit patterns for each digit (0-9)
const byte digitPatterns[] = {
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B10111110, // 6
B11100000, // 7
B11111110, // 8
B11110110, // 9
};
unsigned long prevTime = 0;
void setup() {
Serial.begin(115200);
// Initialize the digit pins as outputs
for (int i = 0; i < 4; i++) {
pinMode(digitPins[i], OUTPUT);
}
// Initialize the segment pins as outputs
for (int i = 0; i < 7; i++) {
pinMode(segmentPins[i], OUTPUT);
}
}
void loop() {
// Start the countdown from 99
int countdown = 99;
while (countdown >= 0) {
// Display the current countdown value
displayCountdown(countdown);
if (millis() - prevTime >= INTERVAL) {
prevTime = millis();
// Decrement the countdown value
countdown--;
//Serial.println(countdown);
}
// Wait for 1 second before updating the display again
//delay(1000);
}
}
void displayCountdown(int countdown) {
// Extract the individual digits from the countdown value
int digit1 = countdown / 1000;
int digit2 = (countdown % 1000) / 100;
int digit3 = (countdown % 100) / 10;
int digit4 = countdown % 10;
// Display each digit on the 7-segment display
displayDigit(digitPins[0], digit1);
displayDigit(digitPins[1], digit2);
displayDigit(digitPins[2], digit3);
displayDigit(digitPins[3], digit4);
}
void displayDigit(int digitPin, int digitValue) {
// Turn off all segments
for (int i = 0; i <= 7; i++) {
digitalWrite(segmentPins[i], HIGH);
}
// Turn on the segments for the current digit value
byte pattern = digitPatterns[digitValue];
for (int i = 0; i <= 7; i++) {
if (bitRead(pattern, i)) {
digitalWrite(segmentPins[7 - i], LOW); // bitRead is LSB first
}
}
// Turn on the digit pin
digitalWrite(digitPin, HIGH);
delay(5); // Hold the digit on for 5ms
digitalWrite(digitPin, LOW);
}