Category Archives: ARDUINO

Countdown timer with Arduino

The need arose a construction of countdown timer. My first thought was to create something, using a NIDRUINO (equivalent of Arduino UNO) that existed in my stock…
To be able to have the adjustment and display parameters I use the module: LCD 1602  2×16 which is accompanied by a number of push buttons for easy management of this where I wanted to construct. Unfortunately, this module does not directly output analog and digital ports so add to the standby holes, single row breakaway male pins like that: http://www.ebay.com/itm/10pc-black-40pin-2-54mm-Single-Row-Breakaway-Male-Pin-Header-for-Arduino-uno-R3-/281773614793? to be able easily get some of the available ports (A0,A1,A2,A3,A4,D2,D3).

The LCD 1602 module use the following analog and digital ports:

Analog 0 Buttons (select, up, right, down and left)
Digital 4 DB4
Digital 5 DB5
Digital 6 DB6
Digital 7 DB7
Digital 8 RS (Data or Signal Display Selection)
Digital 9 Enable
Digital 10 Backlit Control
HIGH = Backlight on
LOW = Backlight off
Use PWM signal to control brightness

And the code for the function as a countdown timer…
I use one buzzer (YL-44) in digital port 2 for sound alarm

// Countdown Timer
// Vlassis Christodoulopoulos 2017

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);   // ports used from LCD panel
int lcd_key = 0;
int adc_key_in = 0;
int backlight = 10;
long tim = 0;
long x = 0;
long s;
long thou = 0;
long tmin = 0;
long tsec = 0;
int ct = 0; //change tag (ct) 1 for Hours, 2 for min, and 3 for sec
int buzzer = 2;
int bl = 1;
int ot = 2;  //default 2 seconds
int otf = 0;  // flag for editing ON TIME time

#define RIGHT 0
#define UP 1
#define DOWN 2
#define LEFT 3
#define SELECT 4
#define NONE 5

int read_LCD_buttons()  // read the buttons
{
adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return NONE; // We make this the 1st option for speed reasons since it will be the most likely result
if (adc_key_in < 50) return RIGHT;
if (adc_key_in < 250) return UP;
if (adc_key_in < 450) return DOWN;
if (adc_key_in < 650) return LEFT;
if (adc_key_in < 850) return SELECT;

return NONE;  // when all others fail, return this…
}

void buzzering(int t, int l)
{
for (int to = (t / (2 * l)); to >= 0; –to) // length loop 2*l milliseconds
{
digitalWrite(buzzer, LOW);
delay(l);
digitalWrite(buzzer, HIGH);
delay(l);
}
}

void setup()
{
pinMode(buzzer, OUTPUT);
pinMode(backlight, OUTPUT);
digitalWrite(buzzer, HIGH);
digitalWrite(backlight, HIGH);
lcd.begin(16, 2); // start the library
lcd.setCursor(0, 0);
lcd.print (“Ver4 Vlassis (c)”);
lcd.setCursor(0, 1);
lcd.print (“Initialising “);
for (int x = 1; x <= 4; x++)
{
delay(1000);
lcd.print(“.”);
}
lcd.setCursor(0, 0);
lcd.print (” “);
lcd.setCursor(0, 1);
lcd.print (” “);
}

void loop()
{
lcd_key = read_LCD_buttons();  // read the buttons
delay(100);
switch (lcd_key)  // depending on which button was pushed, we perform an action
{
case RIGHT:
{
otf = 1;
lcd.setCursor(0, 1);
lcd.print(“ON TIME “);
lcd.setCursor(10, 1);
lcd.print(ot);
lcd.print(“s”);
break;
}
case LEFT:
{
otf = 0;
if (ct > 3) {
ct = 1;
} else {
++ct;
}
lcd.setCursor(0, 1);
if (ct == 1 )
{
lcd.print(“Change HOURS “);
delay(100);
}
if (ct == 2 )
{
lcd.print(“Change MINUTES “);
delay(100);
}
if (ct == 3 )
{
lcd.print(“Change SECONDS “);
delay(100);
}
break;
}
case UP:
{
if (otf == 1)
{
delay(100);
++ot;
lcd.setCursor(10, 1);
lcd.print(ot);
lcd.print(“s “);

}
else
{
if (ct == 1)
{
++thou;
}
if ( thou > 99) {
thou = 0;
}
if (ct == 2)
{
++tmin;
}
if ( tmin > 59) {
tmin = 0;
}
if (ct == 3)
{
++tsec;
}
if ( tsec > 59) {
tsec = 0;
}

tim = thou * 3600 + tmin * 60 + tsec;
}
break;
}
case DOWN:
{
if (otf == 1)
{
delay(100);
–ot;
lcd.setCursor(10, 1);
lcd.print(ot);
lcd.print(“s “);
}
else
{
if (ct == 1)
{
–thou;
}
if ( thou < 0) {
thou = 99;
}
if (ct == 2)
{
–tmin;
}
if ( tmin < 0) {
tmin = 59;
}
if (ct == 3)
{
–tsec;
}
if ( tsec < 0) {
tsec = 59;
}
tim = thou * 3600 + tmin * 60 + tsec;
}
break;
}
case SELECT:
{
lcd.setCursor(0, 0);
analogWrite(backlight, 30);  // Change background light to minimum for countdown mode…
lcd.print(“COUNTDOWN… “);
lcd.setCursor(0, 1);
lcd.print(” “);
for (long x = tim; x > 0; x–)
{
lcd.setCursor(0, 1);
s = x / 3600;
lcd.print (s);
lcd.print (“h “);
s = x – (s * 3600);
lcd.print (s / 60);
lcd.print (“m “);
lcd.print (s % 60 );
lcd.print (“s “);
delay(996);  // Change this value for time base calibration purpose
}
lcd.setCursor(0, 1);
lcd.print(“BEEP… “);
buzzering (ot * 1000, 50);  // sec * milliseconds
analogWrite(backlight, 1200);  // Change background light to maximum for edit mode…
lcd.setCursor(0, 1);
lcd.print(” “);
lcd.setCursor(0, 0);
lcd.print(” “);
}
case NONE:
{
lcd.setCursor(0, 0);
lcd.print(“TIME”);
lcd.setCursor(5, 0);
lcd.print(thou);
lcd.print(“h “);
lcd.print(tmin);
lcd.print(“m “);
lcd.print(tsec);
lcd.print(“s”);
lcd.print(” “);
break;
}
}

}

The code is not the best (optimized)… but can be a basis for making many improvements and additions!





Push buttons
Left  =  (edit mode) -> change hours -> change minutes -> change seconds
Up  = Increase value
Down  =  Decrease value
Right  =  Change activation time value with Up, Down buttons
Rst  =  Reset
Select  =  Go to countdown mode