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!
#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
 Â
}
}