Hello, I has some problem my Project. I used lcd 16x2 and arduino uno to do clock and time countdown.
I want to my time is start countdown again when it has finished for the first time and start with a value that I set it for the first time. But it has nothing when it finished countdown in the first time,
just 00 : 00 : 00. I try to set some variable for use it to do a loop but it’s not success. please help or guide me to do this, thank you sir.
Here is my code
#include <LiquidCrystal.h> //these are libraries that need to be downloaded from the Arduino Playground website (LiquidCrystal.h should come pre-downloaded with
#include <Time.h> //the Arduino): http://playground.arduino.cc/Code/Time
#include <Wire.h>
int buttonPin1 = 10;
int buttonPin2 = 9;
int buttonPin3 = 8;
int buttonPin4 = 6 ;
int buttonPin5 = 13;
int x, y, z,a ;
int tSeconds=10, tMinutes=0, hours=0; //this line, along with another line in void timerFunction(), is where you can adjust the amount of time that is counted down in //the timer function
int centiseconds=0, sSeconds=0, sMinutes=0;
int button1Counter = 0; //this section initializes the button counters, which is how the buttons can have multiple operations within the same function - more info:
int button1State = 0; // http://arduino.cc/en/Tutorial/ButtonStateChange
int lastButton1State = 0;
int button2Counter = 0;
int button2State = 0;
int lastButton2State = 0;
#include<Servo.h>
Servo myservo;
LiquidCrystal lcd(12,11,5,4,3,2);
void setup()
{
lcd.begin(16, 2);
setTime(14,39,00,2,07,2015); // this is how you can set the time for the clock function (hour,minute,second,day,month,year)
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(buttonPin4, INPUT_PULLUP);
pinMode(buttonPin5, INPUT_PULLUP);
myservo.attach(7);
myservo.write(0);
}
void loop()
{
button1State = digitalRead(buttonPin1); //this section gives the "Mode" button the ability to have three seperate operations
if(button1State != lastButton1State)
{
if(button1State = HIGH)
{
button1Counter++;
button2Counter = 1;
delay(200);
}
}
lastButton1State = button1State;
if(button1Counter > 2)
{
button1Counter = 1;
}
button2State = digitalRead(buttonPin2); //similarly, this section allows the "Start/Stop" button the ability to have two seperate functions
if(button2State != lastButton2State)
{
if(button2State = HIGH)
{
button2Counter++;
delay (200);
}
}
lastButton2State = button2State;
if(button2Counter > 2)
{
button2Counter = 1;
}
switch(button1Counter) //switch case is really nice for changing the clock between its three functions
{
case 1: //the clock defaults to clockFunction()
lcd.clear();
lcd.setCursor(2, 1);
lcd.print(":");
lcd.setCursor(5, 1);
lcd.print(":");
clockFunction();
break;
case 2: //if the "Mode" button is pressed, clock switches to timerFunction()
lcd.clear();
lcd.setCursor(2, 1);
lcd.print(":");
lcd.setCursor(5, 1);
lcd.print(":");
timerFunction();
break;
}
}
void clockFunction() //this is the time clock function; it works using the Time.h library
{
lcd.setCursor(0, 0);
printDigits(month());
lcd.setCursor(2, 0);
lcd.print("/");
lcd.setCursor(3, 0);
printDigits(day());
lcd.setCursor(5, 0);
lcd.print("/");
lcd.setCursor(6, 0);
lcd.print(year());
lcd.setCursor(0, 1);
lcd.print(hour());
lcd.setCursor(3, 1);
printDigits(minute());
lcd.setCursor(6, 1);
printDigits(second());
delay(100);
}
void timerFunction() //the timer function was made with the help of this post: http://pastebin.com/f57045830
{
if(button2Counter == 2 || a == 1 ) //if "Start/Stop" is pressed, the timer counts down
{
static unsigned long lastTick = 0;
if (tSeconds > 0)
{
if (millis() - lastTick >= 1000)
{
lastTick = millis();
tSeconds--;
lcdOutput();
}
}
if (tMinutes > 0)
{
if (tSeconds <= 0)
{
tMinutes--;
tSeconds = 60;
}
}
if (hours > 0)
{
if (tMinutes <= 0)
{
hours--;
tMinutes = 60;
}
}
a=0 ;
}
else //if "Start/Stop" is unpressed or pressed a second time, display the current time, but don't count down
{
lcdOutput();
}
if(hours == 00 && tMinutes == 00 && tSeconds == 00 && a==0) //when timer ends, the alarm goes off
{
myservo.write(300);
delay(500);
myservo.write(0);
a=1;
delay(600);
button2Counter = 1;
while(digitalRead(buttonPin3) == HIGH) //the alarm will only go off until "Restart" is pressed
{
lcd.setCursor(0, 1);
lcd.print("00:00:00");
}
}
if(digitalRead(buttonPin3) == LOW) //when "Restart" is pressed, the timer resets
{
hours++;
x = hours;
lcdOutput();
}
if(digitalRead(buttonPin5) == LOW) //
{
tMinutes++;
y = tMinutes;
lcdOutput();
}
if(digitalRead(buttonPin4) == LOW && button2Counter == 1) //resets the timer when "Restart" button is pressed, as long as the timer is NOT running
{
hours = 0 ;//this part also must be changed when the timer is altered, to match the initial time
tMinutes = 0;
tSeconds = 1;
z = tSeconds;
lcdOutput();
}
if (hours > 12)
{
delay(100);
hours = 0 ;
}
}
void lcdOutput() //this is just used to display the timer on the LCD
{
lcd.setCursor(0, 1);
printDigits(hours);
lcd.setCursor(3, 1);
printDigits(tMinutes);
lcd.setCursor(6, 1);
printDigits(tSeconds);
delay(100);
}
void printDigits(int digits) //this void function is really useful; it adds a "0" to the beginning of the number, so that 5 minutes is displayed as "00:05:00", rather than "00:5 :00"
{
if(digits < 10)
{
lcd.print("0");
lcd.print(digits);
}
else
{
lcd.print(digits);
}
}