Keypad "Safe"

I want to build a “Safe”. When I enter the Correct Password, I want a Servo-Motor to start doing something, but I am not so far now. I have successfully connected a Keypad. Now I want to Code that the Program recognize my Password, if I type it in. The Library I used is linked. My Code looks like this:

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

void setup(){
  Serial.begin(9600);
  int Password = 2;
}
  
void loop(){
  char customKey = customKeypad.getKey();
  
  if (customKey){
    Serial.println(customKey);
    
  }
  else if(customKey == 2 ){
    Serial.print("Success!");
  }
}

I want a var in the void setup where I can program my password.

Keypad.zip (9.48 KB)

void setup(){
  Serial.begin(9600);
  int Password = 2;
}

The Password variable goes out of scope immediately, so it was pointless to have created it.

I want a var in the void setup where I can program my password.

That is NOT going to happen. The variable needs to have global scope.

  if (customKey){
    Serial.println(customKey);
   
  }
  else if(customKey == 2 ){

The if statement handles all non-zero values of customKey. The else statement(s) will never be executed.

Let’s start with the simple stuff

else if (customKey == 2 )

Why is this an else ? As written the else will only be executed when no key has been pressed

The Keypad library returns a char not a number so to test for s single digit either test for ‘2’ or subtract ‘0’ (ie 48) from the digit returned and test for an actual digit

Get that working and then you can move onto receiving and testing multiple digits