#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
LiquidCrystal_I2C lcd(0x27,16,2);
Servo servo;
#define comprimentoSenha 5 // comprimento da senha, se a senha é 4 digitos, coloca=se 5
int servoGrau = 0; // posicao do servo
char Particular[comprimentoSenha]; // comprimento da senha
char Specific[comprimentoSenha] = "CA10"; // SENHA ATIVA
byte Particular_Count = 0, Specific_Count = 0;
char Key;
const byte LINHAS = 4; // the amount of rows on the keypad
const byte COLUNAS = 4; // the amount of columns on the keypad
char keys[LINHAS][COLUNAS ] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
bool SmartDoor = true;
byte rowPins[LINHAS] = {7, 6, 5, 4};
byte colPins[COLUNAS] = {3, 2, 1, 0};
Keypad myKeypad( makeKeymap(keys), rowPins, colPins, LINHAS, COLUNAS);
byte Locked[8] = {
B01110,
B10001,
B10001,
B11111,
B11011,
B11011,
B11011,
B11111
};
// open character
byte Opened[8] = {
B01110,
B00001,
B00001,
B11111,
B11011,
B11011,
B11011,
B11111
};
void setup(){
servo.attach(10);
ServoClose();
lcd.init();
lcd.backlight();
lcd.setCursor(2,0);
lcd.print("Caio Kasper");
lcd.setCursor(0,1);
lcd.print("Fechadura Eletro");
delay(4000);
lcd.clear();
}
void loop(){
if (SmartDoor == 0){
Key = myKeypad.getKey();
if (Key == '#'){
lcd.clear();
ServoClose();
lcd.setCursor(0,0);
lcd.print("Porta Fechada");
lcd.createChar(0, Locked);
lcd.setCursor(14,0);
lcd.write(0);
delay(3000);
SmartDoor = 1;
}
}else Open();
}
void clearData(){
while (Particular_Count != 0){
Particular[Particular_Count--] = 0;
}
return;
}
void ServoOpen(){
for (servoGrau = 180; servoGrau >= 0; servoGrau -= 5) { // move 0 para 180 graus
servo.write(servoGrau); // move para a posicao
delay(15);
}
}
void ServoClose(){
for (servoGrau = 0; servoGrau<= 180; servoGrau+= 5) { // move servo do 0 ao 180 graus
servo.write(servoGrau);
delay(15);
}
}
void Open(){
lcd.setCursor(0,0);
lcd.print("Digite a SENHA:");
Key = myKeypad.getKey();
if (Key){
Particular[Particular_Count] = Key;
lcd.setCursor(Particular_Count, 1);
lcd.print("*");
Particular_Count++; // conta tamanho da senha
}
if (Particular_Count == comprimentoSenha - 1){
if (!strcmp(Particular, Specific)){
lcd.clear();
ServoOpen();
lcd.setCursor(0,0);
lcd.print("Porta ABERTA");
lcd.createChar(1, Opened);
lcd.setCursor(14,0);
lcd.write(1);
lcd.setCursor(0,1);
lcd.print("Aperte # FECHAR");
SmartDoor = 0;
}else{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Senha ERRADA!!!");
lcd.setCursor(0,1);
lcd.print("Tente de novo");
for (int x=9; x>=1; x--){
lcd.setCursor(14,1);
lcd.print(x);
delay(1000);
if(x==1){
lcd.clear();
}
SmartDoor = 1;
}
}
clearData();
}
}