#include <ezButton.h>

// Change pins, password and password length here
#define PASSWORD "1234"         // password
#define PASSWORD_LENGTH 4       // password length
#define GREEN_LED 12            // green LED pin
#define RED_LED 11              // red LED pin
#define BUTTON1 2               // Button 1 pin
#define BUTTON2 3               // Button 2 pin
#define BUTTON3 4               // Button 3 pin
#define BUTTON4 5               // Button 4 pin
#define BUTTON5 6               // Button 5 pin
#define BUTTON6 7               // Button 6 pin
#define BUTTON7 8               // Button 7 pin
#define BUTTON8 9               // Button 8 pin
#define BUTTON9 10              // Button 9 pin

// don't change the following variables
String entered_number = "";
bool password_status = 0;

ezButton button1(BUTTON1);
ezButton button2(BUTTON2);
ezButton button3(BUTTON3);
ezButton button4(BUTTON4);
ezButton button5(BUTTON5);
ezButton button6(BUTTON6);
ezButton button7(BUTTON7);
ezButton button8(BUTTON8);
ezButton button9(BUTTON9);

void checkPassword();

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

  button1.setDebounceTime(50);
  button2.setDebounceTime(50);
  button3.setDebounceTime(50);
  button4.setDebounceTime(50);
  button5.setDebounceTime(50);
  button6.setDebounceTime(50);
  button7.setDebounceTime(50);
  button8.setDebounceTime(50);
  button9.setDebounceTime(50);
}

void loop() {
  button1.loop();
  button2.loop();
  button3.loop();
  button4.loop();
  button5.loop();
  button6.loop();
  button7.loop();
  button8.loop();
  button9.loop();

  if (password_status == 0) {
    if (button1.isPressed()) {
      entered_number += "1";
      checkPassword();
    }
    if (button2.isPressed()) {
      entered_number += "2";
      checkPassword();
    }

     if (button3.isPressed()) {
      entered_number += "3";
      checkPassword();
    }

     if (button4.isPressed()) {
      entered_number += "4";
      checkPassword();
    }

     if (button5.isPressed()) {
      entered_number += "5";
      checkPassword();
    }

     if (button6.isPressed()) {
      entered_number += "6";
      checkPassword();
    }

     if (button7.isPressed()) {
      entered_number += "7";
      checkPassword();
    }

     if (button8.isPressed()) {
      entered_number += "8";
      checkPassword();
    }
     if (button9.isPressed()) {
      entered_number += "9";
      checkPassword();
    }

  } else if (password_status == 1) {
    digitalWrite(RED_LED, LOW);
    digitalWrite(GREEN_LED, HIGH);
  }
}


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