#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include <Servo.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 5 // Inserire la lunghezza della password
//se la password è lunga 7 caratteri inserire 8
//se la password è lunga 8 caratteri inserire 9 ecc..
char Password[Password_Length];
char data[Password_Length];
byte data_count = 0, master_count = 0;
char customKey;
char ckPassword[Password_Length];
char pippo;
const int rgbPins[] = { 13, 12, 11};
unsigned long inizio, fine, trascorso;  // variabili per funzioni millis() showStartupMessage

Servo myservo;  // create servo object to control a servo
int pos = 0;    // variable to store the servo position



void setup() {
  Serial.begin(9600);
  myservo.attach(10);  // attaches the servo on pin 10 to the servo object
  //myservo.write(pos + 180);
  for ( int i = 0; i < 3; i++)
    pinMode( rgbPins[i], OUTPUT); // inizializziamo le uscite arduino -> RGB come uscite
  digitalWrite(rgbPins[0], HIGH);
  lcd.init();                      // initialize the lcd
  showStartupMessage();
  lock();
  

}


void loop() {

  lcd.setCursor(0, 0);
  lcd.print("premi un tasto");
  lcd.setCursor(0, 1);
  lcd.print("per iniziare");
  lcd.setCursor(0, 2);
  lcd.print("B per chiudere");
  
  //REDlcdBlynk();
  customKey = keypad.waitForKey();

  if (customKey == '#') {
    changePw();

  }
  else if (customKey == 'B') {
    lock ();

  }






  else if ( customKey )  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("PASSWORD?:");
    for (data_count; data_count < Password_Length - 1; data_count++) {
      customKey = keypad.waitForKey();
      data[data_count] = customKey;
      lcd.setCursor(data_count, 2);
      lcd.print('*');//stampa * al posto del carattere che hai digitato
      //lcd.print(data[data_count]); //se vuoi stampare cosa stai digitando
      Serial.println(data);

    }



    char pippo[Password_Length];
    EEPROM.get(0, pippo);
    //Serial.println ("controllo");
    //Serial.println(pippo);
    //Serial.println(ckPassword);
    //delay(1000);
    if (strcmp(pippo, data) == 0) {
      //GREENlcdBlynk();
      lcd.setCursor(0, 3);
      lcd.print(" Pass Corretta");
      delay(500);
      lcd.clear();
      clearData();

      unlock ();
      
    }
    else if (strcmp(pippo, data) != 0) {

      REDlcdBlynk();
      lcd.setCursor(0, 3);
      lcd.print("Pass Errata");
      delay(500);
      Serial.println("44444");
      lcd.clear();
      clearData();

      
    }
  }

  
  lcd.clear();
  clearData();

}