#include <Servo.h>
Servo servo;
// Servo
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
//lcd
String inserir_s = "";
String senha = "1234";
String nova_senha = "";
//senha
const byte botao_1 = 13;
bool valor_1 = 0;
const byte botao_2 = 12;
bool valor_2 = 0;
const byte botao_3 = 11;
bool valor_3 = 0;
const byte botao_4 = 10;
bool valor_4 = 0;
const byte botao_altera = 8;
bool valor_altera = 0;
//botões
bool estado_troca = 0;
byte contador = 0;
byte tentativa = 0;
const byte pino_led_verm = 5;
const byte pino_led_verd = 3;
//LED
unsigned long tempo_atual =0;
unsigned long tempo_msg =0;
int time = 0;
//Delay
void setup() {
// put your setup code here, to run once:
lcd.init();
lcd.backlight();
servo.attach(6);
pinMode(botao_1, INPUT_PULLUP);
pinMode(botao_2, INPUT_PULLUP);
pinMode(botao_3, INPUT_PULLUP);
pinMode(botao_4, INPUT_PULLUP);
pinMode(botao_altera, INPUT_PULLUP);
pinMode(pino_led_verm, OUTPUT);
pinMode(pino_led_verd, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
lcd.setCursor(2,2);
lcd.print(" ");
valor_1 = !digitalRead(botao_1);
valor_2 = !digitalRead(botao_2);
valor_3 = !digitalRead(botao_3);
valor_4 = !digitalRead(botao_4);
valor_altera = !digitalRead(botao_altera);
estado_troca = false;
lcd.setCursor(3,0);
lcd.print("DIGITE A SENHA");
lcd.setCursor(contador,1);
if(valor_1 == true || valor_2 == true || valor_3 == true || valor_4 == true && contador < 4){
delay(200);
contador = contador + 1;
if(valor_1 == true){
lcd.print("*");
inserir_s = inserir_s +'1';
delay(100);
}
if(valor_2 == true){
lcd.print("*");
inserir_s = inserir_s +'2' ;
delay(100);
}
if(valor_3 == true){
lcd.print("*");
inserir_s = inserir_s + '3';
delay(100);
}
if(valor_4 == true){
lcd.print("*");
inserir_s = inserir_s + '4';
delay(100);
}
}
if(valor_altera == true && time > 5000 ){
lcd.setCursor(0,3);
lcd.print("Nova Senha");
lcd.setCursor(0,1);
lcd.print(" ");
contador = 0;
inserir_s = "";
estado_troca = true;
time = 0;
alterar_senha(nova_senha);
}else if(valor_altera == true){
time += 1000;
delay(1000);
}else{
time = 0;
}
if(contador == 4){
if(inserir_s == senha){
tone(9,700,750);
analogWrite(pino_led_verd, 255);
servo.write(90);
lcd.setCursor(0,3);
lcd.print("Acesso Liberado ");
delay(5000);
servo.write(0);
delay(250);
analogWrite(pino_led_verd, 0);
delay(2000);
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,3);
lcd.print(" ");
contador = 0;
inserir_s = "";
}else if(inserir_s != senha && estado_troca == false){
tone(9,300,1000);
tentativa = 1 + tentativa;
delay(100);
servo.write(0);
lcd.setCursor(0,3);
lcd.print("Acesso Negado ");
for(int i =0; i<4; i++){
analogWrite(pino_led_verm, 255);
delay(1000);
analogWrite(pino_led_verm, 0);
delay(1000);
}
delay(10);
contador = 0;
inserir_s = "";
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,3);
lcd.print(" ");
if( tentativa >= 3){
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,3);
lcd.print(" ");
lcd.setCursor(2,2);
lcd.print("SISTEMA TRAVADO");
delay(10000);
tentativa = 0;
}
}
}
tempo_atual = millis();
if(tempo_atual - tempo_msg > 500){
tempo_msg = tempo_atual;
}
}
void alterar_senha (String nova){
delay(200);
while(contador <4){
if(!digitalRead(botao_1)== true){
lcd.print("*");
nova= nova +'1';
contador++;
delay(100);
}
if(!digitalRead(botao_2) == true){
lcd.print("*");
nova= nova +'2' ;
contador++;
delay(100);
}
if(!digitalRead(botao_3) == true){
lcd.print("*");
nova= nova + '3';
contador++;
delay(100);
}
if(!digitalRead(botao_4) == true){
lcd.print("*");
nova= nova + '4';
contador++;
delay(100);
}
delay(100);
}
if(contador == 4){
senha = nova;
delay(100);
estado_troca = true;
lcd.setCursor(0,3);
lcd.print("Senha Trocada ");
contador = 0;
lcd.clear();
}
}