Keypad/Password Programming (Multiple

Hello everyone. This is my first post here, so I'm sorry if this is in the wrong place. I searched, but didn't find any similar posts.

So, here's my problem.

I am using a keypad and passwords to control (right now just an led) my door lock. I have made code to work with one or more passwords of my choosing, that is fine.

However, I've been trying to work on coding for a new feature called "Guest Mode." In this, I can use a "Lock" password on the keypad to control whether or not a Guest password would be accepted.

I'm trying to figure it out, but when I use the Lock password, the Guest password still works.

Any help would be greatly appreciated. Thank you for your help!

AdamFier

//keypad with password


#include 
#include 

Servo myservo; //create servo object to control a servo

const byte myRows = 4;  // number of rows
const byte myCols = 3;  //number of columns

char keys[myRows][myCols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
}; //character array to map the button layout of the keypad

byte rowPins[myRows] = {7, 2, 3, 5 }; //array to map keypad to MCU pins
byte colPins[myCols] = {6, 8, 4 }; //array to map keypad to MCU pins

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, myRows, myCols ); //keypad library map function

char inputArray[4]; //array to gather user keypad presses
char Main[4] = {'4','6','2','7'}; //array to hold keypad password
char Guest[4] = {'2','4','6','8'}; //array to hold the guest password
char Lock[4] = {'1','1','1','1'}; //array to hold the lock password

#define ledPin 13 //led to register keypad pressses
#define registerPin 12 //led that registers correct password entry
#define ServoPin 10 //servo that registers correct password entry

int i = 0;
int pos = 0; //variable to store the servo position


int open()
{
  digitalWrite(registerPin, HIGH); //turn on registerPin led
  delay(1000);
  digitalWrite(registerPin, LOW); 
  return 0;
};


int MasterGuest()
{                                             //MasterGuest
  {
    //match input array to password array

    if ((inputArray[0] == Main[0] &&
    inputArray[1] == Main[1] &&
    inputArray[2] == Main[2] &&
    inputArray[3] == Main[3]) || 
    (inputArray[0] == Guest[0] &&
    inputArray[1] == Guest[1] &&
    inputArray[2] == Guest[2] &&
    inputArray[3] == Guest[3]))
    {
      open();
    }
  }


  {
    if (inputArray[0] == Lock[0] &&
    inputArray[1] == Lock[1] &&
    inputArray[2] == Lock[2] &&
    inputArray[3] == Lock[3])
    {
      Master();
    }
  }
}



int Master()
{
  {
   //match input array to password array

  if ((inputArray[0] == Main[0] &&
  inputArray[1] == Main[1] &&
  inputArray[2] == Main[2] &&
  inputArray[3] == Main[3]))
    {
    open();
    }
  }


  {
  if (inputArray[0] == Lock[0] &&
  inputArray[1] == Lock[1] &&
  inputArray[2] == Lock[2] &&
  inputArray[3] == Lock[3])
    {
    MasterGuest();
    }
  }
}




  
  
  
  
void setup()
{
  Serial.begin(9600); //open serial porrt
  pinMode(ledPin, OUTPUT); //define led pin as output
  pinMode(registerPin, OUTPUT); //define led pin as output
  myservo.attach(10); //attaches the servo on pin 1 to the servo object
}

void loop()
{
  char key = kpd.getKey();

  //if a key is presseds
  if(key)
  {

    //turn on ledPin
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);
    delay(100);
    inputArray[i] = key; //store entry into array
    i++;
    Serial.println(key); //print keypad character entry to serial port

    if (key=='*')
    {
      Serial.println("Reset");
      i=0; //reset i
    }

    if (i == 4) //if 4 presses have been made
    {
      MasterGuest();
    
      {
       i=0;
      }
    } 
  }
}

Loops and indentation are your friends. As is the "#" button on the post editor toolbar. If you click on "Modify", then higlight all the code in your post, then click on the "#" button on the editor's toolbar, the code in your post will be enclosed in a tidy box.

"Master" and "MasterGuest" are functions, and don't need semicolons at the end of their declarations.

Thank you for your reply and your help so far.

I changed the formatting to make everything clearer and tidied everything up in the post.

What did you mean by loops being my friends? What does that mean?

Also, I removed the semicolons from the declarations of "Master" and "MasterGuest," but I didn't notice any changes? What does removing them do?

Thank you again.

Adam

I'm trying to figure it out, but when I use the Lock password, the Guest password still works.

In MasterGuest, you check to see if the master password or the guest password was entered. If so, you open the door.

Then, you check to see of the lock password was entered.

Seems to me that you want to see if the master password was entered. If so, open the lock. Then, see if the lock password was entered. If it was, skip checking the guest password. Otherwise, check the guest password. If it was entered, open the door.

Have a look at memcmp

Hey guys,

thanks for your help, but I figured it out.

I used a variable (int State = ?) and while states to do what I wanted. It works perfectly for now.

Thanks.

//keypad with password


#include 

const byte myRows = 4;  // number of rows
const byte myCols = 3;  //number of columns

char keys[myRows][myCols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
}; //character array to map the button layout of the keypad

byte rowPins[myRows] = {7, 2, 3, 5 }; //array to map keypad to MCU pins
byte colPins[myCols] = {6, 8, 4 }; //array to map keypad to MCU pins

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, myRows, myCols ); //keypad library map function

char inputArray[4]; //array to gather user keypad presses
char Main[4] = {'4','6','2','7'}; //array to hold keypad password
char Guest[4] = {'2','4','6','8'};
char Lock[4] = {'1','1','1','1'};
char Unlock[4] = {'2','2','2','2'};

#define ledPin 13 //led to register keypad pressses
#define registerPin 12 //led that registers correct password entry

int i = 0;
int state = 1; //Creates the state

void setup()
{
Serial.begin(9600); //open serial porrt
pinMode(ledPin, OUTPUT); //define led pin as output
pinMode(registerPin, OUTPUT); //define led pin as output

}

void loop()
{
 {
   
 while (state = 1)

  {
       
       
  char key = kpd.getKey();

  //if a key is presseds
    if(key)
      {

      //turn on ledPin
      digitalWrite(ledPin, HIGH);
      delay(100);
      digitalWrite(ledPin, LOW);
      delay(100);
      inputArray[i] = key; //store entry into array
      i++;
      Serial.println(key); //print keypad character entry to serial port

      if (key=='*')
        {
          Serial.println("Reset");
          i=0; //reset i
        }

      if (i == 4) //if 4 presses have been made
        {
          {

          //match input array to password array

            if (inputArray[0] == Main[0] &&
            inputArray[1] == Main[1] &&
            inputArray[2] == Main[2] &&
            inputArray[3] == Main[3])
               {
                digitalWrite(registerPin, HIGH); //turn on registerPin led
                delay(1000);
                digitalWrite(registerPin, LOW);
               }
          }
          
          {

          //match input array to password array

            if (inputArray[0] == Guest[0] &&
            inputArray[1] == Guest[1] &&
            inputArray[2] == Guest[2] &&
            inputArray[3] == Guest[3])
               {
                digitalWrite(registerPin, HIGH); //turn on registerPin led
                delay(1000);
                digitalWrite(registerPin, LOW);
               }
          }
          
          {

          //match input array to password array

            if (inputArray[0] == Lock[0] &&
            inputArray[1] == Lock[1] &&
            inputArray[2] == Lock[2] &&
            inputArray[3] == Lock[3])
               {
                Serial.println("State 2");
                state = 2;
                break;
               }
          }
          
          {
          i=0; //reset i
          }
        }
      }
    };
   }
  
 {
  
  while (state = 2)

  {
       
       
  char key = kpd.getKey();

  //if a key is presseds
    if(key)
      {

      //turn on ledPin
      digitalWrite(ledPin, HIGH);
      delay(100);
      digitalWrite(ledPin, LOW);
      delay(100);
      inputArray[i] = key; //store entry into array
      i++;
      Serial.println(key); //print keypad character entry to serial port

      if (key=='*')
        {
          Serial.println("Reset");
          i=0; //reset i
        }

      if (i == 4) //if 4 presses have been made
        {
          {

          //match input array to password array

            if (inputArray[0] == Main[0] &&
            inputArray[1] == Main[1] &&
            inputArray[2] == Main[2] &&
            inputArray[3] == Main[3])
               {
                digitalWrite(registerPin, HIGH); //turn on registerPin led
                delay(1000);
                digitalWrite(registerPin, LOW);
               }
          }
          
          {

          //match input array to password array

            if (inputArray[0] == Unlock[0] &&
            inputArray[1] == Unlock[1] &&
            inputArray[2] == Unlock[2] &&
            inputArray[3] == Unlock[3])
               {
                Serial.println("State 1"); 
                state = 1;
                break;
               }
          }
          
          {
          i=0; //reset i
          }
        }
      }
    };
  
 }
  
  
}
      if (i == 4) //if 4 presses have been made
        {
          {

          //match input array to password array

            if (inputArray[0] == Main[0] &&
            inputArray[1] == Main[1] &&
            inputArray[2] == Main[2] &&
            inputArray[3] == Main[3])
               {
                digitalWrite(registerPin, HIGH); //turn on registerPin led
                delay(1000);
                digitalWrite(registerPin, LOW);
               }
          }

          {

Can you explain what all the open and close braces are for, and which ones are needed?

Many of these are not.