#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
int controlPin = 13;
int dirPin = 8;
int stepPin = 9;
char currentTimeValue[4];
int currentState = 1;
int timerSeconds = 0;
int lpcnt = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte ROWS = 4;
const byte COLS = 4;
byte rowPins[ROWS] = {28, 30, 32, 34};
byte colPins[COLS] = {42, 44, 46, 48};
char keys[ROWS][COLS] = {
{'1', '2', '3', 'U'},
{'4', '5', '6', 'R'},
{'7', '8', '9', 'L'},
{'*', '0', '#', 'D'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup(){
lcd.init();
lcd.backlight();
displayCodeEntryScreen();
pinMode(controlPin, OUTPUT);
digitalWrite(controlPin, LOW);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
digitalWrite(stepPin, LOW);
currentTimeValue[0] = '0';
currentTimeValue[1] = '0';
currentTimeValue[2] = '0';
currentTimeValue[3] = '0';
showEnteredTime();
}
void loop(){
int l;
char tempVal[3];
char key = keypad.getKey();
if (int(key) != 0 and currentState == 1){
switch (key){
case '*':
relayStatus(false);
currentTimeValue[0] = '0';
currentTimeValue[1] = '0';
currentTimeValue[2] = '0';
currentTimeValue[3] = '0';
showEnteredTime();
currentState = 1;
lpcnt = 0;
timerSeconds = 0;
break;
case '#':
tempVal[0] = currentTimeValue[0];
tempVal[1] = currentTimeValue[1];
tempVal[2] = 0;
timerSeconds = atol(tempVal) * 60;
tempVal[0] = currentTimeValue[2];
tempVal[1] = currentTimeValue[3];
tempVal[2] = 0;
timerSeconds = timerSeconds + atol(tempVal);
currentState = 2;
break;
default:
currentTimeValue[0] = currentTimeValue[1];
currentTimeValue[1] = currentTimeValue[2];
currentTimeValue[2] = currentTimeValue[3];
currentTimeValue[3] = 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';
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(){
lcd.setCursor(5,1);
lcd.print(currentTimeValue[0]);
lcd.print(currentTimeValue[1]);
lcd.print(":");
lcd.print(currentTimeValue[2]);
lcd.print(currentTimeValue[3]);
}
void relayStatus(bool status){
if (status)
digitalWrite(stepPin, HIGH);
else
digitalWrite(stepPin, LOW);
}
void showCountdown(){
char timest[6];
lcd.setCursor(0,0);
lcd.print(" COUNTING DOWN ");
lcd.setCursor(2,1);
lcd.print("** ");
sprintf(timest, "%d:%.2d" , (timerSeconds/60), (timerSeconds - ((timerSeconds/60)*60)));
lcd.print(timest);
lcd.print(" **");
}
void displayCodeEntryScreen(){
clearScreen();
lcd.setCursor(0,0);
lcd.print("Enter time:");
}
void clearScreen(){
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
}