This project is about making a timer with an Arduino board!
That’s a quick explanation:
There are 4 buttons. With those buttons, you can Increase, Decrease and Reset and Start and Stop the Timer.
When the timer ends, a buzzer will make a beep and 2 LEDs will blink. Then you have to press the Stop button and it will stop making noise.
Let’s see what you will need for this project.
- 4 buttons
- 2 LEDs
- 1 LCD
- on-off switch button
- 1 9V battery
- some resistors
- 1 potentiometer
- 1 buzzer
- 3D models
Schematics
Code
#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);
}