/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/faq/how-to-input-a-multiple-digits-number-using-the-keypad
 */

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

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

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

byte pin_rows[ROW_NUM] = {9, 8, 7, 6};   //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3}; //connect to the column pinouts of the keypad

LiquidCrystal_I2C   lcd(0x27, 16, 2);                                                 // Creates LCD   "object" (I2C address, rows, columns)

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

String inputString;
long inputInt;
char key = keypad.getKey();


void setup() {
  Serial.begin(9600); // maximum number of digit for a number is 10, change if needed
  inputString.reserve(16); // maximum number of digit for a number is 10, change if needed

  // LCD Initialization
  lcd.backlight(); 
  lcd.init();
  lcd.clear();

  passEnter();
}

void loop() {
  
}

void passEnter()
{
  Serial.println("Enter Password");

  lcd.setCursor(0, 0);                                   //   Four is added to pressCount to center the input on the LCD
  lcd.print("Enter Password:");
  lcd.setCursor(0, 1);

  while (inputInt == 0)
  {
    char key = keypad.getKey();
    if (key) 
    {
    Serial.println(key);

    if (key >= '0' && key <= '9') 
    {     // only act on numeric keys
      inputString += key;
      lcd.print(key);              // append new character to input string
    } 
    else if (key == '#') 
    {
      if (inputString.length() > 0) {
        inputInt = inputString.toInt(); // YOU GOT AN INTEGER NUMBER
        inputString = "";               // clear input
        // DO YOUR WORK HERE
      Serial.print("password entered: ");
      Serial.println(inputInt);
      timerStart();
      }
    } 
    else if (key == '*') 
    {
      inputString = "";
      lcd.clear();
      lcd.setCursor(0, 0);                                   //   Four is added to pressCount to center the input on the LCD
      lcd.print("Enter Password:");
      lcd.setCursor(0, 1);                 // clear input
    }
    /*Serial.println(inputString);
    Serial.println(inputInt);*/
    }
  }
}

void timerStart()
{
  Serial.println("timer started");
  Serial.print("password: ");
  Serial.println(inputInt);

}