Key Pad with passcode

I have written some code that is looking for a 4 digit code. If the code is incorrect a two tone buzzer will play and allow you to enter another code. If the correct code is entered it will power a relay. That part all works great.

I also have another part of the code that is monitoring a door sensor that is normally closed. When the door opens I want the LED strip to power on regardless of if the door closes and opens again. I want the LEDs to go off and stay off when the correct code is entered.

My code turns the LEDs off when the correct code is entered but then they turn back on after 5 seconds. Any suggestions?

//******************************************************************************************
#include <Keypad.h>//include library code
#include "pitches.h"//include library code
//******************************************************************************************
// Melodies definition: rejection - you can change melodies. see in file pitches.h
int keypadRead;  // keypad read 1 = good, 0 = bad for playTune function

int noaccess_melody[] = {NOTE_C5, NOTE_C4};
int noaccess_noteDurations[] = {2};
const int buzzerPin = 12; //active piezo buzzer
int doorPosition = 13; //Door position sensor. N.C. when safe door is closed
int led = 11; //LED strip inside safe
const int correctPass_relay = 10;   //relay that triggers when correct password is entered


#define Password_Lenght 5     //give enough room for 8 chars + NULL char
char Data[Password_Lenght];   //8 is the number of chars it can hold + the null char = 9
char Master[Password_Lenght] = "1234";//default password is 1234
byte data_count = 0;          //variable to store the number counting
const byte rows = 4; //four rows
const byte cols = 3; //three columns
char keys[rows][cols] = {//define the symbols on the buttons of the keypads
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'#', '0', '*'}
};
byte rowPins[rows] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[cols] = {5, 4, 3};    //connect to the column pinouts of the keypad
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );
//**********************************************************************************************
void setup()
{
  pinMode(buzzerPin, OUTPUT);           //sets buzzerPin as OUTPUT
  pinMode(doorPosition, INPUT_PULLUP);  //tells when safe door is open
  pinMode(led, OUTPUT);                 //output for LED strip inside safe
  pinMode(correctPass_relay, OUTPUT);   //relay OUTPUT triggers with correct password
  Serial.begin(9600);                   //initialise the serial communication at 9600 bps
  Serial.println("********************");
  Serial.println("*****Electronic*****");
  Serial.println("*****Keypad Lock****");
  Serial.println("********************");
  Serial.println("*****Enter code*****");
}

void loop() {

  char customKey = customKeypad.getKey();//reads the key pressed
  if (customKey) // makes sure that the key is actually pressed
  {
    Data[data_count] = customKey; // store char into data array
    Serial.print(customKey); // print char at said cursor
    data_count++; // increment data array by 1 to store new char, also keep track of the number of chars entered

  }
  if (digitalRead(doorPosition) == LOW)  {
    digitalWrite (led, HIGH); //Turns LEDs inside safe ON when safe door is open
  }

  if (data_count == Password_Lenght - 1) // if the array index is equal to the number of expected chars, compare data to master
  {
    if (!strcmp(Data, Master)) {
      keypadRead = 1;
      digitalWrite(led, LOW); //Turns LEDs inside safe OFF when puzzle is solved
      digitalWrite(correctPass_relay, HIGH);//relay is on
      Serial.println("");
      Serial.println("********************");
      Serial.println("** ACCESS GRANTED **");
      Serial.println("**   WELCOME!!    **");
      Serial.println("********************");
      delay(5000);//sets delay for 5 seconds
      digitalWrite(correctPass_relay, LOW);//relay is off
      resetAll ();//reset all function
    }

    else {
      keypadRead = 0;
      playTune(keypadRead);
      Serial.println("");
      Serial.println("********************");
      Serial.println("** ACCESS DENIED! **");
      Serial.println("**  INVALID CODE  **");
      Serial.println("********************");
      resetAll ();//reset all function
    }
  }


}
void resetAll ()
{
  Serial.println("********************");
  Serial.println("*****Electronic*****");
  Serial.println("*****Keypad Lock****");
  Serial.println("********************");
  Serial.println("*****Enter code*****");
  while (data_count != 0)
  {
    Data[data_count--] = 0; //clear array for new data
  }

  return;
}
//**************************************************************
//Function to play the melody during No access
void playTune(int Scan) {
  if (Scan == 0) // Wrong secret code entered - No Access
  {
    for (int i = 0; i < 2; i++)    //loop through the notes
    {
      int noaccess_noteDuration = 1000 / noaccess_noteDurations[i];
      tone(buzzerPin, noaccess_melody[i], noaccess_noteDuration);
      int noaccess_pauseBetweenNotes = noaccess_noteDuration * 1.30;
      delay(noaccess_pauseBetweenNotes);
      noTone(buzzerPin);
    }
  }
}

Why do you have “stupid” comments

#define Password_Lenght 5     //give enough room for 8 chars + NULL char
char Data[Password_Lenght];   //8 is the number of chars it can hold + the null char = 9

when I start reading this that throws me off as I don’t know what the real intent is now...the code does something not aligned with its explanation...(and I think in English it’s length and not lenght)

Then in your comparison if (data_count == Password_Lenght - 1)you get confused...

Why don’t you use a boolean if it has 2 values, true or false?int keypadRead;  // keypad read 1 = good, 0 = bad for playTune function

Your melody and duration arrays could be const and the duration is probably directly a const value, does not need to be an array... and thus

   for (int i = 0; i < 2; i++)    //loop through the notes
    {
      int noaccess_noteDuration = 1000 / noaccess_noteDurations[i];

is an array overflow as there is no such thing as noaccess_noteDurations[1]

In this codeif (!strcmp(Data, Master)) {you rely on the last character of Data array to be a NULL char, it’s the case indeed because global arrays get initialized to 0 but it’s good to remember that if you want one day to have a variable length password and code dealing with long input until a ‘#’ is pressed (here you are giving away that the pwd is 4 digits so brute force attack is easy)

Here is your 5 second before the issue.delay(5000);//sets delay for 5 secondsand they turn back on because nothing tells the door to remember the code was entered in then right way.

  if (digitalRead(doorPosition) == LOW)  {
    digitalWrite (led, HIGH); //Turns LEDs inside safe ON when safe door is open
  }

You need a better state machine (also handling what happens when the door closes)