Trying to use Serial Monitor for password input

I'm trying to write a program, which will allow me to enter a password using the serial monitor, and compare it to a pre-entered password. Just comparing the password i enter letter by letter to the pre-entered one. It should be simple enough, but it simply isn't working. Still new to Arduino, and could use some help. Am I missing something?

password1.ino (1.27 KB)

I think its better if you save all the serial data in one string, and then compare the data recieved with the password saved using strcmp, instead of doing it char by char

What is the problem? What to program do that you think it shouldn't do?
At the first glance the program seems correct to me.

After a few experiences with your program I see that is not completed correct.
If you place here a ';' you always do the following block, and is the same that don't have the if at all:

      if(index < 8);

I did not understand the ' mismatch == 0' part of the while condition:

   while(Serial.available() > 0 && mismatch == 0)

with this part if you get a mismatch you will stop to reading the characters that come from the serial and you will never see the correct password.

The reason I'm using mismatch == 0 in the while condition, is so that if any of the inserted characters is wrong, the program will say that the password is wrong without having to read the rest of it. And thanks for the observation about the if statement! Embarrassing, but I had totally overlooked that...But even after correcting the if thing, I still cant figure out why the program is breaking out of the while loop!!

Look at the Arduino code in this demo for a system that reliably receives data over a serial connection. It works with the Serial Monitor as well as with Python.

...R

hephestus:
The reason I'm using mismatch == 0 in the while condition, is so that if any of the inserted characters is wrong, the program will say that the password is wrong without having to read the rest of it.
(...)

So, what you need is:

        if(inchar != set_password[index] && tries < 3)
         {
          Serial.println("Wrong. ");
         
          tries++;
          mismatch = 1;
          digitalWrite(led, HIGH);
          delay(1000);
          index = 0; // add this
         }

and in the while you need:

   while(Serial.available() > 0 && tries < 3)

hephestus:
(...)
But even after correcting the if thing, I still cant figure out why the program is breaking out of the while loop!!

You are breaking from the while because you don't have always characters to read. So, this part:

Serial.available() > 0

is false and you break from the while.