#include <Servo.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(22, 23, 24, 25, 26, 27); // crea objeto y asigna pines a los cuales se
// encuentran conectados RS, E, D4, D5, D6, D7
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {13, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char password[7];
char realpassword[7] = "123456";
byte index = 0;
Servo puerta; // se nombra al servo
const int BOTON = 53;
int val = 0; //val se emplea para almacenar el estado del boton
int state = 0; // 0 LED apagado, mientras que 1 encendido
int old_val = 0; // almacena el antiguo valor de val
boolean abierta = false, abrir = false;
boolean abiertaAnt = false;
byte pos=0;
bool hubocambios = false;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
puerta.attach(11);// conecta el servo a un pin
pinMode(BOTON, INPUT_PULLUP); // y BOTON como señal de entrada
Serial.println("Empezando");
}
void loop() {
byte check;
char key = keypad.getKey();
val = digitalRead(BOTON); // lee el estado del Boton
if ((val == HIGH) && (old_val == LOW)) {
state = 1 - state;
delay(100);
abrir = state;
hubocambios = true;
}
old_val = val; // valor del antiguo estado
if (key != NO_KEY) {
lcd.setCursor(index, 0);
lcd.println(key);
password[index] = key;
index++;
}
if (key == '#') {
lcd.clear();
Serial.println("Borrar pantalla.");
}
if (index == 6) {
check = 0;
for (int i = 0; i < 6; i++) {
lcd.print(password[i]);
if (password[i] == realpassword[i]) {
check++;
}
}
abrir = (check == 6)?true:false;
hubocambios = true;
index = 0;
lcd.clear();
}
if (hubocambios) {
if(abrir) {
lcd.setCursor(0, 1);
lcd.println("abriendo ");
puerta.write(90); //puerta abierta
delay(1000);
abierta = true;
lcd.setCursor(0, 1);
lcd.println("abierta ");
Serial.println("Puerta abierta");
}
else {
lcd.setCursor(0, 1);
lcd.println("cerrando ");
puerta.write(0);
delay(1000);
abierta = false;
lcd.setCursor(0, 1);
lcd.println("cerrada ");
Serial.println("Puerta cerrada");
}
hubocambios = false;
}
}