Please help me to make this counter code counts down hrs ???
#include <Wire.h>
//#include <LiquidCrystal.h>
#include <Keypad.h>
int controlPin = 13;
char currentTimeValue[6];
int currentState = 1;
int timerHr = 0;
int timerMin = 0;
int timerSec = 0;
int timerSeconds = 0;
int lpcnt = 0;
//define the keypad
const byte ROWS = 4; // four rows
const byte COLS = 3; // three columns
char keys[ROWS][COLS] =
{
{'1','2','3' },
{'4','5','6' },
{'7','8','9' },
{'*','0','#' }
};
byte rowPins[ROWS] = {5, 6, 7, 8};
byte colPins[COLS] = {2, 3, 4};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
//LiquidCrystal lcd(0x3F,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup()
{
//lcd.init(); // initialize the lcd
//
// Print a message to the LCD.
// lcd.backlight();
Serial.begin(9600);
//display main screen
displayCodeEntryScreen();
//setup and turn off relay
pinMode(controlPin, OUTPUT);
digitalWrite(controlPin, LOW);
//setup default time to 00:00
currentTimeValue[0]='0';
currentTimeValue[1]='0';
currentTimeValue[2]='0';
currentTimeValue[3]='0';
currentTimeValue[4]='0';
currentTimeValue[5]='0';
showEnteredTime();
}
void loop()
{
int l;
char tempVal[3];
char key = keypad.getKey();
//key pressed and state is 1
if (int(key) != 0 and currentState == 1) {
switch (key) {
case '*':
relayStatus(false);
currentTimeValue[0]='0';
currentTimeValue[1]='0';
currentTimeValue[2]='0';
currentTimeValue[3]='0';
currentTimeValue[4]='0';
currentTimeValue[5]='0';
showEnteredTime();
currentState = 1;
lpcnt = 0;
timerSeconds = 0;
break;
case '#':
tempVal[0] = currentTimeValue[0];
tempVal[1] = currentTimeValue[1];
tempVal[2] = 0;
timerHr = atol(tempVal) * 3600;
tempVal[0] = currentTimeValue[2];
tempVal[1] = currentTimeValue[3];
tempVal[2] = 0;
timerMin = atol(tempVal) * 60;
tempVal[0] = currentTimeValue[4];
tempVal[1] = currentTimeValue[5];
tempVal[2] = 0;
timerSec = atol(tempVal);
timerSeconds = timerHr + timerMin + timerSec;
currentState = 2;
break;
default:
currentTimeValue[0] = currentTimeValue[1];
currentTimeValue[1] = currentTimeValue[2];
currentTimeValue[2] = currentTimeValue[3];
currentTimeValue[3] = currentTimeValue[4];
currentTimeValue[4] = currentTimeValue[5];
currentTimeValue[5] = key;
showEnteredTime();
break;
}
}
if (currentState == 2) {
if (int(key) != 0) {
if (key == '*') {
relayStatus(false);
displayCodeEntryScreen();
currentTimeValue[0]='0';
currentTimeValue[1]='0';
currentTimeValue[2]='0';
currentTimeValue[3]='0';
currentTimeValue[4]='0';
currentTimeValue[5]='0';
showEnteredTime();
currentState = 1;
lpcnt = 0;
timerSeconds = 0;
}
}
else {
if (lpcnt > 9) {
lpcnt = 0;
--timerSeconds;
showCountdown();
if (timerSeconds <= 0) {
currentState = 1;
relayStatus(false);
displayCodeEntryScreen();
showEnteredTime();
}
else {
relayStatus(true);
}
}
++lpcnt;
delay(100);
}
}
}
void showEnteredTime()
{
Serial.print(currentTimeValue[0]);
Serial.print(currentTimeValue[1]);
Serial.print(":");
Serial.print(currentTimeValue[2]);
Serial.print(currentTimeValue[3]);
Serial.print(":");
Serial.print(currentTimeValue[4]);
Serial.print(currentTimeValue[5]);
Serial.println("");
}
void relayStatus(bool state)
{
if (state)
digitalWrite(controlPin, HIGH);
else
digitalWrite(controlPin, LOW);
}
void showCountdown()
{
char timest[6];
Serial.println("COUNTING DOWN");
sprintf(timest,"%d:%.2d", (timerSeconds/60), (timerSeconds - ((timerSeconds/60)*60)));
Serial.print(timest);
Serial.println("");
}
void displayCodeEntryScreen()
{
clearScreen();
Serial.println("Let's Make It Count");
Serial.println("Down Time...");
Serial.println("Enter Time hh:mm:ss");
}
void clearScreen()
{
Serial.print(" ");
Serial.print(" ");
Serial.print(" ");
Serial.print(" ");
}