Limit the password to 3 digits only in keypad 4x4

I have this code for password locking system operating 2 Locks

#include <Keypad.h>

const int ROW_NUM    = 4; //four rows
const int COLUMN_NUM = 4; //four columns
char keys[ROW_NUM][COLUMN_NUM] = {
  {'1','2','3', 'A'},
  {'4','5','6', 'B'},
  {'7','8','9', 'C'},
  {'*','0','#', 'D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );

#define password_Length 2
char userInput[password_Length];

char password[2];
char initial_password[2];

int currentposition=0;

const String password_1 = "111"; // change your password here
const String password_2 = "2222"; // change your password here

String input_password;
char customKey;
byte pressCount = 0;

voidSetup()
{
}

void loop()

{
char key = keypad.getKey();
  if (key)
  {
   
    if(key == 'A')   
    {
      input_password = "";
      lcd.setCursor(0, 1);
      lcd.print("Reset");
      delay(500);
    }

    else if 
    (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);
      }

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

                    
                    
               if  (input_password == password_1)                   
             		{
               		digitalWrite(PIN11,1);
             		}
	  else if  (input_password == password_2)                   
             		{
               		digitalWrite(PIN12,1);
             		}
          
	  else 
             	{ 

              	lcd.setCursor(0,1);
              	lcd.print("Denied");
              	delay(5);
	      
             	}

	  input_password = "";
	  
    }

   else 
    {
      if(input_password.length() == 0) 
      input_password += key; // append new character to input password string
      lcd.setCursor(input_password.length(), 1); // move cursor to new position
      lcd.print(key);                  // print key showing the character
      //lcd.print('*');               // print * key as hiden character
    }
    
}

I got it here in forum only and very thankful for that

I find no problem and its working very fine

But I want to limit the length of digit to 3 and the system should not allow to enter more than 3 digits

I mean when someone is going to press 1234
then after 123 nothing will get displayed and nothing is going to happen
It ll not proceed to display after 123

Please guide me with above code

So many people advised me so many things and i added all that in code but its not working

The code you posted does not compile.

ya may be due to absence of library

I too had same issue at the beginning

But le me check it again

Library, lcd is not defined, and missing closing brackets

YA PLEASE GO THROUGH THIS ONE ITS WORKING

#include <Keypad.h>

#include <LCD_I2C.h>
LCD_I2C lcd(0x27, 16, 2);

const int pins[2] = {38, 39};

const int ROW_NUM    = 4; //four rows
const int COLUMN_NUM = 4; //four columns
char keys[ROW_NUM][COLUMN_NUM] = {
  {'1','2','3', 'A'},
  {'4','5','6', 'B'},
  {'7','8','9', 'C'},
  {'*','0','#', 'D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );

#define password_Length 2
char userInput[password_Length];

char password[2];
char initial_password[2];

int currentposition=0;

const String password_1 = "111"; // change your password here
const String password_2 = "222"; // change your password here

String input_password;
char customKey;
byte pressCount = 0;

const byte PIN38 = 38;
const byte PIN39 = 39;

void setup()
{
pinMode (PIN38, OUTPUT);
digitalWrite(PIN38, LOW);
  
pinMode (PIN39, OUTPUT);
digitalWrite(PIN39, LOW);
}

void loop()

{
char key = keypad.getKey();
  if (key)
  {
   
    if(key == 'A')   
    {
      input_password = "";
      lcd.setCursor(0, 1);
      lcd.print("Reset");
      delay(500);
    }

    else if 
    (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);
      }

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

                    
                    
               if  (input_password == password_1)                   
                {
                  digitalWrite(PIN38,1);
                }
    else if  (input_password == password_2)                   
                {
                  digitalWrite(PIN39,1);
                }
          
    else 
              { 

                lcd.setCursor(0,1);
                lcd.print("Denied");
                delay(5);
        
              }

    input_password = "";
    
    }

   else 
    {
      if(input_password.length() == 0) 
      input_password += key; // append new character to input password string
      lcd.setCursor(input_password.length(), 1); // move cursor to new position
      lcd.print(key);                  // print key showing the character
      //lcd.print('*');               // print * key as hiden character
    }
    
}
}
}

Please let me know !!!

The braces are a bit wonky:

    else if (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);
      }

      else if (key == '#')
      {

This will only process the '#' (Enter) key if it is also the 'B' (Delete) key! You want:

    else if (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);
      }
    }
    else if (key == '#')
    {

Note: This means there is an extra '}' somewhere else in the sketch.

    else
    {
      if (input_password.length() == 0)
        input_password += key; // append new character to input password string
      lcd.setCursor(input_password.length(), 1); // move cursor to new position
      lcd.print(key);                  // print key showing the character
      //lcd.print('*');               // print * key as hiden character
    }

This will only let you add a key to 'input_password' if the length of 'input_password' is zero. In other words, it only allows 1-digit passwords.

To allow for 3-digit password, change to:
if (input_password.length() < 3)

1 Like

I think this:

  if (key)
  {
    if (key == 'A')
    {
      input_password = "";
      lcd.setCursor(0, 1);
      lcd.print("Reset");
      delay(500);
    }
    else if (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);
      }
      else if (key == '#')
      {
        lcd.setCursor(0, 1);
        lcd.print("   ");
        if  (input_password == password_1)
        {
          digitalWrite(PIN38, 1);
        }
        else if  (input_password == password_2)
        {
          digitalWrite(PIN39, 1);
        }
        else
        {
          lcd.setCursor(0, 1);
          lcd.print("Denied");
          delay(5);
        }
        input_password = "";
      }
      else
      {
        if (input_password.length() == 0) input_password += key; // append new character to input password string
        lcd.setCursor(input_password.length(), 1); // move cursor to new position
        lcd.print(key);                  // print key showing the character
        //lcd.print('*');               // print * key as hiden character
      }
    }
  }

should be:

  if (key)
  {
    if (key == 'A')
    {
      input_password = "";
      lcd.setCursor(0, 1);
      lcd.print("Reset");
      delay(500);
    }
    else if (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);
      }
    }
    else if (key == '#')
    {
      lcd.setCursor(0, 1);
      lcd.print("   ");
      if  (input_password == password_1)
      {
        digitalWrite(PIN38, 1);
      }
      else if  (input_password == password_2)
      {
        digitalWrite(PIN39, 1);
      }
      else
      {
        lcd.setCursor(0, 1);
        lcd.print("Denied");
        delay(5);
      }
      input_password = "";
    }
    else
    {
      if (input_password.length() == 0) input_password += key; // append new character to input password string
      lcd.setCursor(input_password.length(), 1); // move cursor to new position
      lcd.print(key);                  // print key showing the character
      //lcd.print('*');               // print * key as hiden character
    }
  }

These things are easy to spot if you format your code properly.

This doesn't make sense to me. It will constrain your password length to 1.

if (input_password.length() == 0) input_password += key; // append new character to input password string

But still I can write 111111111111 so many times with no limit

I'm not sure how. I used the following stripped down code in my simulator and I could only enter 1 character for the password. If you press the '1' key and then press the '2' key then it just continues to echo 1 because it doesn't take any more input. If you press 'A' or 'B' then it will take another character.

void loop()
{
  char key = keypad.getKey();
  if (key)
  {
    if (key == 'A')
    {
      input_password = "";
      Serial.println("Reset");
      delay(500);
    }
    else if (key == 'B')
    {
      if (input_password.length() > 0)
      {
        input_password.remove(input_password.length() - 1);
      }
    }
    else if (key == '#')
    {
      Serial.println("Compare password");
      input_password = "";
    }
    else
    {
      if (input_password.length() == 0) input_password += key; // append new character to input password string
    }
    Serial.println(input_password);
  }
}

If you change:

if (input_password.length() == 0) input_password += key;

to:

if (input_password.length() < 3) input_password += key;

You can constrain the password length to 3.

1 Like

Ok le me check with that

I am following right now here everything

Yess that's the only solution i just need to introduce else nothing

Sir how to display "Wrong pass code" when a wrong code is entered ??

Change "Denied" to "Wrong pass code" here:

Ya but it's not displaying denied word also here don't know what happen :pensive:

Ya started displaying as delay time was 5 so converted it to 500

Working !!! Thanks for all your support :blush:

Is there any option to manipulate such delay etc values via keypad only and without connecting it to IDE via laptop etc ?

Sir why always I need to press number of times I want to enter the same ??

Like if I want to enter 111 then I need to press Key 1 for 3 times

Any automatic entry option for triple 1 while pressing it for 1 or 2 second ??

Like an auto-repeat? You will have to switch from "getKey()" to "getKeys()". See: File -> Examples -> Keypad -> MultiKey". That will let you know when each key is pressed or released. Then you can keep a list of keys that are still pressed and use millis() to repeat that key if it has been held 'long enough'. There is even a 'HELD' event for the initial timer but for a second repeat you need to do your own timing.

Ya auto repeat !

I see . . Le me go through all that first

Thanksss !!!

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