You need to move the code that does the lock activation into it's own routine so you can call it from a successful keypad entry or a button press.
Connect a button from pin 9 to ground and this code should do what you want or at least get you going....
#include <Password.h> //http://playground.arduino.cc/uploads/Code/Password.zip //tells to use password library
#include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip //tells to use keypad library
#include <Servo.h> //tells to use servo library
const int servoPin = 12;
const int ledGreenPin = 11;
const int ledRedPin = 10;
const int buttonPin = 9;
Servo servo;
int servoAngle = 170; // servo position in degrees
Password password = Password( "1924" ); //password to unlock, can be changed
const byte ROWS = 4; // Four rows
const byte COLS = 4; // columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 5, 6, 7, 8 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 14, 15, 16, 17 };
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
Serial.begin(9600);
Serial.write(254);
Serial.write(0x01);
delay(200);
pinMode(ledGreenPin, OUTPUT); //green light
pinMode(ledRedPin, OUTPUT); //red light
pinMode(buttonPin, INPUT_PULLUP);
servo.attach(12);
keypad.setDebounceTime(50);
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
servo.write(170);
}
void loop() {
keypad.getKey();
if ( digitalRead(buttonPin ) == LOW ) {
// button pressed
activateLock();
delay(1000);
}
}
//take care of some special events
void keypadEvent(KeypadEvent eKey) {
switch (keypad.getState()) {
case PRESSED:
Serial.print("Enter:");
Serial.println(eKey);
delay(10);
Serial.write(254);
switch (eKey) {
case '*': checkPassword(); delay(1); break;
case '#': password.reset(); delay(1); break;
default: password.append(eKey); delay(1);
}
}
}
void checkPassword() {
if (password.evaluate()) { //if password is right open
Serial.println("Accepted");
Serial.write(254); delay(10);
//Add code to run if it works
activateLock();
password.reset(); delay(1);
} else {
Serial.println("Denied"); //if passwords wrong keep locked
Serial.write(254); delay(10);
//add code to run if it did not work
// servo.write(170);
digitalWrite(ledRedPin, HIGH); //turn on
delay(500); //wait 5 seconds
digitalWrite(ledRedPin, LOW);//turn off
password.reset(); delay(1);
}
}
void activateLock() {
servo.write(20); //deg
digitalWrite(ledGreenPin, HIGH);//turn on
delay(4500); //wait 5 seconds
digitalWrite(ledGreenPin, LOW);// turn off
digitalWrite(ledRedPin, LOW);// turn off
delay (500);
digitalWrite(ledGreenPin, HIGH);//turn on
digitalWrite(ledRedPin, HIGH);//turn on
delay (500);
digitalWrite(ledGreenPin, LOW);//turn off
digitalWrite(ledRedPin, LOW);//turn off
delay (500);
digitalWrite(ledGreenPin, HIGH);//turn on
digitalWrite(ledRedPin, HIGH);//turn on
delay (500);
digitalWrite(ledGreenPin, LOW);//turn off
digitalWrite(ledRedPin, LOW);//turn off
delay (500);
digitalWrite(ledGreenPin, HIGH);//turn on
digitalWrite(ledRedPin, HIGH);//turn on
delay (500);
digitalWrite(ledGreenPin, LOW);//turn off
digitalWrite(ledRedPin, LOW);//turn off
delay (500);
digitalWrite(ledGreenPin, HIGH);//turn on
digitalWrite(ledRedPin, HIGH);//turn on
delay (500);
digitalWrite(ledGreenPin, LOW);//turn off
digitalWrite(ledRedPin, LOW);//turn off
servo.write(170);
}