#include <Keypad.h>
#include <LiquidCrystal_I2C.h>


const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};

uint8_t colPins[COLS] = { 5, 4, 3, 2 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display

#define Password_Length 8 // Inserire la lunghezza della password
//se la password è lunga 7 caratteri inserire 8
//se la password è lunga 8 caratteri inserire 9 ecc..
char Data[Password_Length]; 
char Master[Password_Length] = "1234567";   //Modifica la password 
byte data_count = 0, master_count = 0;
bool Pass_is_good;
char customKey;





void setup() {
  Serial.begin(9600);

  lcd.init();                      // initialize the lcd

showStartupMessage();


delay(1000);

}
  

void loop() {
  
lcd.setCursor(0,0);
  lcd.print("  PASSWORD...");

  customKey = keypad.getKey();
  if (customKey){
    Data[data_count] = customKey; 
    lcd.setCursor(data_count,1); 
    lcd.print('*');//stampa * al posto del carattere che hai digitato
   // lcd.print(Data[data_count]); //se vuoi stampare cosa stai digitando
    data_count++; 
    }

  if(data_count == Password_Length-1){
    lcd.clear();

    if(!strcmp(Data, Master)){
      lcd.print(" Pass Corretta");
       
      delay(1500);
      
      }
    else{
      lcd.print("Pass Errata");
      delay(3000);
      }
    
    lcd.clear();
    clearData();  
  }
}

void clearData(){
  while(data_count !=0){
    Data[data_count--] = 0; 
  }
  return;
}


  



void showStartupMessage() {
  lcd.setCursor(0, 0);
  lcd.print("Welcome!");
  delay(1000);

  lcd.setCursor(0, 1);
  String message = "inserisci il codice";
  for (byte i = 0; i < message.length(); i++) {
    lcd.print(message[i]);
    delay(50);
  }
  Serial.println(message);
  lcd.setCursor(0, 2);
String message1 = "(7 cifre)";
  for (byte i = 0; i < message.length(); i++) {
    lcd.print(message1[i]);
    delay(50);
  }

  delay(500);
  lcd.clear();
}