Arduino mega password lock with lcd, keypad and servo

Hi there,
Like the title says: i need help with a password lock project.

Problem 1:
Everytime i press the 4,5,6 or the 7 everything on the lcd disappears.

Problem 2:
When i enter my code the lcd has to say: Acces Granted, Welcome. Instead of that it all disappears again. Just like when i enter the wrong code.Than it has to say: Acces Denied. But it don’t.

Problem 3:
When i start up my arduino and i enter my code the servo does what is has to do and the green led blinks.
But when i enter my code for the second time (or more) it says my code is wrong.

Who can help me?

#include <Password.h>
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>

Servo myservo;

//constants for LEDs
int greenLED = 22;
int redLED = 23;

LiquidCrystal lcd(13, 12, 11, 5, 4, 3, 2);
Password password = Password( "5144" );
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four 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] = {
  39, 38, 37, 36}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
  43, 42, 41, 40}; //connect to the column pinouts of the keypad

// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
  lcd.begin(16, 2);
  Serial.begin(9600);
  keypad.setDebounceTime(50);
  displayCodeEntryScreen();
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
  myservo.attach(53);
  myservo.write(180);
  //setup and turn off both LEDs
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  digitalWrite(redLED, LOW);
  digitalWrite(greenLED, LOW);
}

void displayCodeEntryScreen()
{
  lcd.clear();
  Serial.begin(9600);
  lcd.setCursor(2, 0);
  lcd.print("Enter Code:");
  lcd.setCursor(0,1);
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
  //setup and turn off both LEDs
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  digitalWrite(redLED, LOW);
  digitalWrite(greenLED, LOW);
}

void loop(){
  keypad.getKey();
}

//take care of some special events
void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){
  case PRESSED:
    lcd.print(eKey);
    switch (eKey){
    case '#': 
      checkPassword();
      break;
    case '*': 
         displayCodeEntryScreen(); break;
    default:
      password.append(eKey);
    }
 switch (keypad.getState()){
   case PRESSED:
   switch (eKey){
   case 'D': myservo.write(180);
    }
  }
  }
}

void checkPassword(){
  if (password.evaluate()){
    digitalWrite(greenLED, HIGH);
    lcd.clear();
    delay(30);
    lcd.setCursor(1, 0);
    lcd.print("Acces Granted");
    lcd.setCursor(4, 1);
    lcd.print("Welcome");
    delay(2500);
    unlockdoor();
    displayCodeEntryScreen();
  }
  else{
    digitalWrite(redLED, HIGH);
    lcd.clear();
    delay(10);
    lcd.setCursor(2, 0);
    lcd.print("Acces Denied");
    delay(2500);
    lcd.clear();
    displayCodeEntryScreen();
  }
}

void unlockdoor(){ //controls servo that locks the door
  myservo.write(90);
  digitalWrite(greenLED, HIGH);
  delay(5000);
}

My guess for Problem 3 is that you aren't clearing the password buffer after you evaluate the password. Since "51445144" does not match "5144" it is not recognized as correct.

Do you know how to reset the password buffer?

TomDeprins:
Hi there,
Like the title says: i need help with a password lock project.

Problem 1:
Everytime i press the 4,5,6 or the 7 everything on the lcd disappears.

Problem 2:
When i enter my code the lcd has to say: Acces Granted, Welcome. Instead of that it all disappears again. Just like when i enter the wrong code.Than it has to say: Acces Denied. But it don't.

these problems suggest a power issue.

how are you powering your keypad, lcd, etc...

Via USB or a 12V computer adapter.
I have the problem with both.

maybe a photo of your circuit would help.

I added a fritzing file with my circuit.
Except for the keypad is everything on the file.
The keypad is connected (from left to right) to the pins: 43,42,41,40 (COLS) and 39,38,37,36 (ROWS).

Untitled Sketch 3.fzz (5.57 KB)

TomDeprins:
Do you know how to reset the password buffer?

Looking at the library documentation (http://playground.arduino.cc/Code/Password) it appears that you do it with:

 password.reset();

I would put that in displayCodeEntryScreen() which is where you go when it's time to look for the password.

I think you should roll the check for 'D' in with the already existing switch statement, rather than in its own switch statement.

Johnwasser, thank you very much. You solved problem 3. :slight_smile:

But i still have the other 2 problems.
And it's not the power to blame. I connected a 19V adapter and it almost broke my board.
I also connected a 12V 3A adapter (from an old laptop) but it didn't help either.
Can somebody please help me. If those 2 problems are solved i'm ready my my project.