How to Create a Timer With Arduino
by dziubym in Circuits > Arduino
4928 Views, 2 Favorites, 0 Comments
How to Create a Timer With Arduino
In this project I will build timer device that counts down from a specified time interval
Supplies
We need following parts to build this project:
- Arduino Nano
- Buzzer
- 2x 1k Ohm resistors
- Oled I2C SSD1306 display
- 2 push buttons
Project Objectives
We wanto to create the device that would work in the following way.
- When you power it on the OLED display turns on and shows 0 min
- Each time we press the first button one minute is added to that time
- When the right amount of time is selected with the press of the second button the countdown is started one sec at the time and it is displayed on the OLED screen
- when we go all the way back to zero the buzzer off off together with the ringing bell animation on the oled display
- when the second button is pressed while the alarm is on the buzzer goes off and the oled screen again displays zero minutes and we are ready to set up a new time to come down from
Connectivity
Let's discuss connectivity. As always we are connecting five volts and ground arduino pins to ground and 5v sections of the breadboard. Then we connect 5 volt to left pins of both push buttons the other pin of the first push button goes to digital pin 2 and via pull up resistor to ground. Same for the second push button it goes to digital pin 3 and via pull up resistor to ground.Then the buzzer is connected to pin 12. And OLED display is connected like shown in the diagram.
Preparing Ringing Bell OLED Animation
I provided instruction how to create OLED animations in another instructable
https://www.instructables.com/Create-Animation-on-OLED-Display-Controlled-by-Ard/
Arduino Code
First we need few declarations
We need to attach two libraries for spi communication
#include <SPI.h> #include <Wire.h>
Two more libraries to help us with controlling oled display
#include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h>
Then we declare all the display dimensions
#define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels
buzzer pin
#define Buzzer 12
and all 5 oled display pins
#define OLED_MOSI 5 #define OLED_CLK 4 #define OLED_DC 7 #define OLED_CS 8 #define OLED_RESET 6
and finally declaring the oled display itself
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
We need to have few variables
int minutes=0; // State values 0 - setup 1- countdown int State=0; long Timestamp_Button_Pressed;
- minutes - would hold the number of minutes we would be counting down from
- timestamp_button_pressed - variable is the variable i used to prevent single press of the button being recognized as multiple press of the button i will explain the specific use of this variable later
- state - when it is 0 that means that we are in a time setup mode when it changes to 1 it means that we are in a countdown mode
When we come down to 0 and the alarm goes off, the state variable is also equal to 1.
In the setup function we define the interrupt on digital pin 2 linked to the first push button
attachInterrupt(digitalPinToInterrupt(2), Press_A_Button,RISING);
Each time this button is pressed following function is executed
void Press_A_Button(){ if (millis() - Timestamp_Button_Pressed>200){ if (State==0) minutes = minutes+1; Timestamp_Button_Pressed=millis(); } }
In it we first perform the check if 200 milliseconds passed since the button was pressed the last time.
The code will only be executed if that condition is fulfilled. If it is we just increase minutes variable by one minute and update Timestamp_Button_Pressed variable with current value of millis.
We also have an interrupt defined for the pushbutton connected to digital pin 3
attachInterrupt(digitalPinToInterrupt(3), Press_B_Button,RISING);
Each time this button is pressed the following function is executed.
void Press_B_Button(){ if (millis() - Timestamp_Button_Pressed>200){ if (State==0){ Countdown_start=millis(); State=1; } else{ State=0; minutes =0; } } }
When if we are in the setup mode pressing this button causes the countdown to start. We change the state to 1 (countdown).
If we were in countdown state pressing this button stops the countdown or stops the already running alarm and resets the state to 0 (setup mode)
In the loop function we have a section of code executed if the state variable is zero, that means we are still in the time setup mode. Here we make sure the low signal is sent to the buzzer pin so the alarm is off and then we set up the font size font color and cursor position to output updated value of minutes variable on oled display
if (State==0 ){ digitalWrite(Buzzer,LOW); display.clearDisplay(); display.setTextSize(2); display.setCursor(5,0); display.setTextColor(SSD1306_WHITE); display.println((String)minutes+" min"); display.display(); }
if the state is 1 that means we are in the countdown mode. So first we check if we have counted down to zero. If not we display the remaining time.
if(State==1){ if(minutes*60000 >(millis()-Countdown_start)){ display.clearDisplay(); display.setCursor(5,0); display.setTextSize(4); display.setTextColor(SSD1306_WHITE); display.println(TimeLeft((minutes*60000-(millis()-Countdown_start)))); display.display(); } else { Ring_Bell(Current_Frame); Current_Frame=Current_Frame+1; if (Current_Frame==5) Current_Frame=1; } }
If this countdown was completed, we execute Ring_bell function which displays the current frame of the animation. Also for frames 1 2 and 3 it sends high signal to buzzer pin to sound the alarm and for frame 4 it turns it off so the signal is not continuous but intermittent.When the ring bell function is executed we increase current frame variable so in the next execution of main loop we will display next frame in line.
You can check the full code here
Conclusion
This is the end of this instructable. Thanks for spending you time going over it.
Please check the full tutorial on YT where you can observe the final result.
Hope you would find this instructable useful:)