Keypad Door Lock

Hello guys!!!
I wanted to create a keypad based door lock for my room but there's a problem.
I'm using the code from 30 arduino projects for the evil genius as a base, but there's something missing which is a button on the inside of the room so that I can have the keypad on the outside of the door just to get in, but use the button to get out.
How can I add this button in the program, using a momentary switch???
I would really aprecciate the help!!!

The code from the book is this:

// Project 27 Keypad door lock

#include <Keypad.h>
#include <EEPROM.h>

char* secretCode = "1234";
int position = 0;

const byte rows = 4; 
const byte cols = 3; 
char keys[rows][cols] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[rows] = {7, 2, 3, 5}; 
byte colPins[cols] = {6, 8, 4}; 
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);

int redPin = 13;
int greenPin = 12;
int solenoidPin = 10;

void setup()                    
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(solenoidPin, OUTPUT);
  loadCode();
  flash();
  lock();
  Serial.begin(9600);
  while(!Serial);
  Serial.print("Code is: "); Serial.println(secretCode);
  Serial.println("Change code: cNNNN");
  Serial.println("Unloack: u");
  Serial.println("Lock: l");
}

void loop()                    
{
  if (Serial.available())
  {
    char c = Serial.read();
    if (c == 'u')
    {
      unlock();
    }
    if (c == 'l')
    {
      lock();
    }
    if (c == 'c')
    {
      getNewCode();
    }
  }
  char key = keypad.getKey();
  if (key == '#')
  {
    lock();
  }
  if (key == secretCode[position])
  {
    position ++;
  }
  else if (key != 0)
  {
    lock();
  }
  if (position == 4)
  {
    unlock();
  }
  delay(100);
}

void lock()
{
  position = 0;
  digitalWrite(redPin, HIGH);
  digitalWrite(greenPin, LOW);  
  digitalWrite(solenoidPin, LOW);
  Serial.println("LOCKED");
}

void unlock()
{
  digitalWrite(redPin, LOW);
  digitalWrite(greenPin, HIGH);  
  digitalWrite(solenoidPin, HIGH);
  Serial.println("UN-LOCKED");
  delay(5000);
  lock();
}


void getNewCode()
{
  for (int i = 0; i < 4; i++ )
  {
    char ch = Serial.read();
    secretCode[i] = ch;
  }
  saveCode();
  flash();flash();
  Serial.print("Code changed to: "); Serial.println(secretCode);
}

void loadCode()
{
  if (EEPROM.read(0) == 1)
  {
    secretCode[0] = EEPROM.read(1);
    secretCode[1] = EEPROM.read(2);
    secretCode[2] = EEPROM.read(3);
    secretCode[3] = EEPROM.read(4);
  }
}

void saveCode()
{
  EEPROM.write(1, secretCode[0]);
  EEPROM.write(2, secretCode[1]);
  EEPROM.write(3, secretCode[2]);
  EEPROM.write(4, secretCode[3]);
  EEPROM.write(0, 1);  
}

void flash()
{
    digitalWrite(redPin, HIGH);
    digitalWrite(greenPin, HIGH);    
    delay(500);
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);    
}

Thank you

How can I add this button in the program, using a momentary switch???
I would really aprecciate the help!!!

Yea, if the button is pressed, call the function Unlock(); then wait a few seconds so you can get out then call lock();

For you button, the easiest way to set it up is to make the button go LOW when pressed. So you will need to make it use the internal pullup resistors, pinMode(buttonPin, INPUT_PULLUP); then check to see if the button goes LOW.

If you use a tactile button, then it may need to be debounced, if you use a softtact button then you shouldn't need to debounce it.

Ohh ok!!!!
That makes sense. Thanks for the sugestion.
Do you think something like this would work?

// Project 27 Keypad door lock

#include <Keypad.h>
#include <EEPROM.h>

char* secretCode = "1234";
int position = 0;

const byte rows = 4; 
const byte cols = 3; 
char keys[rows][cols] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[rows] = {7, 2, 3, 5}; 
byte colPins[cols] = {6, 8, 4}; 
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);

int redPin = 13;
int greenPin = 12;
int solenoidPin = 10;
int buttonPin = 11;
int buttonstate = 0;

void setup()                    
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(solenoidPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  loadCode();
  flash();
  lock();
  Serial.begin(9600);
  while(!Serial);
  Serial.print("Code is: "); Serial.println(secretCode);
  Serial.println("Change code: cNNNN");
  Serial.println("Unlock: u");
  Serial.println("Lock: l");
}

void loop()                    
{
  if (Serial.available())
  {
    buttonstate = digitalRead(buttonPin);
    char c = Serial.read();
    if ((c == 'u')|| buttonstate == LOW)
    {
      unlock();
    }
    if (c == 'l')
    {
      lock();
    }
    if (c == 'c')
    {
      getNewCode();
    }
  }
  char key = keypad.getKey();
  if (key == '#')
  {
    lock();
  }
  if (key == secretCode[position])
  {
    position ++;
  }
  else if (key != 0)
  {

It should, but do you need to lock it again afterwards?

Yes I do.

What would I have to change.

Add this code and take out buttonstate from here.

if ((c == 'u')|| buttonstate == LOW)

unsigned long lockTimer; // global
bool Locked = true; // global

if(buttonstate == LOW)
{
unlock(); // open door
lockTimer = millis(); // rocord current millis()
Locked = false; // allow next IF statement to execute.
}

if((millis() - lockTimer >= 5000) && !Locked) // 5 second duration
{
Locked = true; // door is now locked, so prevent this IF statement from executing
lock();
}

Oh ok thank you again for the help!!!
I was struggling with this project.
I'm still getting started with arduino, so I wanted to base on a project that already existed and add something else to it as well.
Thank you,
Sam