Problem with subroutine skipping to its end on being called.

I am trying to create a keypad lock using Arduino with user defined password and a master password for set and reset. The problem with the code is that the function is skipping to its end on being called. I am newbie to programming and would like any help with the code that helps me solve this problem.

Attaching the .ino file for reference. Thanks in advance.

SMART_LOCK.ino (2.64 KB)

# include<Keypad.h>
# include<LiquidCrystal.h>
# include<EEPROM.h>

LiquidCrystal lcd(4, 5, 6, 7, 8, 9);

char masterpassword[4] = {1, 2, 3, 4};

char setpass[4], checkpass[4], getuser[4], getmaster[4], masterid[4];

char key = 0;

const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] =
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {14, 15, 16, 17};
byte colPins[COLS] = {18, 19, 2, 3};

Keypad kpd =  Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup()
{
  lcd.begin(16, 2);
  lcd.print("Electronic");
  lcd.setCursor(0, 1);
  lcd.print("  Smart Lock  ");
  delay(2000);
  lcd.clear();
  writemasterpassword();

}

void loop()

{
  writemasterpassword();
  key = kpd.getKey();
  if (key == '0')
  {
    setpassword();

  }

  key = kpd.getKey();
  if (key == '*')
  {
    checkpassword();

  }

  if (key == '#')
  {
    resetpassword();

  }
}

void setpassword()
{
  masterauthentication();
  int i = 0;
  int j = 0;
  int k = 0;
  lcd.print("Enter New Key:");
  for (i = 0; i < 4; i++)
  {
    key = kpd.getKey();
    if (key)
    {
      setpass[i++] = key;
      lcd.setCursor(0, 1);
      lcd.print(key);
    }
  }
  if (i == 4)
  {
    for (k = 0; k < 4; k++)
    {
      for (j = 5; j < 9; j++)
      {
        EEPROM.write(j, setpass[k]);
      }
    }
    lcd.clear();
    lcd.print("New Key Saved");
  }
}

void checkpassword()
{
  int a = 0;
  lcd.print("Enter Pass Key");
  key = kpd.getKey();
  for (a = 0; a < 4; a++)
  {
    if (key)
    {
      checkpass[a++] = key;
      lcd.print(key);
    }
  }
  if (a == 4)
  {
    delay(2000);
    for (int b = 5; b < 9; b++)
    {
      getuser[b] = EEPROM.read(b);
    }
  }
  if (!(strncmp(checkpass, getuser, 4)))
  {
    lcd.clear();
    lcd.print("Key Accepted");
    lcd.setCursor(0, 1);
    lcd.print(" Access Granted ");
  }
  else
  {
    lcd.clear();
    lcd.print("Invalid Key");
    lcd.setCursor(0, 1);
    lcd.print("Access Denied");
  }
}

void resetpassword()
{
  masterauthentication();
  int c;

  for (c = 5; c <= EEPROM.length(); c++)
  {
    EEPROM.write(c, 0);
  }
  delay(10000);
  setpassword();
}

void masterauthentication()
{
  int d = 0;
  lcd.clear();
  lcd.print("Enter Master Key");
  key = kpd.getKey();
  for (d = 0; d < 4; d++)
  {
    if (key)
    {
      masterid[d++] = key;
      lcd.print(key);
    }
  }
  if (d == 4)
  {
    delay(2000);
    for (int e = 0; e < 4; e++)
    {
      getmaster[e] = EEPROM.read(e);
    }
  }
  if (!(strncmp(masterid, getmaster, 4)))
  {
    lcd.clear();
    lcd.print("Master Key Valid");
  }
}


void writemasterpassword()
{
  int f, g;
  for (f = 0; f < 4; f++)
  {
    EEPROM.write(f, 0);
  }
  for (g = 0; g < 4; g++)
  {
    EEPROM.write(g, masterpassword[g]);
  }
}

Edit: added auto-format

OK, which function are you referring to?

EEPROM killer would be a better name of the sketch.
You are writing to the EEPROM locations 0-4 at least twice each loop.
This should kill your EEPROM in a very short time.

MorganS:
OK, which function are you referring to?

if i press '0' it is going to masterauthentication() function, and before i enter the values using the keypad, it is printing "New Key Saved" onto LCD, thus skipping all the intermediate statements. It is the same for other functions too. The functions are directly skipping to the end.

Perhaps something to do with this loop:

  key = kpd.getKey();
  for (d = 0; d < 4; d++)
  {
    if (key)
    {
      masterid[d++] = key;
      lcd.print(key);
    }
  }

Once a single key is pressed, you’ll immediately run through the loop 4 times, putting the same vaalue into the first four locations of masterid. You need to WAIT for the key to be released, and another key pressed before doing each iteration of the loop.

Regards,
Ray L.

RayLivingston:
Perhaps something to do with this loop:

  key = kpd.getKey();

for (d = 0; d < 4; d++)
  {
    if (key)
    {
      masterid[d++] = key;
      lcd.print(key);
    }
  }




Once a single key is pressed, you'll immediately run through the loop 4 times, putting the same vaalue into the first four locations of masterid. You need to WAIT for the key to be released, and another key pressed before doing each iteration of the loop.

Regards,
Ray L.

As i am newbie to programming, a little bit of sample code would be appreciated. Sir, a little bit of help on how to update an array using the keypad would reduce a lot of complications in my code.

RayLivingston:
Perhaps something to do with this loop:

  key = kpd.getKey();

for (d = 0; d < 4; d++)
  {
    if (key)
    {
      masterid[d++] = key;
      lcd.print(key);
    }
  }




Once a single key is pressed, you'll immediately run through the loop 4 times, putting the same vaalue into the first four locations of masterid. You need to WAIT for the key to be released, and another key pressed before doing each iteration of the loop.

Regards,
Ray L.

Thank you Sir, I got the solution from a small hint you provided me with. The problem was with the array itself. I changed the loop to while loop and took the keypad scanning statements into the loop.

Regards,
Akshay Joshi.