Regarding my project of the Keypad Lock.

void setpassword()
 {
  int a = 0;
  int b = 0;
  int c;
  Serial.println(" Enter Master Key to set password: ");

  while(b < 4)
  {
    key = kpd.getKey();
  if(key)
  {
    pass1[b++] = key;
    Serial.println(key);
  }}
  if (b == 3)
  {
  for(c = 0; c < 4; c++)
  {
    pass[c] = EEPROM.read(c);
  }
  }
 if(c == 3)
  {
 compareduringset();
  }
 if(!flag)
 {
    Serial.println("Master Key to set is valid");
    Serial.println("Enter New password: ");
    while(a < 4)
    {
      key = kpd.getKey();
    if(key)
    {
      pass2[a++] = key;
      Serial.println(key);
    }
    if(a == 3)
    {
    EEPROM.write(8,pass2[0]);
    EEPROM.write(9,pass2[1]);
    EEPROM.write(10,pass2[2]);
    EEPROM.write(11,pass2[3]);
    }
  }
  Serial.println("New Password Saved");
 }
 else
 {
  Serial.println("Master Key to set is Invalid");
  call();
 }
 }

Code for comparing pass

 and pass1[c]: Please correct me if i am wrong with the comparison logic:

[code]

void compareduringset()
{
  int h;
  for(h = 0; h<4; h++)
  {
    if(pass[h] == pass1[h])
    flag = 0;
  }
}
 

[/code]


Note that pass[] and pass1[] are character arrays.



I am saving 2 master passwords in the EEPROM through a sketch and then retrieving them using this sketch. But, my problem is whatever value I enter for my master password in this sketch(other than the actual one) it is always showing the output as Master key valid. I have tried another alternative, but in that it is displaying the output as Master key invalid, even if the entered password is correct.

Note that pass[] and pass1[] are character arrays.

Note that pass and pass1 are stupid names. If you are going to number some variables in what appears to be a set, number ALL of them. pass1 and pass2, not pass and pass1.

Even better, though, is to use names that make sense. masterPassOne, masterPassTwo, and thisPass, for instance. Then, it is very obvious what you are collecting keystrokes or EEPROM data for and what you are comparing.

Create a function to get an array of keys. Call that function more than once. Do NOT replicate the functionality.

a, b, c, and flag don't mean squat. Use names that mean something, with values that mean something. if(b == 3) doesn't mean anything. if(thisPassLength == 3) suggests that there is a good reason for doing something different on this pass through some function.