Limiting the length of a password or number of characters for keypad

Hello :blush:

I am having this code here :

if    //(keypad.key[i].stateChanged)  //case PRESSED: 
    (key == 'B')
    {
      if (input_password.length() > 0)
      {
        lcd.setCursor(input_password.length(), 1); // move cursor to the last added character
        lcd.print(' ');  // Replace it with a blank
        input_password.remove(input_password.length() - 1);
      } 
    }

I just want to limit the length of password to 3 digits and if we try to press after entering 3 digit
it ll simply not getting entered or displayed on screen

How to go for that ??

Create a variable to hold the count of characters entered. Increment the count each time a character is entered. If 3 or more, stop inputting from the keyboard until you need to enter another password. Then clear the count and start again.

Please post the whole program. Snippets are often less than helpful.

Thanks Sir . . But can you elaborate in the form of script required to add . . Just that part only

Please post the whole program.

if(key == '#')  
    {
      lcd.setCursor(0,1);               
      lcd.print("   ");

                    
                    
                    if  (input_password == password_1)     
                    {
                       digitalWrite(13, 1);
                    }
else 
                                        { 
                                                  //Serial.println("password is incorrect, try again"); 
                                                  lcd.setCursor(0,1);
                                                  lcd.print("Denied");
                                                  delay(5);     
                                                 lcd.setCursor(0,1);   
                                                 lcd.print("      ");
        
                                              
                                                 }


                                            
                                                 
                            input_password = ""; // reset the input password
      
    }
else 
    {
      if(input_password.length() == 0) 
    {
        lcd.clear();
    }
      input_password += key; // append new character to input password string
      lcd.setCursor(input_password.length(), 1); // move cursor to new position
      lcd.print(key);
}

As asked, please post the COMPLETE program.

Before copying from the IDE, please use tools -> autoformat in the IDE; your indentations seem to be all over the show.

Please post the whole program.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.