Open door with keypad and servo

Hi, was wondering if anyone can help me with my project. I am trying to create a door opener using an Arduino UNO, 4x4 keypad and servo. The green LED lights up and the servo turns when the correct password is entered. The red LED lights up when the incorrect password is entered and the servo does nothing. Everything works well except for the servo which turns immediately as I plug in the USB cable.

Attached are the sketch and schematics. Below is the code.

Thanks in advance! :slight_smile:

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


Servo myservo; //declares servo
Password password = Password( "123" ); 

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

byte rowPins[ROWS] = { 5, 4, 3, 2 };
byte colPins[COLS] = { 9, 8, 7, 6 };


// 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(12, OUTPUT);  //green light
  pinMode(11, OUTPUT);  //red light
  myservo.attach(13); //servo on digital pin 9 //servo
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
  }

void loop(){
  keypad.getKey();
  myservo.write(0);
  }
  //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 box
    
    Serial.println("Accepted");
    Serial.write(254);delay(10);
    //Add code to run if it works
    myservo.write(5); //160deg
    
        digitalWrite(11, HIGH);//turn on
    delay(500); //wait 5 seconds
    digitalWrite(11, LOW);// turn off
    
    
}else{
    Serial.println("Denied"); //if passwords wrong keep box locked
    Serial.write(254);delay(10);
    //add code to run if it did not work
    myservo.write(0);
    digitalWrite(12, HIGH); //turn on
    delay(500); //wait 5 seconds
    digitalWrite(12, LOW);//turn off
    
}
}

  myservo.attach(13); //servo on digital pin 9 //servo

Stupid comment, when it doesn't match the code.

You can write a position to the servo before attaching it. It will hold still if the position written to it is the position it is in.

  keypad.getKey();

This function returns a value. It is pointless to call it if you don't care what the value is.

  myservo.attach(13); //servo on digital pin 9 //servo

    myservo.write(5); //160deg

    delay(500); //wait 5 seconds

Just three examples of where the comment does not match the command on the same line. Is this just a lazy programmer or is this disorientation by design? Cleanup that code and post a version that you expect to work, we depend on the comments to show what you expect from the code!

You provided "wiring diagrams" made in fritzing that shows why many forum members refuse to accept any fritzing output. If you are to lazy to make a breadboard view where connections are visible or a schematic where all ratsnest connections are transfered to proper wires, you should simply draw your stuff on a piece of paper, it's probably much more readable.

Thanks for your tips. I am new to this so please ignore the obvious mistakes. I changed the position of some of the components but didn't update the comments.

There are some good combination lock projects on line and in Simon Monks books you ought to study , for example :

can i see the protoype ? i very thanked to this code's owner because i can program it well . but still my project doesnt go well . so i want to know why by checking everything properly .