Hello!!! I have an arduino mega 2560 and a lcd .I have a password (to unlock something) and I type some characters in serial monitor to find the password and I always get “ACCESS DENIED” message,before type all the 6 characters that the password has.
For example ,the password “53A#10” and then I type in serial monitor the number 5 and I push enter.
Then ,the number 3 and push enter and when I push the letter “A” ,I get “ACCESS DENIED” message in lcd.
Here is the code:
This is because each time you press enter you send a non-printng character (known as carriage return and normally shown in text as ). You’re looking for six characters and you send ‘5’ ‘’ ‘3’ ‘’ ‘A’ ‘’ which fulfils your six character requirement but does not match your password string. Either enter all the characters at one go or filter out the carriage return characters before counting and trying to match with the stored password.
arduiNICK:
I typed all the characters and pressed enter , but the same thing happens.
As about the second one you said ,how can I do it?
Thanks…
That is because you’ve sent 7 characters (password + ) which is unlikely to match the seventh character in your stored 6 character password.
If you want to filter out carriage returns then do your Serial.read() and only do the subsequent tests if the character does not equal (decimal 13, hex 0x0D).
I think the problem here may be that you are unaware of the differences between a String object and an ordinary C string which is just an array of characters.
This code:
system_password[num_of_digits]
won’t work because system_password is a String, not a character array, so you can’t access a particular character position like that. You won’t get an error message at compile time or run time, it will just do something wierd and unexpected, which you have seen. To access a character position in a String you must use
system_password.charAt(num_of_digits)
However, instead of that, I would recommend using character array instead of String:
Post your updated code and what you see on serial monitor. Also, each time your code writes to the LCD, write the same text to serial, so we can see that as well.
I made them as you said.
I had String system_password = “8924” and i made it char system_password = “8924”
Second , I had given_password == system_password to strcmp(total_password, system_password) == 0
I think I did not forget anything…
Please advice me…