My Keypad Door Lock.

Hello, this is my first arduino project and I thought I would share it with the community so others can use it. Basically this code uses a keypad, and when the correct code it entered it sends a command to the servo that then turns the lock open. If you have any questions please ask, and if you have any corrections to my code please also list them. In my next revision I want to add a LCD screen and maybe an alarm. If anyone wants to help me with the code to change the guest code by entering a programming mode on the arduino please post it below. This can also be adapted to use a stepper motor, all you need to do is replace the servo commands with appropriate stepper motor commands. I also will upload a schematic if you have problems hooking the connections up.

Revisions:
1.0.0- Original code released.
1.0.1- added a lot of comments to explain.

//Coded by: ironicDeveloper 12/18/2011
//Help & parts of code from arduino forum
//if you use this code all I ask is you leave the header in it.
#include <Keypad.h>
#include <Servo.h>

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 3, 2}; //connect to the column pinouts of the keypad

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

char inputArray[4]; //password length
char Main[4] = {'4','6','2','7'}; //main or master password
char Guest[4] = {'1','2','3','4'}; //guest password

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

int i = 0;
int state = 1; //automatically sets to locked state

Servo mrservo;  //creates servo object to control the servo

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

}

void loop()
{
 {

 while (state = 1)

  {


  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 (i == 4) //if 4 presses have been made
	  {
	    {

	    //match input array to password array

		if (inputArray[0] == Guest[0] &&
		inputArray[1] == Guest[1] &&
		inputArray[2] == Guest[2] &&
		inputArray[3] == Guest[3])
		   {
                    // moves servo after correct code entered.
		    mrservo.write(75);
                    delay(15000); 
                    mrservo.write(30); 
                    delay(500);
                
		   }
	    }
	    {
	    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);
		   }
	    }

	    {
	    i=0; //reset i
	    }
	  }
	}
    };

 }

Hi ironicDeveloper!

Thanks for sharing this bit of code, it really helped me out with a project involving a keypad, a stepper motor and my pillar drill!

Thanks again for sharing!

KayWarner

i have a question. Did you do the door lock with a keypad, servo motor and an LCD display?

hi im doing similar project, and i have question. let say I add 1 LED(represents as siren), pressing incorrect keys (3 times) that will make LED turn On. How to program that? Using array and for fuctions?

in relation with my previous question above.

My objective: pressing pushSwitch (3 times) will make redLed turn on.

Can't figure it out.

int redLed = 3;
int pushSwitch = 2;

void setup()

{
  
  pinMode(redLed, OUTPUT);
  pinMode(pushSwitch, INPUT);
}

int i;

void loop()
{

for (i =0; i < 4; i+1);

if (i ==3)
{
  digitalWrite(redLed, HIGH);
}

}

i hope it not too late :~

int redLed = 3;
int pushSwitch = 2;
int array[3]={0,0,0};

void setup()

{
  
  pinMode(redLed, OUTPUT);
  pinMode(pushSwitch, INPUT);
  digitalWrite(redLed, LOW);
}


int i=0;
int x=0;
void loop(){
if(array[0]==1&&array[1]==1&&array[2]==1){
  digitalWrite(redLed, HIGH);
}else if(digitalRead(pushSwitch)==HIGH){
  for(i=x;i<3;i++){
  if(array[i]==0){
    if(digitalRead(pushSwitch)==HIGH){
    array[i]=1;
    delay(130);
    x++;
  }
  }
  }
}else{
  digitalWrite(redLed, LOW);
}
}

Hello, I'm doing a similar project, but what if I want to allow the user to reset the password. Any help/advice will be appreciated. Thanks !

I have one question. When the "electric power" is stopped and started, in the starter my arduino uno make all the digital output in "high" for few seconds, so the door open. How avoid this?