OVERVIEW
There is something about Countdown Timers that reminds me of many scenes in movies, and when I see one I can’t help to think about the tv series 24, or Mission Impossible.
In this tutorial we will use a 8 digits 7 segment module to display the countdown, a buzzer to make the tick sound and a button to start the countdown.
The code below will work for up to a 24 hours countdown.
PARTS USED
8 Digits 7 Segment Module
Passive Buzzer Module
Arduino UNO
These are Amazon affiliate links...
They don't cost you anything and it helps me keep the lights on
if you buy something on Amazon. Thank you!
CONNECTIONS
The Button is connected to Pin 5.
We connect the Passive Buzzer Module to Pin 3.
And the 7 segment module is connected to Pin 10 (DIO), Pin 11 (SCK) and Pin12 (RCK).
THE CODE
The hardest part of this tutorial is to format the Countdown value so that it represent time and not just a number counting down.
For example if we start with this number: 1640155
The way we breakdown the number in our code is : HHMMSSS
So our number represent: 16 hours, 40 minutes 15 seconds and 5 tenths of a second.
So if we start decreasing the number, at some point we will get to this number:
1640003 – 1640002 – 1640001 – 1640000 – 1639999
Now 163999 doesn’t represent time: 16 hours, 39 minutes, 99 seconds and 9 tenths of a second.
So we need to detect these incorrect 9’s in our code to change them to a correct time number.
We can do this by first converting our number to a string, and then detect those 9’s at a certain position and then decrease our number accordingly.
For example: 1639999 – 400 = 1639599.
1639599 converts to: 16 hours, 39 minutes, 59 seconds and 9 tenths of a second, which is correct.
Now granted the code below is a little more complicated than in our previous tutorials, but if you go line by line you should be able to graps the logic behind it.
As always please watch our tutorial video for more information and to better understand the process.
We use the passive buzzer to make the ticking sound at each second, and the alarm sound when the countdown reaches zero.
The button is used to get the countdown started.
As always please watch our Tutorial video for more information.
//Countdown Timer Sketch
#define LATCH 12 //pin 12 connect to RCK
#define CLOCK 11 //pin 11 connect to SCK
#define DATA 10 //pin 10 connect to DIO
#define BUZZ 3 //pin 3 connect to Buzzer
#define BUTTON 5 //pin 5 connect to Button
#define LEFT 0 // define the value for left justify
#define RIGHT 1 // define the value for right justify
int buzztrig = 9; //Trigger counter for Buzzer
String mystring; //Save Counter Value to a String
unsigned long value = 2000000; // Countdown value: HHMMSSS
// array to activate particular digit on the 8x7segment module
// it is the common anode of 7 segment
byte anode[8] = { 0b10000000, //digit 1 from right
0b01000000, //digit 2 from right
0b00100000, //digit 3 from right
0b00010000, //digit 4 from right
0b00001000, //digit 5 from right
0b00000100, //digit 6 from right
0b00000010, //digit 7 from right
0b00000001 //digit 8 from right
};
//array for decimal number, it is the cathode.
//logic low will activate the particular segment
byte cathode[12] = {0b11000000, // 0
0b11111001, // 1
0b10100100, // 2
0b10110000, // 3
0b10011001, // 4
0b10010010, // 5
0b10000010, // 6
0b11111000, // 7
0b10000000, // 8
0b10010000, // 9
0b01111111, //dot
0b11111111 //blank
};
//fucntion to send the serial data out to two 74HC595 serial to parallel shift register and activate the 7 segment.
void display8x7segment(byte datapin, byte clockpin, byte latchpin, byte digit, byte number)
{
digitalWrite(latchpin, LOW);
shiftOut(datapin, clockpin, MSBFIRST, digit); // clears the right display
shiftOut(datapin, clockpin, MSBFIRST, number); // clears the left display
digitalWrite(latchpin, HIGH);
}
//function to display value on 8x7 segment display according to the justify state
void displayNumber8x7segment(byte justify, unsigned long value)
{
byte decimal[8] = {0};
value = value % 100000000; //ensure the value is within 8 digits only
decimal[7] = value / 10000000; //extract digit 7 from value
value = value % 10000000; //extract the rest of 7 digit value
decimal[6] = value / 1000000;
value = value % 1000000;
decimal[5] = value / 100000;
value = value % 100000;
decimal[4] = value / 10000;
value = value % 10000;
decimal[3] = value / 1000;
value = value % 1000;
decimal[2] = value / 100;
value = value % 100;
decimal[1] = value / 10;
decimal[0] = value % 10;
byte zero = 0;
if (justify == RIGHT)
{
for(byte e = 8; e > 0 ; e --)
{
if(zero == 0)
{
if(decimal[e-1] != 0)
{
display8x7segment(DATA, CLOCK, LATCH, anode[e-1], cathode[decimal[e-1]]);
zero = 1;
}
}
else display8x7segment(DATA, CLOCK, LATCH, anode[e-1], cathode[decimal[e-1]]);
delay(1);
}
}
else //if justify == left
{
byte d = 0;
for(byte e = 8; e > 0; e --)
{
if(zero == 0)
{
if(decimal[e-1] != 0)
{
display8x7segment(DATA, CLOCK, LATCH, anode[7], cathode[decimal[e-1]]);
zero = 1;
d ++;
delay(1);
}
}
else
{
display8x7segment(DATA, CLOCK, LATCH, anode[7-d], cathode[decimal[e-1]]);
d ++;
delay(1);
}
}
}
}
void setup() {
pinMode(LATCH, OUTPUT);
pinMode(CLOCK, OUTPUT);
pinMode(DATA, OUTPUT);
pinMode(BUZZ, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);
digitalWrite(LATCH, HIGH); //Wake Up Display
digitalWrite(BUZZ, LOW); //Buzzer Off
digitalWrite(BUTTON, HIGH); // Button Low
delay(1000); //delay for 1 second
}
void loop(){
//Wait for button press to start Countdown
while(digitalRead(BUTTON) == HIGH){
displayNumber8x7segment(RIGHT, value); //display the Countdown Start value
}
for (value; value != -1; value --)
{
for(byte i = 9; i > 0; i --) // Adjust i=x to speed up or slow time
{
String mystring = String(value); //Transform Counter Int to String
//Do time Offset
for (int z = 0; z < 5; z++){
if ( mystring.substring(z) == "99999" ) {
value = (value - 40000);
}
if ( mystring.substring(z) == "999" ) {
value = (value - 400);
}
}
displayNumber8x7segment(RIGHT, value); //display the value in right justify format
}
// Countdown is Finished? Sound Alarm
if (value == 0) {
for(byte k = 0; k < 11; k++)
{
for (int j = 0; j < 80; j++) // Buzzer frequency sound 1
{
digitalWrite (BUZZ, HIGH) ;// Buzzer On
delay (1) ;// Delay 1ms
digitalWrite (BUZZ, LOW) ;// Buzzer Off
delay (1) ;// delay 1ms
}
for (int j = 0; j < 100; j++) // Buzzer frequency sound 2
{
digitalWrite (BUZZ, HIGH) ;// Buzzer On
delay (2) ;// delay 2ms
digitalWrite (BUZZ, LOW) ;// Buzzer Off
delay (2) ;// delay 2ms
}
display8x7segment(DATA, CLOCK, LATCH, 0xff, cathode[0]); //activate all digit // Put ALL zeros
delay(300);
display8x7segment(DATA, CLOCK, LATCH, 0xff, cathode[11]); // Blank Display
delay(300);
}
}
//Countdown Tick sound
if (buzztrig == 0){
for (int i = 0; i < 5; i++) // Buzzer frequency sound
{
digitalWrite (BUZZ, HIGH) ;// Buzzer On
delay (2) ;// Delay 2ms
digitalWrite (BUZZ, LOW) ;// Buzzer Off
delay (1) ;// delay ms
}
buzztrig = 9; // Trigger for countdown sound
}
else {
buzztrig = buzztrig - 1;
}
}
}
TUTORIAL VIDEO
DOWNLOAD
Copy and paste the above code in the Arduino IDE to program your Arduino.
Komentarze