Password sketch problem

Hi, I'm realize a simple sketch, for the management of a 6-digit password. Basically everything worked, but when I go to enter a 7-digit code instead of six, and then I enter the correct password, I always get the message that the password isnt correct!. As if the buffer of the character array that contains the elements of the password is not empty!This my code,thanks!:

#define Password_Lenght 7 // Give enough room for six chars + NULL char

char Data[Password_Lenght]; // 6 is the number of chars it can hold + the null char = 7
char Master[Password_Lenght] = "123456";
byte data_count = 0, master_count = 0;
bool Pass_is_good;


void setup()
{
  Serial.begin(9600);
  Serial.println("Insert password: ");
}

void loop()
{ //START LOOP

  
  if (Serial.available()){
    char input = Serial.read();


  if (input) 
  {
    Data[data_count] = input; // store char into data array
  
    data_count++; // increment data array by 1 to store new char, also keep track of the number of chars entered
  }

  if(data_count == Password_Lenght-1) // if the array index is equal to the number of expected chars, compare data to master
  {
  
    if(strncmp(Data, Master,7)) 
      Serial.println("Wrong Password!");
    else
      Serial.println("Correct Password!");

    delay(1000);// added 1 second delay to make sure the password is completely shown on screen before it gets cleared.
    
      clearData();   
  }
}

}// END LOOP

void clearData()
{
  Serial.println(Data);
  while(data_count !=0)
  {   // This can be used for any array size, 
    Serial.println(Data);
    Data[data_count--] = 0; //clear array for new data
    Serial.println(Data);
  }
  return;
}

when I go to enter a 7-digit code instead of six

Don’t forget that the 7th digit will remain in the serial buffer until you read from it again when it will become the first digit of the next password attempt.

#define Password_Lenght 7 // Give enough room for six chars + NULL char

Can't spell, huh?

You don't seem to understand what NULL-terminated means, either. The NULL is a stop sign. If code is going to ignore the stop sign, putting up 6 more is no guarantee that the code will stop.

   Serial.println(Data);
    Data[data_count--] = 0; //clear array for new data
    Serial.println(Data);

Anonymous, undelimited printing of strings, sucks. Do not do that. It is NOT any more difficult to type:

  Serial.print("Data: [");
   Serial.print(Data);
   Serial.println("]");

The advantage is that you get a LOT more information.

UKHeliBob: Don't forget that the 7th digit will remain in the serial buffer until you read from it again when it will become the first digit of the next password attempt.

How do I make sure to empty the buffer? Should I use null on the last digit? (Seven digit?)

How do I make sure to empty the buffer? Should I use null on the last digit? (Seven digit?)

As has been pointed out you are not using a zero terminated array of chars (aka a string - lowercase s) so a null in the last character is irrelevant. What you need to do is to empty the serial input buffer before reading the next password attempt. It is tempting to think that Serial.flush() will do this but it doesn't. You can, however, do a series, say 10 or more, dummy Serial.read()s and ignore what is read or read until Serial.available() returns false, which is a neater way of doing it.

As a matter of interest, what have you got the line ending set to in the serial monitor because any line ending characters will also be present in the buffer, but dummy reads will get rid of them too.

Can I be eliminated void clear data()? I put n Serial.read in the loop end?Thanks for answer.

The clearData() function puts a zero in all array positions used and sets data_count to zero. The first part is not necessary but you do need to reset data_count to zero.

If I were you, when you have checked a password I would use

while Serial.available()
{
  Serial.read();
}

to remove all data from the serial receive buffer before asking for a password again.