Regarding my project of Bluetooth Lock.

char received[4];        
char correct[4] = {'Q','W','E','R'};   
boolean result;        


void setup()
{
pinMode(lock, OUTPUT);
Serial.begin(9600);
digitalWrite(lock, LOW); 
}

void loop()
{
  int i = 0;
  int j = 0;
  Serial.println("Enter Password");
while(Serial.available() >= 4) 
{
 while(i<4)   
  {
   received[i] = Serial.read();  
   i++;
  }
  

  while(j < 4)
  {
   if(received[j]==correct[j]) 
   {
    result = true;   
   }
   else
   {
    result = false; 
    break;   
   }
  }
}

if(result == true)  
{
 digitalWrite(lock, HIGH);
 Serial.println("Locked");
}
else
{
 digitalWrite(lock, LOW);
 Serial.println("Unlocked");
  }
}

I am making a small project on door access using Bluetooth.

My problem here is after i receive the message Enter Password on the Serial monitor, it is not allowing me to enter the password and is directly showing the message unlocked.

My aim is to change it in a way such that when i get the message Enter Password on the Serial Monitor, i should enter the password then it must get compared with the correct password and then display locked or unlocked. I have tried different things but I am unable to get it.

I am new to Arduino and C programming. Any help would be appreciated. Thanks in advance.

while(Serial.available() >= 4)

Why are you using a while() statement here?

Why are you expecting that, nanoseconds after buffering the message to be sent to the Serial Monitor, the user will have entered 4 characters, no more and no less, AND that they will have been received by the Arduino?

You need to isolate the display of the message from the eventual reading of the response. For instance, if the number of characters received is 0 AND the message has not been sent, send the message and set the "message has been sent" flag to true.

You need to read the data whenever there is data to read, not just when there are 4 or more bytes to read.

When you have read the correct number of characters (4 for now, but that could (and should)) change (that means that you will need to read until the end of packet marker arrives), deal with the door lock and clear the "message has been sent" flag.

You need to (eventually) handle longer or shorter passwords.

You need to deal with the line ending settings on the Serial Monitor application. If set to carriage return/line feed, 6 character will be sent when the user clicks send after typing 4 characters.