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
}
}
}
};
}