Project – Timer

Project – Timer

A Timer is one of the most common things that everyone has in their house. You can use a Timer for a lot of reasons: while you are cooking, playing a board game or even while you are working out!…so why not build yourself your Arduino based Timer??

What do you need?

  • 4 buttons
  • 2 LEDs
  • 1 LCD
  • on-off switch button
  • 1 9V battery
  • some resistors
  • 1 potentiometer
  • 1 buzzer
  • 3D models

How It Works?

There are 4 buttons.

You can Increase and Decrease the timer, Start and Stop and with the last button you can Reset the timer.

When the timer ends, a buzzer will make a bip and 2 LEDs will blink. Then you have to press the Stop button and it will stop making noise.

Schematics

battery schematic
led schematic
lcd schematic
buzzer schematic
button schematics

Sketch

#include <LiquidCrystal.h>

const int rs = 1, en = 11, d4 = 10, d5 = 9, d6 = 8, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

const int  buttonPinIncrease = 2;
const int  buttonPinDecrease = 3;
const int  buttonPinStartStop = 4;
const int  buttonPinReset = 5;

const int ledPin = 13;

int buttonStateStartStop = 0;         // current state of the button
int lastButtonStateStartStop = 0;     // previous state of the button

int buttonStateDecrease = 0;         // current state of the button
int lastButtonStateDecrease = 0;     // previous state of the button

int buttonStateIncrease = 0;         // current state of the button
int lastButtonStateIncrease = 0;     // previous state of the button

const int buzzerPin = 6;

bool endTimer = false;               //used when the timer is finished
bool start=false;                    //used to start and stop the timer

long tMax= 300000;                   //max time in milliseconds (5 minutes) -> if you want to change it do the operation 1000 * 60 * nMinutes 
                                        //(1000 is 1 second in milliseconds, 60 are the seconds in a minute, nMinutes are the minutes you want as max time)
long temp=0;                         //used for the 1 second delay in the timer
long tempR=0;                        //time left until 0
long tempReset=0;                    //used to reset time to the initial value

void setup()
{
  lcd.begin(16, 2);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  pinMode(buzzerPin,OUTPUT);
  pinMode(buttonPinDecrease, INPUT);
  pinMode(buttonPinIncrease, INPUT);
  pinMode(buttonPinStartStop, INPUT);
  pinMode(buttonPinReset, INPUT);
  temp=millis();                      //search millis() on Arduino Reference if you don't know how it works
}

void loop()
{
  buttonStateStartStop = digitalRead(buttonPinStartStop);       //*button bounce*
  if (buttonStateStartStop != lastButtonStateStartStop)
  {
    if (buttonStateStartStop == HIGH)
    {
      start= !start;
    }
    delay(50);
  }
  lastButtonStateStartStop = buttonStateStartStop;
  
  if(!start)
  {
      buttonStateIncrease = digitalRead(buttonPinIncrease);     //*button bounce*
      if (buttonStateIncrease != lastButtonStateIncrease)
      {
        if (buttonStateIncrease == HIGH)
        {
          if(!(tempR>(tMax-20000)))                             //increase the timer, not more then tMax
          {
            tempR= tempR + 20000;
            tempReset= tempR;
            stampa(tempR);
          }
          else
          {
            stampa(tempR);
          }
        }
        delay(50);
      }
      lastButtonStateIncrease = buttonStateIncrease;
    
      buttonStateDecrease = digitalRead(buttonPinDecrease);     //*button bounce*
      if (buttonStateDecrease != lastButtonStateDecrease)
      {
        if (buttonStateDecrease == HIGH)
        {
          if(!(tempR<20000))                                    //decrease timer, not less than 00:00
          {
            tempR= tempR - 20000;
            tempReset= tempR;
            stampa(tempR);
          }
          else
          {
            stampa(tempR);
          }
        }
        delay(50);
      }
      lastButtonStateDecrease = buttonStateDecrease;
  }
  
  if(digitalRead(buttonPinReset))
  {
    tempR = tempReset;                                    //reset timer
  }
  
  if(millis()-temp>1000 && tempR>0 && start)              //timer function
  {
    /*if(digitalRead(buttonPinReset))
    {
      tempR = tempReset;
    }*/
    tempR= tempR-1000;
    stampa(tempR);
    temp=millis();
  }
  else if(!(tempR>0) && start)
  {    
    while(!endTimer)
    {
      tone(buzzerPin, 10000, 300);
      digitalWrite(ledPin, HIGH);
      delay(100);
      digitalWrite(ledPin, LOW);
      delay(100);
      buttonStateStartStop = digitalRead(buttonPinStartStop);             //*button bounce*
      if (buttonStateStartStop != lastButtonStateStartStop)                   //stop the buzzer
      {
        if (buttonStateStartStop == HIGH)
        {
          endTimer = true;
          start= false;
        }
        delay(50);
      }
      lastButtonStateStartStop = buttonStateStartStop;
     }
     endTimer = false;   
   }
}

void stampa(long timerTime)
{
  int seconds= timerTime/1000;
  int minutes= seconds/60;
  seconds= seconds - minutes*60;
  lcd.clear();
  lcd.setCursor(1,1);
  lcd.print(minutes);
  lcd.setCursor(3,1);
  lcd.print(":");
  lcd.setCursor(4,1);
  lcd.print(seconds);
}

Hope you will enjoy making your own timer with Arduino!!

Davide Busco

Davide Busco is an engineering student at Federico II University of Naples. He uses his passion and studies to create his projects. He loves be a maker and spread his knowledge. Moreover he has gained the second place in a national makers-tournament between Italian's Universities.

Leave a Reply

Your email address will not be published. Required fields are marked *

en_GBEnglish (UK)
it_ITItaliano en_GBEnglish (UK)
%d bloggers like this: