#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
const int Up_buttonPin = 2;
const int Down_buttonPin = 3;
const int Start_buttonPin = 4;
int buttonPushCounter = 0;
int up_buttonState = 0;
int up_lastButtonState = 0;
int down_buttonState = 0;
int down_lastButtonState = 0;
int start_buttonState = 0;
int start_lastButtonState = 0;
bool bPress = false;
unsigned long seconds = 1000L;
unsigned long minutes = seconds * 60;
unsigned long hours = minutes * 60;
const int REL1 = 8;
const int REL2 = 9;
void setup()
{
Serial.begin(9600);
pinMode( Up_buttonPin , INPUT_PULLUP);
pinMode( Down_buttonPin , INPUT_PULLUP);
pinMode(REL1, OUTPUT);
pinMode(REL2, OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("CountDown Timer:");
lcd.setCursor(7,1);
lcd.print(buttonPushCounter);
}
void loop()
{
checkUp();
checkDown();
checktimer();
if( bPress){
bPress = false;
lcd.setCursor(0,0);
lcd.print("****************");
lcd.setCursor(7,1);
lcd.print(buttonPushCounter);
}
}
void checkUp()
{
up_buttonState = digitalRead(Up_buttonPin);
if (up_buttonState != up_lastButtonState) {
if (up_buttonState == LOW && buttonPushCounter<59 ) {
bPress = true;
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else if (up_buttonState == LOW && buttonPushCounter>=59 ) {
bPress = true;
buttonPushCounter = -1;
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else {
Serial.println("off");
}
delay(50);
}
up_lastButtonState = up_buttonState;
}
void checkDown()
{
down_buttonState = digitalRead(Down_buttonPin);
if (down_buttonState != down_lastButtonState) {
if (down_buttonState == LOW && buttonPushCounter<=59 && buttonPushCounter>0) {
bPress = true;
buttonPushCounter--;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else if (down_buttonState == LOW && buttonPushCounter>=0) {
bPress = true;
buttonPushCounter = 60;
buttonPushCounter--;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else {
Serial.println("off");
}
delay(50);
}
down_lastButtonState = down_buttonState;
}
//////////////////////////////////////////////////////////////////////////////
void checktimer()
{
start_buttonState = digitalRead(Start_buttonPin);
if (start_buttonState != start_lastButtonState) {
if (start_buttonState == LOW && buttonPushCounter <= 59 ) {
bPress = true;
lcd.clear();
for(buttonPushCounter <= 59; buttonPushCounter >= 1; buttonPushCounter--){
lcd.setCursor(5,1);
lcd.print(buttonPushCounter-1);
lcd.setCursor(7,1);
lcd.print(":");
int i = 59;
for(i<=59; i>=0;i--){
lcd.setCursor(9,1);
lcd.print(i);
delay(1 * seconds);
}
//delay(1 * minutes);
//lcd.clear();
}
digitalWrite(REL1, 1);
digitalWrite(REL2, 0);
delay(0.5 * seconds);
digitalWrite(REL1, 0);
digitalWrite(REL2, 0);
delay(2 * seconds);
////////////////////////////////////////
digitalWrite(REL1, 0);
digitalWrite(REL2, 1);
delay(0.5 * seconds);
digitalWrite(REL1, 0);
digitalWrite(REL2, 0);
delay(0.5 * seconds);
////////////////////////////////////////
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
lcd.clear();
}
else {
Serial.println("off");
}
delay(50);
}
start_lastButtonState = start_buttonState;
}