#include <Key.h>
#include <Keypad.h>
#include <Servo.h>
Servo siervo;

//LCD config
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);

//Variables
int contra=0;
bool cerrar=false;
int character = 0;
int character1 = 0;
int activated =0;
int activated1 =0;
char Str[16] = {' ', ' ', ' ', ' ', ' ', ' ', '-', '*', '*', '*', ' ', ' ', ' ', ' ', ' ', ' '};  
int rojo=13;
int verde=12;
int azul=11;
int intruso=0;
int i=0;

//Keypad config
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the symbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','4','7','*'},
  {'2','5','8','0'},
  {'3','6','9','#'},
  {'A','B','C','D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 


void setup(){
  
  pinMode(rojo,OUTPUT); 
  pinMode(verde,OUTPUT);
  pinMode(azul,OUTPUT);
  lcd.init();  //Init the screen and print the first text
  lcd.backlight();
  lcd.clear();
  lcd.print(" Your Password");
  lcd.setCursor(0,1);
  lcd.print("      -***     ");
  siervo.attach(10);
  pinMode(10, OUTPUT);
 
}
  

void loop(){

  if (activated==0)
  {

    char customKey = customKeypad.getKey(); //this function reads the presed key

    if(customKey)
    {
    lcd.setCursor(0,1);
    lcd.print("Your Password");
    lcd.setCursor(0,1);
    lcd.print("      -***     ");
    
      if (character ==0)
      {  
      Serial.println(customKey);
      Str[6]= customKey;   
   
      lcd.setCursor(0,1);
      lcd.print("Your Password");    
      lcd.setCursor(0,1);
      lcd.print(Str);
      analogWrite(azul,255);
      delay(150);
      analogWrite(azul,LOW);
      }


      if (character ==1)
      {  
      Serial.println(customKey);
      Str[7]= customKey;   

      lcd.setCursor(0,1);
      lcd.print("Your Password");    
      lcd.setCursor(0,1);
      lcd.print(Str);
      analogWrite(azul,255);
      delay(150);
      analogWrite(azul,LOW);
        
      }

      if (character ==2)
      {  
      Serial.println(customKey);
      Str[8]= customKey;   
      lcd.setCursor(0,1);
      lcd.print("Your Password");    
      lcd.setCursor(0,1);
      lcd.print(Str);
      analogWrite(azul,255);
      delay(150);
      analogWrite(azul,LOW);
      }

      if (character ==3)
      {  
      Serial.println(customKey);
      Str[9]= customKey;   
      lcd.setCursor(0,1);
      lcd.print("Your Password");    
      lcd.setCursor(0,1);
      lcd.print(Str);
      analogWrite(azul,255);
      delay(70);
      analogWrite(azul,LOW);
      contra=Str[6]+Str[7]+Str[8]+Str[9];
      activated=1;
      lcd.clear();
      lcd.print("    PASSWORD");
      lcd.setCursor(0,1);
      Str[6]= '-';
      Str[7]= '*'; 
      Str[8]= '*'; 
      Str[9]= '*';
      Str[10]= ' ';
      lcd.clear();    
      lcd.setCursor(4,0);
      lcd.print("PASSWORD");
      lcd.setCursor(0,1);
      lcd.print(Str);
      analogWrite(azul,20);
      analogWrite(verde,20);
      analogWrite(rojo,255);
      delay(1000);
      analogWrite(azul,LOW);
      analogWrite(verde,LOW);
      analogWrite(rojo,LOW);
      
      }
        character=character+1;
    }

  }
  if(activated==1)
  {

    char customKey = customKeypad.getKey(); 
    if (customKey)
      {
          
      if (character1 ==0)
      {  
        
        lcd.setCursor(4,0);
        lcd.print("PASSWORD");
        lcd.setCursor(0,1);
        lcd.print(Str);
        Serial.println(customKey);
        Str[6]= customKey;   
        lcd.setCursor(0,0);
        lcd.print("    PASSWORD");    
        lcd.setCursor(0,1);
        lcd.print(Str);
        analogWrite(azul,255);
        delay(150);
        analogWrite(azul,LOW);
      }

      if (character1 ==1)
      {  
        Serial.println(customKey);
        Str[7]= customKey;   
        lcd.setCursor(0,0);
        lcd.print("    PASSWORD");    
        lcd.setCursor(0,1);
        lcd.print(Str);
        analogWrite(azul,255);
        delay(150);
        analogWrite(azul,LOW);
        
      }

      if (character1 ==2)
      {  
        Serial.println(customKey);
        Str[8]= customKey;   
        lcd.setCursor(0,0);
        lcd.print("    PASSWORD");    
        lcd.setCursor(0,1);
        lcd.print(Str);
        analogWrite(azul,255);
        delay(150);
        analogWrite(azul,LOW);
      }

      if (character1 ==3)
      {  
        Serial.println(customKey);
        Str[9]= customKey;   
        lcd.setCursor(0,0);
        lcd.print("    PASSWORD");    
        lcd.setCursor(0,1);
        lcd.print(Str);
        analogWrite(azul,255);
        delay(70);
        analogWrite(azul,LOW);
        activated1=1;
        activated=3;
      }

      character1=character1+1;
    }
  }
  if (activated1 == 1)
  {

    if( character1==4 && contra== Str[6]+Str[7]+Str[8]+Str[9] )
    {
      Str[6]= '-';
      Str[7]= '*'; 
      Str[8]= '*'; 
      Str[9]= '*';
      Str[10]= ' ';
      lcd.clear();
      lcd.setCursor(4,0);
      lcd.print("ACCEPTED");
      analogWrite(verde,255);
      lcd.clear();    
      lcd.setCursor(4,0);
      lcd.print("WELCOME");
      delay(500);
      lcd.clear();    
      lcd.setCursor(3,0);
      lcd.print("DOOR  OPEN");
      intruso=0;
      cerrar=true;
      delay(500);
      siervo.write(200); 
      delay(700);
      analogWrite(verde,LOW);
      activated1=0;
    }
    else
    {
      intruso++; 
      lcd.clear();    
      lcd.setCursor(4,0);
      lcd.print("ROBBER!!");
      lcd.setCursor(1,1);
      lcd.print("CALLING POLICE");
      character1=5;
      while(i<=90 && intruso==3)
         { 
            analogWrite(rojo,255);
            delay(70);
            analogWrite(rojo,LOW);
            delay(40); 
            i++;
            if(i==90)
              {
                lcd.noDisplay();
                delay(100);
                lcd.noBacklight();
              }
          }  
      if(intruso!=3 && intruso<4)
      {
        
      lcd.clear();    
      lcd.setCursor(1,0);
      lcd.print("PASSWORD ERROR");
      lcd.setCursor(3,1);
      analogWrite(rojo,255);
      lcd.print("TRY  AGAIN");
      delay(2000);
      activated1=0;
      activated=1;
      character1=0;
      Str[6]= '-';
      Str[7]= '*'; 
      Str[8]= '*'; 
      Str[9]= '*';
      Str[10]= ' ';
      lcd.clear();    
      lcd.setCursor(4,0);
      lcd.print("PASSWORD");
      lcd.setCursor(0,1);
      lcd.print(Str); 
      analogWrite(rojo,LOW);
       }
        
    }

  }
  if(cerrar==true)
  {
    
    lcd.clear();    
    lcd.setCursor(0,0);
    lcd.print("PRESS B TO SWAP");
    lcd.setCursor(0,1);
    lcd.print("PRESS C TO CLOSE");
    char customKey = customKeypad.getKey();
    Str[6]= customKey;
    lcd.print(Str); 
      
      if(Str[6]=='C')
        {
        
        lcd.clear();    
        lcd.setCursor(3,0);
        lcd.print("DOOR CLOSE"); 
        siervo.write(-200);
       
        analogWrite(azul,150);
        analogWrite(verde,150);
        analogWrite(rojo,150);
        delay(1000);
        analogWrite(azul,LOW);
        analogWrite(verde,LOW);
        analogWrite(rojo,LOW);
        lcd.clear();
        lcd.print("    PASSWORD");
        lcd.setCursor(0,1);
        Str[6]= '-';
        Str[7]= '*'; 
        Str[8]= '*'; 
        Str[9]= '*';
        Str[10]= ' ';
        lcd.clear();    
        lcd.setCursor(4,0);
        lcd.print("PASSWORD");
        lcd.setCursor(0,1);
        lcd.print(Str);
        character=5;
        activated=1;
        character1=0;
        
        cerrar=false;

         }
        if(Str[6]=='B')
        {
        lcd.clear(); 
        lcd.setCursor(5,0);
        lcd.print("CHANGE");
        lcd.setCursor(0,1);
        lcd.print("      -***     ");    
        analogWrite(azul,10);
        analogWrite(verde,70);
        analogWrite(rojo,300);
        delay(1000);
        analogWrite(azul,LOW);
        analogWrite(verde,LOW);
        analogWrite(rojo,LOW);
        character=0;
        character1=0;
        activated=0;
        cerrar=false;

         }
  }
 
}

