#include <Keypad.h>

#define PASSWORD "123456789"
#define GREEN_LED 12
#define RED_LED 11

const uint8_t ROWS = 3;
const uint8_t COLS = 3;

// digits
char keys[ROWS][COLS] = {
  { '1', '2', '3'},
  { '4', '5', '6'},
  { '7', '8', '9'}
};

// change the following pins
uint8_t colPins[COLS] = { 5, 4, 3}; // Pins connected to C1, C2, C3
uint8_t rowPins[ROWS] = { 8, 7, 6}; // Pins connected to R1, R2, R3

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String entered_number = "";
bool password_status;

void setup() {
  Serial.begin(9600);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(RED_LED, OUTPUT);

  digitalWrite(RED_LED, HIGH);
}

void loop() {
  if (password_status == 0) {
    char key = keypad.getKey();
    if (key != NO_KEY) {
      entered_number += key;
    }
    if (entered_number.length() == 9) {
      if (entered_number == PASSWORD) {
        Serial.println("Password matched!");
        password_status = 1;
      }
      else {
        Serial.println("Password Mismatched! Try again...");
        for (int i = 0; i < 3; i++) {
          digitalWrite(RED_LED, LOW);
          delay(500);
          digitalWrite(RED_LED, HIGH);
          delay(500);
        }
        entered_number = "";
      }
    }
  } else {
    digitalWrite(RED_LED, LOW);
    digitalWrite(GREEN_LED, HIGH);
  }
}
1
2
3
4
5
6
7
8
9