Project tutorial
Game Turn Duration Timer

Game Turn Duration Timer © GPL3+

Simple timer to time your set duration for each go when playing a game. Countdown display and buzzer to signify end of turn.

  • 1,803 views
  • 0 comments
  • 6 respects

Components and supplies

Ard nano
Arduino Nano R3
Or and Arduino UNO
×1
19c7994 40
Tactile Switch, Top Actuated
×3
Youmile LED Display Module TM1637
×1
Universal Passive Buzzer
Any passive buzzer will do (Not sure if the link is a passive buzzer at is not working for me)
×1

About this project

This is a timer that can be used as a countdown timer for any purpose but was created to use when playing games. Once you set the turn duration, using the up and down buttons, simply press go when the turn starts and the timer will beep, the display will show "go" and the countdown will start. The countdown will display after 3 seconds. When the time is up, "End" will be displayed and a longer beep sounded. After 5 seconds the set duration will be redisplayed for the start of the next players turn.

The duration can be from a few seconds to 99 minutes and clearly it can be used as a timer for any purpose.

The up and down buttons can be held down to repeatedly change the time, above 60 seconds the increment is increased to 10 seconds.

Circuit

The circuit is straightforward.

The 3 momentary switches connect pins D2, D3 and D4 to ground when pressed. The pins are set to INPUT_PULLUP, hence are normally HIGH and go LOW when a button is pushed.

The buzzer is connected to pin 9.

The 4 digit LED display has 4 connections.

  • VCC to +5v
  • GND to GND
  • CLK to pin 6
  • DIO to pin 7

Display

I am using a 4 digit 7 segment LCD display with a TM1637 driver. The one I used was a Youmile DR-YM-174, but any make should work.

The program

The program uses a library to drive the 4 digit display. I am using the library TM1637 v.1.2.0 by Avishay Orpaz.

The constants seg_go and seg_end define the segments that light to display the words "go" and "end".

The duration of the timer is held in a variable called "duration" and is set to 30 seconds by default in setup.

ShowTime() takes a single parameter of the time in seconds to display. If more than a second has elapsed since the last call, the time is displayed using the call display.showNumberDecEx(number, 0b01000000, true, 4, 0). The second parameter turns on the dividing colon and the third displays leading zeros.

WaitForStart() contains a while loop that repeats until the start button is pressed. In the repeat the up and down buttons are checked and, if pressed, the duration changed. When the start button is pressed, "go" is displayed by the call to display.setSegments(seg_go) and the busser sounded.

TimeDuration() is then called which calls the time display in a loop until the time is up, after which it displays "End" and sounds the buzzer.

After a 5 second pause, the process repeats.

Code

Countdown timerArduino
// Game Countdown Timer
// After turning on, use the UP and DOWN buttons to
//    set the time in minutes and seconds for the time allowed,
//    hold button for repeated time changes
// The set time will remain showing on the display
// Press start a short tone will sound, "go" will be displayed 
//    and the time will start counting down to 0
// When 0 is reached a longer tone will sound and "End" displayed 
//    for 5 seconds before resetting to the allocated time 
//    for the next players go.

// Include the library driver for display:
#include <TM1637Display.h>

// Define the connections pins for display
#define CLK 6
#define DIO 7

// Define other pin connections
#define UP_BUTTON 2
#define DOWN_BUTTON 3
#define START_BUTTON 4
#define BUZZER 9

int duration;      // Duration in seconds

// Create display object of type TM1637Display:
TM1637Display display = TM1637Display(CLK, DIO);

// Set the individual segments for the word displays:
const uint8_t seg_end[] = {
  SEG_A | SEG_D | SEG_E | SEG_F | SEG_G,         // E
  SEG_C | SEG_E | SEG_G,                         // n 
  SEG_B | SEG_C | SEG_D | SEG_E | SEG_G,          // d
  0x00                                           // All off
};

const uint8_t seg_go[] = {
  SEG_A | SEG_B | SEG_C | SEG_D | SEG_F | SEG_G,// g
  SEG_C | SEG_D | SEG_E | SEG_G,                  // o
  0x00,                                           // All off
  0x00                                           // All off
};

void setup() {
  pinMode(UP_BUTTON, INPUT_PULLUP);
  pinMode(DOWN_BUTTON, INPUT_PULLUP);
  pinMode(START_BUTTON, INPUT_PULLUP);
  pinMode(BUZZER, OUTPUT);
  duration = 30;              // Default to 30 seconds
  display.setBrightness(3);   // 0 to 7 change if required
  ShowTime(duration);
}

void loop() {
  // Function will checks for time change buttons and only return 
  // when start button pressed
  WaitForStart();
  // Start the duration timer
  TimeDuration();
}

void WaitForStart(){
  // Check for button presses every 0.15 seconds
  while (digitalRead(START_BUTTON) == HIGH){
    // Check if up or down has been pressed
    //if time > 60 then increment by 10 seconds
    if (digitalRead(UP_BUTTON) == LOW){
      if (duration < 60){
        duration++;
       }
       else{
        duration += 10;
       }
      ShowTime(duration);
    }
    if (digitalRead(DOWN_BUTTON) == LOW){
      if (duration > 60){
        duration -= 10;
       }
       else{
        duration--;
       }
      ShowTime(duration);
    }
    delay(150);
  }
  // Start button has been pressed
  tone(BUZZER, 1500, 100);
  display.clear();
  display.setSegments(seg_go);
}

void TimeDuration(){
  // While loop will continue until time up
  unsigned long startTime = millis();
  unsigned long timer = 1000ul * duration;
  // Repeatedly check if time is up
  while ((millis() - startTime) <= timer){
    // Calculate time elapsed in seconds
    int elapsed = int((millis() - startTime)/1000);
    // Only start to display countdown after 3 seconds
    if ((millis() - startTime) > 3000){
      ShowTime(duration - elapsed);
    }
  }
  // Time up
  tone(BUZZER, 750, 250);
  display.clear();
  display.setSegments(seg_end);
  // Wait 5 seconds and reset display
  delay(5000);
  // Show duration for next player
  ShowTime(duration);
}

void ShowTime(int value){
  static int lastTime;
  // Update the display if time has changed
  if (lastTime != value) {
    lastTime = value;
    int iMinutes = value / 60;
    int iSeconds = value - (iMinutes * 60);
    // Show on 4 digit display
    uint16_t number = iMinutes * 100 + iSeconds;
    display.showNumberDecEx(number, 0b01000000, true, 4, 0);    
  }
}

Schematics

Countdown timer
Countdown timer df4rrq2prp

Comments

Similar projects you might like

Arduino Pocket Game Console + A-Maze - Maze Game

Project tutorial by Alojz Jakob

  • 19,343 views
  • 9 comments
  • 47 respects

Laser Game

Project tutorial by ardutronic

  • 4,452 views
  • 0 comments
  • 12 respects

LED Roulette Game

Project tutorial by Arduino “having11” Guy

  • 21,117 views
  • 9 comments
  • 28 respects

Pixel Chaser Game

Project tutorial by Unexpected Maker

  • 11,456 views
  • 7 comments
  • 39 respects

Arduino Gyroscope Game with MPU-6050

Project tutorial by Mirko Pavleski

  • 7,493 views
  • 5 comments
  • 20 respects

Arduino Game

Project tutorial by RoboCircuits

  • 4,444 views
  • 0 comments
  • 13 respects
Add projectSign up / Login