Components and supplies
1
Arduino Nano R3
1
Universal Passive Buzzer
3
Tactile Switch, Top Actuated
1
Youmile LED Display Module TM1637
Project description
Code
Countdown timer
arduino
1// Game Countdown Timer 2// After turning on, use the UP and DOWN buttons to 3// set the time in minutes and seconds for the time allowed, 4// hold button for repeated time changes 5// The set time will remain showing on the display 6// Press start a short tone will sound, "go" will be displayed 7// and the time will start counting down to 0 8// When 0 is reached a longer tone will sound and "End" displayed 9// for 5 seconds before resetting to the allocated time 10// for the next players go. 11 12// Include the library driver for display: 13#include <TM1637Display.h> 14 15// Define the connections pins for display 16#define CLK 6 17#define DIO 7 18 19// Define other pin connections 20#define UP_BUTTON 2 21#define DOWN_BUTTON 3 22#define START_BUTTON 4 23#define BUZZER 9 24 25int duration; // Duration in seconds 26 27// Create display object of type TM1637Display: 28TM1637Display display = TM1637Display(CLK, DIO); 29 30// Set the individual segments for the word displays: 31const uint8_t seg_end[] = { 32 SEG_A | SEG_D | SEG_E | SEG_F | SEG_G, // E 33 SEG_C | SEG_E | SEG_G, // n 34 SEG_B | SEG_C | SEG_D | SEG_E | SEG_G, // d 35 0x00 // All off 36}; 37 38const uint8_t seg_go[] = { 39 SEG_A | SEG_B | SEG_C | SEG_D | SEG_F | SEG_G,// g 40 SEG_C | SEG_D | SEG_E | SEG_G, // o 41 0x00, // All off 42 0x00 // All off 43}; 44 45void setup() { 46 pinMode(UP_BUTTON, INPUT_PULLUP); 47 pinMode(DOWN_BUTTON, INPUT_PULLUP); 48 pinMode(START_BUTTON, INPUT_PULLUP); 49 pinMode(BUZZER, OUTPUT); 50 duration = 30; // Default to 30 seconds 51 display.setBrightness(3); // 0 to 7 change if required 52 ShowTime(duration); 53} 54 55void loop() { 56 // Function will checks for time change buttons and only return 57 // when start button pressed 58 WaitForStart(); 59 // Start the duration timer 60 TimeDuration(); 61} 62 63void WaitForStart(){ 64 // Check for button presses every 0.15 seconds 65 while (digitalRead(START_BUTTON) == HIGH){ 66 // Check if up or down has been pressed 67 //if time > 60 then increment by 10 seconds 68 if (digitalRead(UP_BUTTON) == LOW){ 69 if (duration < 60){ 70 duration++; 71 } 72 else{ 73 duration += 10; 74 } 75 ShowTime(duration); 76 } 77 if (digitalRead(DOWN_BUTTON) == LOW){ 78 if (duration > 60){ 79 duration -= 10; 80 } 81 else{ 82 duration--; 83 } 84 ShowTime(duration); 85 } 86 delay(150); 87 } 88 // Start button has been pressed 89 tone(BUZZER, 1500, 100); 90 display.clear(); 91 display.setSegments(seg_go); 92} 93 94void TimeDuration(){ 95 // While loop will continue until time up 96 unsigned long startTime = millis(); 97 unsigned long timer = 1000ul * duration; 98 // Repeatedly check if time is up 99 while ((millis() - startTime) <= timer){ 100 // Calculate time elapsed in seconds 101 int elapsed = int((millis() - startTime)/1000); 102 // Only start to display countdown after 3 seconds 103 if ((millis() - startTime) > 3000){ 104 ShowTime(duration - elapsed); 105 } 106 } 107 // Time up 108 tone(BUZZER, 750, 250); 109 display.clear(); 110 display.setSegments(seg_end); 111 // Wait 5 seconds and reset display 112 delay(5000); 113 // Show duration for next player 114 ShowTime(duration); 115} 116 117void ShowTime(int value){ 118 static int lastTime; 119 // Update the display if time has changed 120 if (lastTime != value) { 121 lastTime = value; 122 int iMinutes = value / 60; 123 int iSeconds = value - (iMinutes * 60); 124 // Show on 4 digit display 125 uint16_t number = iMinutes * 100 + iSeconds; 126 display.showNumberDecEx(number, 0b01000000, true, 4, 0); 127 } 128}
Countdown timer
arduino
1// Game Countdown Timer 2// After turning on, use the UP and DOWN buttons 3 to 4// set the time in minutes and seconds for the time allowed, 5// hold 6 button for repeated time changes 7// The set time will remain showing on the display 8// 9 Press start a short tone will sound, "go" will be displayed 10// and the 11 time will start counting down to 0 12// When 0 is reached a longer tone will sound 13 and "End" displayed 14// for 5 seconds before resetting to the allocated 15 time 16// for the next players go. 17 18// Include the library driver for 19 display: 20#include <TM1637Display.h> 21 22// Define the connections pins for 23 display 24#define CLK 6 25#define DIO 7 26 27// Define other pin connections 28#define 29 UP_BUTTON 2 30#define DOWN_BUTTON 3 31#define START_BUTTON 4 32#define BUZZER 33 9 34 35int duration; // Duration in seconds 36 37// Create display object 38 of type TM1637Display: 39TM1637Display display = TM1637Display(CLK, DIO); 40 41// 42 Set the individual segments for the word displays: 43const uint8_t seg_end[] = 44 { 45 SEG_A | SEG_D | SEG_E | SEG_F | SEG_G, // E 46 SEG_C | SEG_E | 47 SEG_G, // n 48 SEG_B | SEG_C | SEG_D | SEG_E | SEG_G, 49 // d 50 0x00 // All off 51}; 52 53const 54 uint8_t seg_go[] = { 55 SEG_A | SEG_B | SEG_C | SEG_D | SEG_F | SEG_G,// g 56 57 SEG_C | SEG_D | SEG_E | SEG_G, // o 58 0x00, // 59 All off 60 0x00 // All off 61}; 62 63void 64 setup() { 65 pinMode(UP_BUTTON, INPUT_PULLUP); 66 pinMode(DOWN_BUTTON, INPUT_PULLUP); 67 68 pinMode(START_BUTTON, INPUT_PULLUP); 69 pinMode(BUZZER, OUTPUT); 70 duration 71 = 30; // Default to 30 seconds 72 display.setBrightness(3); // 73 0 to 7 change if required 74 ShowTime(duration); 75} 76 77void loop() { 78 79 // Function will checks for time change buttons and only return 80 // when 81 start button pressed 82 WaitForStart(); 83 // Start the duration timer 84 TimeDuration(); 85} 86 87void 88 WaitForStart(){ 89 // Check for button presses every 0.15 seconds 90 while (digitalRead(START_BUTTON) 91 == HIGH){ 92 // Check if up or down has been pressed 93 //if time > 60 then 94 increment by 10 seconds 95 if (digitalRead(UP_BUTTON) == LOW){ 96 if (duration 97 < 60){ 98 duration++; 99 } 100 else{ 101 duration += 102 10; 103 } 104 ShowTime(duration); 105 } 106 if (digitalRead(DOWN_BUTTON) 107 == LOW){ 108 if (duration > 60){ 109 duration -= 10; 110 } 111 112 else{ 113 duration--; 114 } 115 ShowTime(duration); 116 117 } 118 delay(150); 119 } 120 // Start button has been pressed 121 tone(BUZZER, 122 1500, 100); 123 display.clear(); 124 display.setSegments(seg_go); 125} 126 127void 128 TimeDuration(){ 129 // While loop will continue until time up 130 unsigned long 131 startTime = millis(); 132 unsigned long timer = 1000ul * duration; 133 // Repeatedly 134 check if time is up 135 while ((millis() - startTime) <= timer){ 136 // Calculate 137 time elapsed in seconds 138 int elapsed = int((millis() - startTime)/1000); 139 140 // Only start to display countdown after 3 seconds 141 if ((millis() - startTime) 142 > 3000){ 143 ShowTime(duration - elapsed); 144 } 145 } 146 // Time up 147 148 tone(BUZZER, 750, 250); 149 display.clear(); 150 display.setSegments(seg_end); 151 152 // Wait 5 seconds and reset display 153 delay(5000); 154 // Show duration for 155 next player 156 ShowTime(duration); 157} 158 159void ShowTime(int value){ 160 static 161 int lastTime; 162 // Update the display if time has changed 163 if (lastTime != 164 value) { 165 lastTime = value; 166 int iMinutes = value / 60; 167 int iSeconds 168 = value - (iMinutes * 60); 169 // Show on 4 digit display 170 uint16_t number 171 = iMinutes * 100 + iSeconds; 172 display.showNumberDecEx(number, 0b01000000, 173 true, 4, 0); 174 } 175}
Downloadable files
Countdown timer
Countdown timer

Comments
Only logged in users can leave comments