I am developing a hardware project which contains a facility to change the stored password by any user, if he/she knows the current password.The new password is stored in arduino eeprom. In the main screen, if the user enters the current password, door is unlocked from inside. . The issue faced here is that the point where current password is entered by user, it works fine, but I want to check that '#' is not entered by user in the new password which is not performed properly.
Because in the main screen, when user presses '#' key, change password function is called up, where user is asked to enter current password.
Here is the screenshot of the circuit prepared from fritzing.
The code snippet for entering new password is as under.
lcd.clear();
lcd.print("New Passkey:");
lcd.setCursor(0, 1);
while (j < 4)
{
char key = keypad4x4.getKey();
if (key)
{
new_password[j++] = key;
lcd.print(key);
tone(buzzer, 1000, 50);
if (j == 4)
{
lcd.print("4 reached");
delay(1000);
new_password[5] = '\0';
String str = String(new_password);
if (str.indexOf('#')) /** this condition comes out to be true always i.e. always returns a non-zero value**/
{
tone(buzzer, 200, 700);
lcd.clear();
lcd.print("Invalid Entry"); /** It is printed everytime on lcd**/
delay(2000);
key = 0;
changePass();
break;
}
else /** this block is not entered, so new password is not saved in EEPROM **/
{
for (int i = 0; i < 4; i++)
{
EEPROM.write(i, new_password[i]);
}
lcd.print(" Done......");
delay(1000);
break;
}
}
}
}
and
new_password[5]
is used to store new password entered by user.
Now, I am using a string variable to store this new password.
I am using string.indexof()
function to identify whether this new password contains a '#' character anywhere in it.But it seems that when I put this in an if block in code, it becomes true everytime.I have added comments at respective positions to highlight the problem faced by me. I have tested this code in Proteus simulator, and actual project too.
So, my issue is this one, even if correct password is entered by user,without '#', then also, string.indexOf('#') condition becomes true(returns a non-zero value everytime) and thus even valid password is not saved in EEPROM. I guess I was able to explain my query accurately, please go through the code, and tell me where I am wrong, because I have gone through it many times and could not find the exact issue. Thanks in advance
if (str.indexOf('#')+1)
+1
thing you mentioned in your comment,I didn't quite understand it. What does it do??