#include <LiquidCrystal.h>
#include <Keypad.h>

LiquidCrystal lcd(12, 11, 10, 9, 8, 7);


const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //four columns

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

byte pin_rows[ROW_NUM] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {A3, A2, A1, A0}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );

const String password = "A1B2C3"; // change your password here
String input_password;
int cursorColumn = 0;


void setup(){
  Serial.begin(9600);
  input_password.reserve(32); // maximum input characters is 33, change if needed
  lcd.begin(16, 2); // set up number of columns and rows


  //lcd.setCursor(0, 0);         // move cursor to   (0, 0)
  //lcd.print("Arduino");        // print message at (0, 0)
  //lcd.setCursor(2, 1);         // move cursor to   (2, 1)
  //lcd.print("GetStarted.com"); // print message at (2, 1)
}

void loop(){
  char key = keypad.getKey();

  if (key){
    Serial.println(key);
    lcd.setCursor(cursorColumn, 0); // move cursor to   (cursorColumn, 0)
    lcd.print("*");                 // print key at (cursorColumn, 0)
    cursorColumn++;

    if(key == '*') {
      input_password = ""; // clear input password
      cursorColumn = 0;
      lcd.clear();
      
    } else if(key == '#') {
      if(password == input_password) {
        lcd.setCursor(0,0);
        lcd.print("Correct");
        
        Serial.println("password is correct");
        // DO YOUR WORK HERE
        
      } else {
        Serial.println("password is incorrect, try again");
      }

      input_password = ""; // clear input password
    } else {
      input_password += key; // append new character to input password string
    }
  }
}