#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {19, 18, 17, 16}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 33, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal_I2C lcd(0x27, 16,2);
String password = "86312497";
String mypassword;
int redled = 12;
int lock = 13;
int counter = 0;
int attempts = 0;
int max_attempts = 3;
void setup(){
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.init();
lcd.backlight();
pinMode(redled, OUTPUT);
pinMode(lock, OUTPUT);
digitalWrite(redled, LOW);
digitalWrite(lock, LOW);
Serial.println("Digite a Senha");
lcd.print("Digite a Senha:");
}
void loop()
{
keypadfunction();
}
void keypadfunction()
{
char key = keypad.getKey();
if (key){
Serial.println(key);
counter = counter + 1;
lcd.setCursor(counter, 1);
lcd.print("*");
}
if (key == '1')
{
mypassword = mypassword + 1;
}
if (key == '2')
{
mypassword = mypassword + 2;
}
if (key == '3')
{
mypassword = mypassword + 3;
}
if (key == '4')
{
mypassword = mypassword + 4;
}
if (key == '5')
{
mypassword = mypassword + 5;
}
if (key == '6')
{
mypassword = mypassword + 6;
}
if (key == '7')
{
mypassword = mypassword + 7;
}
if (key == '8')
{
mypassword = mypassword + 8;
}
if (key == '9')
{
mypassword = mypassword + 9;
}
if (key == '0')
{
mypassword = mypassword + 0;
}
if (key == '*')
{
Serial.println(mypassword);
if ( password == mypassword )
{
lcd.clear();
lcd.setCursor ( 0,0 );
lcd.println("Bem vindo");
lcd.setCursor(0,1);
lcd.println("a nossa casa !");
digitalWrite(lock, HIGH);
delay(3000);
digitalWrite(lock,LOW);
mypassword = "";
counter = 0;
lcd.clear();
lcd.setCursor(0,0);
lcd.println("Digite a Senha:");
}
else
{
Serial.println("ERRADA!");
digitalWrite(lock, LOW);
attempts = attempts + 1;
if (attempts >= max_attempts )
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ENT. BLOQUEADA !");
tone(8, 262, 250);
digitalWrite(redled, HIGH);
delay(10000);
digitalWrite(redled, LOW);
attempts = 0;
}
mypassword = "";
counter = 0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Senha Errada!");
lcd.setCursor(0,1);
lcd.print("190 acionado!");
delay(1000);
lcd.setCursor(0,1);
lcd.print("Tentativ. max. 3");
delay(1000);
lcd.clear();
lcd.println("Digite a Senha:");
lcd.setCursor(0,1);
}
}
}