This is a countdown that uses three 7-Segment displays to countdown from any number between 99 to 1. This uses the digital pins from 13 to 4 on an Arduino Uno. The Pins 13, 12, & 11 are as a multiplexer to enable one 7-segment display at a time. While the rest of the pins are used for lighting each LED. Once the countdown reaches zero, each display blinks three times, then resets the counter.
Three 7-Segment Countdown Timer
//Setting up the time delay for counting down the milliseconds unsigned long startMillis; unsigned long currentMillis; const unsigned long period = 100;
//Setting the countdown numbers int millisecNum = 0; int countdownNumber = 15; int storedCountdownNum = countdownNumber;
//Setting up the pins for the Arduino const int enable_1 = 13; const int enable_2 = 12; const int enable_3 = 11; const int leftDP = 2, rightDB = 3, bottom = 4, bottomLeft = 5, bottomRight = 6, middle = 7, topLeft = 8, topRight = 9, top = 10;
void setup() { // put your setup code here, to run once: setupPins(); startMillis = millis(); //initial start time }
void setupPins() { for (int i = 2; i<11; data-preserve-html-node="true" i++) { pinMode(i, OUTPUT); digitalWrite(i, HIGH); }
for (int i = 11; i<14; data-preserve-html-node="true" i++) { pinMode(i, OUTPUT); digitalWrite(i, LOW); } }
//Displays a number from 0 to 9 void displayNum(int num) { resetDisplay(); switch (num){ case 0: digitalWrite(topLeft, LOW); digitalWrite(top, LOW); digitalWrite(topRight, LOW); digitalWrite(bottomLeft, LOW); digitalWrite(bottomRight, LOW); digitalWrite(bottom, LOW); break;
case 1:
digitalWrite(topRight, LOW);
digitalWrite(bottomRight, LOW);
break;
case 2:
digitalWrite(top, LOW);
digitalWrite(topRight, LOW);
digitalWrite(middle, LOW);
digitalWrite(bottomLeft, LOW);
digitalWrite(bottom, LOW);
break;
case 3:
digitalWrite(top, LOW);
digitalWrite(topRight, LOW);
digitalWrite(middle, LOW);
digitalWrite(bottomRight, LOW);
digitalWrite(bottom, LOW);
break;
case 4:
digitalWrite(topLeft, LOW);
digitalWrite(middle, LOW);
digitalWrite(topRight, LOW);
digitalWrite(bottomRight, LOW);
break;
case 5:
digitalWrite(top, LOW);
digitalWrite(topLeft, LOW);
digitalWrite(middle, LOW);
digitalWrite(bottomRight, LOW);
digitalWrite(bottom, LOW);
break;
case 6:
digitalWrite(top, LOW);
digitalWrite(topLeft, LOW);
digitalWrite(middle, LOW);
digitalWrite(bottomRight, LOW);
digitalWrite(bottom, LOW);
digitalWrite(bottomLeft, LOW);
break;
case 7:
digitalWrite(top, LOW);
digitalWrite(topRight, LOW);
digitalWrite(bottomRight, LOW);
break;
case 8:
digitalWrite(top, LOW);
digitalWrite(topLeft, LOW);
digitalWrite(topRight, LOW);
digitalWrite(middle, LOW);
digitalWrite(bottomRight, LOW);
digitalWrite(bottom, LOW);
digitalWrite(bottomLeft, LOW);
break;
case 9:
digitalWrite(top, LOW);
digitalWrite(topLeft, LOW);
digitalWrite(topRight, LOW);
digitalWrite(middle, LOW);
digitalWrite(bottomRight, LOW);
break;
default:
resetDisplay();
} }
//Resets the display by setting all of the pins to HIGH void resetDisplay() { for (int i=2; i<11; data-preserve-html-node="true" i++) { digitalWrite(i, HIGH); } }
//Not used. Testing for looping through each 7-segment display and displaying each number from 0 to 9 void loopThroughEachNumber() { for (int en=13; en>=11; en--) { digitalWrite(en, HIGH);
for (int i=0; i<10; i++) {
displayNum(i);
delay(500);
}
digitalWrite(en, LOW);
} }
//Loops through each 7-segment display while counting down to zero void countDownFrom(int num, int countdownNum) { int firstDigit = num / 10; int secondDigit = num % 10;
digitalWrite(enable_1, HIGH); displayNum(firstDigit); delay(10); digitalWrite(enable_1, LOW);
digitalWrite(enable_2, HIGH); displayNum(secondDigit); delay(10); digitalWrite(enable_2, LOW);
digitalWrite(enable_3, HIGH); displayNum(countdownNum); delay(10); digitalWrite(enable_3, LOW); }
//Flashes zeros when the program reaches zero void flashZeros() { int i = 0; digitalWrite(enable_1, HIGH); digitalWrite(enable_2, HIGH); digitalWrite(enable_3, HIGH); resetDisplay(); while(i<3) data-preserve-html-node="true" { displayNum(0); delay(100); resetDisplay(); delay(100); i++; }
displayNum(0); delay(3000); resetDisplay(); }
void loop() { // put your main code here, to run repeatedly: currentMillis = millis(); // Captures the current millisecons at the start of the loop
countDownFrom(countdownNumber, millisecNum);
if (currentMillis - startMillis >= period) { // Every millisecond, updated the display millisecNum--; countDownFrom(countdownNumber, millisecNum); startMillis = currentMillis; }
if (millisecNum == 0 && countdownNumber == 0) { // If the milliseconds and the countdownNumber equals zero, flash all zeros and reset the counter flashZeros(); countdownNumber = storedCountdownNum; millisecNum = 1; } else if (millisecNum == 0 && countdownNumber != 0) { millisecNum = 9; countdownNumber--; } }