// C++ code
//
#include <stdlib.h>
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const unsigned char ROWS = 4;
const unsigned char COLS = 4;
#define MOTOR 10
struct SYSFLAGS{//este es un campo de bits, lo usamos para definir banderas
unsigned int stateM :4;
};
struct SYSFLAGS sysFlags;//asignamos un nombre para poder acceder a el
char timeNum[4] = {0};
char timeNum_Wr = 0;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
unsigned char rowPins[ROWS] = {9,8,7,6};
unsigned char colPins[COLS] = {5,4,3,2};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal_I2C lcd(0x27,16,2);
int timeOut = 0;
int timerOne = 0;
int timeCnt = 0;
String array = "0";
void setup()
{
lcd.init();
lcd.backlight();
lcd.clear();
pinMode(MOTOR,OUTPUT);
sysFlags.stateM = 0;
}
void loop()
{
static int cursor = 7;
char key = keypad.getKey();
switch(sysFlags.stateM){
case 0:
digitalWrite(MOTOR, LOW);
lcd.setCursor(4,0);
lcd.print("WELCOME");
if(key){
lcd.clear();
sysFlags.stateM++;
lcd.setCursor(cursor++,0);
lcd.print(key);
timeNum[timeNum_Wr++] = key;
timerOne = millis();
}
break;
case 1:
lcd.setCursor(0,0);
lcd.print("TIME_S");
if(key){
timerOne = millis();
if(cursor <= 9){
lcd.setCursor(cursor++,0);
lcd.print(key);
timeNum[timeNum_Wr++] = key;
timeCnt = atoi(timeNum);
timerOne = millis();
}
}
timeOut = (millis() - timerOne);
if(timeOut > 2000){
lcd.clear();
timeNum_Wr = 0;
cursor = 7;
sysFlags.stateM++;
}
break;
case 2:
digitalWrite(MOTOR, HIGH);
lcd.setCursor(0,0);
lcd.print("TIME_T");
lcd.setCursor(cursor,0);
lcd.print(timeCnt);
timeOut = (millis()-timerOne);
if(timeOut > 1000){
lcd.setCursor(cursor,0);
lcd.print(" ");
timerOne = millis();
if(--timeCnt < 0){
digitalWrite(MOTOR, LOW);
sysFlags.stateM = 0;
lcd.clear();
}
}
break;
default:
break;
}
}