Arduino keypad with password help

Im trying to create a safe using arduino and so far I’ve managed to have default passwords set up so that when the user inputs the correct default password it will turn the servo which will open the safe and also input a password which will then close it. What i am having trouble with is that I want to give the user the option of changing their password to whatever they want after opening it with the default for the very first time and storing it so that from then on out they can use their own custom password. If anyone has any ideas please help

array_password_keypad_working.pde (3.17 KB)

//keypad with password
#include <Keypad.h>
#include <Servo.h>

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] = {8, 7, 6, 5 }; //array to map keypad to MCU pins
byte colPins[myCols] = {4, 3, 2 }; //array to map keypad to MCU pins

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

char inputArray[5]; //array to gather user keypad presses
char Open[5] = {'4','6','2','7','#'}; //array to hold keypad password
char Close[5] = {'0','0','0','0','#'}; //array to hold the guest password
char Lock[5] = {'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 9 //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);
  myservo.write(160);
  delay(100);
  return 0;
};

int close()
{
  myservo.write(10);
  delay(100);
}


int MasterFunction()
{							   
  {
    //match input array to password array
        // array that controls the function for servo to open
    if (inputArray[0] == Open[0] &&
    inputArray[1] == Open[1] &&
    inputArray[2] == Open[2] &&
    inputArray[3] == Open[3] &&
    inputArray[4] == Open[4])
    {
	open();
    }
    
    if (inputArray[0] == Close[0] &&
    inputArray[1] == Close[1] &&
    inputArray[2] == Close[2] &&
    inputArray[3] == Close[3] &&
    inputArray[4] == Close[4])
    {
        close();
    }
  }


  {
    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] == Open[0] &&
  inputArray[1] == Open[1] &&
  inputArray[2] == Open[2] &&
  inputArray[3] == Open[3]&&
  inputArray[4] == Open[4]))
    {
    open();
    }
  }


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








void setup()
{
  myservo.write(10);
  Serial.begin(9600); //open serial porrt
  pinMode(ledPin, OUTPUT); //define led pin as output
  pinMode(registerPin, OUTPUT); //define led pin as output
  myservo.attach(9); //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 == 5) //if 4 presses have been made
    {
	MasterFunction();

	{
	 i=0;
	}
    }
  }
}

Maintain a state variable that tells you whether the safe is open or closed.

If the safe is closed, accept either the default password or the guest password to open it - initialize the guest password to be the same as the default.

If the safe is open, accept any four digit password to close it, copy the input password to the guest password so it will be accepted for open.

What i am having trouble with is that I want to give the user the option of changing their password to whatever they want after opening it with the default for the very first time and storing it so that from then on out they can use their own custom password.

Store the password in EEPROM so it is remembered when the power is off. http://www.arduino.cc/en/Reference/EEPROM

wildbill:
Maintain a state variable that tells you whether the safe is open or closed.

If the safe is closed, accept either the default password or the guest password to open it - initialize the guest password to be the same as the default.

If the safe is open, accept any four digit password to close it, copy the input password to the guest password so it will be accepted for open.

I have tried writing code that would do that…here is what I wrote but somehow it closes the servo back to original position before I have the chance to input a new password.

boolean lid_status = false;

int MasterFunction()
{							   
  {
    //match input array to password array
        // array that controls the function for servo to open
    if (inputArray[0] == Open[0] &&
    inputArray[1] == Open[1] &&
    inputArray[2] == Open[2] &&
    inputArray[3] == Open[3] )
    //inputArray[4] == Open[4])
    {
        Serial.println("Safe opening...");
	open();  
        delay(1000);
        lid_status = true;
    }

 if (inputArray[0] == Close[0] &&
    inputArray[1] == Close[1] &&
    inputArray[2] == Close[2] &&
    inputArray[3] == Close[3] )
    //inputArray[4] == Close[4])
    {
        Serial.println("Safe now closing...");
        close();
        delay(1000);
        lid_status = false;  
    }
  }
}

that was my function and then in the main loop I have this if statement:

 if (lid_status)
    {
      while(i < 3)
      {
       inputArray[i] = key;
       Open[i] = inputArray[i];
       i++;
      }
       close();
    }

any help would be appreciated…thanks in advance

lid_status? How does a name like that, with a value of true or false convey any meaning?

lidOpen = true;

means something that anyone looking at the code can immediately grasp.

Does your code really look like what you posted? If not, then you can see that there really is a reason to use the icon with the # symbol when posting code.

Ok, so I decided to enter the full code as I have it for better legibility, the reason I didnt use lidOpen was because I’ve already named that variable so when the servo is turned to point 160 as a const int at the very top. I changed the variable name to lidUp and rather than use a boolean Im using an int to see if its 0 then it means the lid is closed, if the int is 1 then it means the lid is Open. I figured out how to store the array password so that when a user enters a new password when the lid is open it would overwrite the old one. But now it also overwrites the closing password and doesnt allow me to close it. If I add the close() function to the follwing:

if (lidUp = 1)
    {
        for (int k=0; k<4; k++)
          {
            inputArray[k] = key;
            inputArray[k] = Open[k]; 
          }
         lidUp = 0;
    }

the servo opens and closes right away without giving me the chance to enter a new password.

Here is the full code:

//keypad with password
#include <Keypad.h>
#include <Servo.h>

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

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

const int lidOpen       = 160;             //Servo Value to Open Lid
const int lidClosed     = 10;              //Servo Value to Close Lid

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] = {8, 7, 6, 5 }; //array to map keypad to MCU pins
byte colPins[myCols] = {4, 3, 2 }; //array to map keypad to MCU pins

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

char inputArray[5]; //array to gather user keypad presses
char Open[4] = {'1','2','3','4'}; //array to hold keypad password
char Close[4] = {'0','0','0','0'}; //array to hold the guest password
// char Lock[5] = {'1','1','1','1','#'}; //array to hold the lock password

int ledPin = 13; //led to register keypad pressses
int registerPin = 12; //led that registers correct password entry
int ServoPin = 9; //servo that registers correct password entry

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

int lidUp = 0;  // variable that determines whether the lid is currently closed (0) or open (1)

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

int close()
{
  myservo.write(lidClosed);
  delay(100);
}


int MasterFunction()
{							   
  {
    //match input array to password array
        // array that controls the function for servo to open
    if (inputArray[0] == Open[0] &&
    inputArray[1] == Open[1] &&
    inputArray[2] == Open[2] &&
    inputArray[3] == Open[3] )
    {
        Serial.println("Safe opening...");
        open();  
        delay(1000); 
    }
    else
      {
          Serial.println("Sorry, incorrect password.");
         delay(300);
         digitalWrite (ledPin, HIGH);
         delay(300);
         digitalWrite (ledPin, LOW); 
         delay(300);
         
         digitalWrite (ledPin, HIGH);
         delay(300);
         digitalWrite (ledPin, LOW); 
         delay(300);
         
         digitalWrite (ledPin, HIGH);
         delay(300);
         digitalWrite (ledPin, LOW); 
         delay(300);
      }
      
    if (inputArray[0] == Close[0] &&
    inputArray[1] == Close[1] &&
    inputArray[2] == Close[2] &&
    inputArray[3] == Close[3] )
    {
        Serial.println("Safe now closing...");
        
        close();
        delay(1000);
        lidUp = 0;
        
    }
  }
  
  
}

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

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

  //if a key is pressed
  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 (key=='#')
    {
        Serial.println("Password submitted");
        //MasterFunction();
        i = 0;
    }
    
    if (lidUp = 1)
    {
        for (int k=0; k<4; k++)
          {
            inputArray[k] = key;
            inputArray[k] = Open[k]; 
          }
         lidUp = 0;
    }
    
    if (lidUp = 0)
        close();
  }
}

Couple of typos:

if (lidUp = 1)
if (lidUp = 0)

Should of course be:

if (lidUp == 1)
if (lidUp == 0)

@wildbill: Oh yes, of course silly mistake but regardless whenever I input a password it overwrites the current password (which I want it to do) but then I cant close it using the close password [0,0,0,0]...I would like to make it so that when it is open, it will stay open until I insert a new password and then it will close. And the next time I go to open it, it will require the new password that I inserted before.

Assuming that you fixed those errors, your code looks like this now?

    if (key=='#')
      {
      Serial.println("Password submitted");
      //MasterFunction();
      i = 0;
      }

    if (lidUp == 1)
      {
      for (int k=0; k<4; k++)
        {
        inputArray[k] = key;
        inputArray[k] = Open[k]; 
        }
      lidUp = 0;
      }

    if (lidUp == 0)
      close();

Number of issues:

  • The lidup ifs execute every time you read a key - shouldn’t it be only after you read a ‘#’?
  • lidup is never set to one, so the close function is called every time you read a key
  • You’re overwriting the InputArray with the contents of the Open array - I assume it was supposed to be the other way around