Use 4x4 buttons as password


I have a 4x4 pushbutton matrix that I want to use in an "Escaperoom game". I want to make a password out of these buttons and need the players to press it in the correct order to get it right. If they guess something, a yellow light lights up for 3 seconds, and after that either a red or green light lights up depending on if they guessed the right or wrong password.

My problem:
The system reads my pushbutton input and counts the amount of pushes before the lights works. But it doesn't work with the passord. So all guesses are wrong. Every try, even if it should be right, the yellow and then red light goes off. What am I missing?

#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 4;

int RED = 12;
int YELLOW = 11;
int GREEN = 10;

//Length of password +1 for null character
#define Password_Length 7
// Character to hold password input
char Data[Password_Length];
char Master[Password_Length] = "123456";

// Counter for character entries
byte data_count = 0;
//Character to hold key input
char customKey;

//Array to represent buttons
//We will choose 123456 as password.
char keys[ROWS][COLS] = {
  {'3', 'A', 'D', '0'},
  {'6', '2', '4', 'E'},
  {'5', '7', '1', '9'}, 
  {'C', '8', 'F', 'B'}

//Connections to Arduino
byte rowPINS[ROWS] = {9, 8, 7, 6};
byte colsPINS[COLS] = {5, 4, 3, 2};

//Create keypad object
Keypad customKeypad = Keypad(makeKeymap(keys), rowPINS, colsPINS, ROWS, COLS);

void setup() {
  // put your setup code here, to run once:
  pinMode(RED, OUTPUT);
  pinMode(YELLOW, OUTPUT);
  pinMode(GREEN, OUTPUT);

void loop() {
  // put your main code here, to run repeatedly:
  //Get key value if presset
 char costumKey = customKeypad.getKey();

  if (costumKey) {
    //Enter keypress into array and increment counter
    Data[data_count] = customKey;

  //See if we have reached the password length
  if (data_count == Password_Length -1) {
    if (!strcmp(Data, Master)) {
      //Password is correct
      digitalWrite(YELLOW, HIGH);
      digitalWrite(YELLOW, LOW);
      digitalWrite(GREEN, HIGH);
      digitalWrite(GREEN, LOW);
    else {
      // Password is incorrect
      digitalWrite(YELLOW, HIGH);
      digitalWrite(YELLOW, LOW);
      digitalWrite(RED, HIGH);
      digitalWrite(RED, LOW);

    // Clear data

void clearData() {
  // Go through array and clear data
  while (data_count != 0) {
    Data[data_count--] = 0;

It seems like Data[] is missing the terminating null.

Also, are you sure you want to do this:


I would suggest that you print the value of Data and length of Data before the compare.

But first, I would make sure that the 2 arrays actually have the same size.

How should I write the terminating null?

You read 'costumKey' but store 'customKey'.

Wow! Thank you! Now it works! Hate small spelling errors.