Trouble getting servo to work properly with keypad

I’m working on code that moves a servo 90 degrees when the correct password is entered on 4X4 keypad.I’m using an arduino duemilanove with 328 chip and a towerpro sg90 servo. I have used the command myservo.write(90) to turn the servo 90 degrees. I have placed this inside an if statement . However when the code executes, the servo goes to 90 degrees no matter what. I also tried resetting the servo to 0 degrees at the beginning of the loop, but it makes a horrible grinding noise because it is trying to switch from 0 to 90 and back so fast. please help!

if(digitalRead(5)==HIGH && i==8){
    
  delay(250); 
  Serial.println('#');
  pwcount=0;
  for(k=0; k<=4; k++){
   if(k==4){
   Serial.println("Access Granted");
   myservo.write(90);
   delay(1000);
   myservo.write(0);break;
   }
   if(password[k] != entry[k]){
    Serial.println("Denied");
     break; 
  }
  }
  for(k=0; k<10; k++)
  entry[k]=0;
  }

What do the serial debug prints tell you?

Have you noticed that when stuff wanders all over the page,

it is really hard to read?

Try using auto format on your code before posting.

Formatting your code correctly will make the fault in it much more obvious.

How is your setup wired? You may have issues with pin 5 floating high. May be better to set pin 5 high, then pull it low with the button like in the below code.

//zoomkat servo button test 12-29-2011
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }    
  
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
}

Here is all of the code. Sorry about bad formatting in original post.

#include <Servo.h>
Servo myservo;
int i, k, pwcount=0;
char password[] = "1234", entry[10];
void setup()
{
  Serial.begin(9600);
  myservo.attach(10);




  for(i=2; i<=5; i++)
    pinMode(i, INPUT);
  for(i=6; i<=8; i++)
    pinMode(i, OUTPUT);

}

void loop()
{

  for(i=6; i<=8; i++){
    reset();
    digitalWrite(i, HIGH);
    if(digitalRead(2)==HIGH && i==6){

      entry[pwcount] = '1';
      pwcount++;
      delay(250); 
      Serial.print('1');
    }
    if(digitalRead(3)==HIGH && i==6){

      entry[pwcount] = '4';
      pwcount++;
      delay(250); 
      Serial.print('4');
    }
    if(digitalRead(4)==HIGH && i==6){

      entry[pwcount] = '7';
      pwcount++;
      delay(250); 
      Serial.print('7');
    }  
    if(digitalRead(5)==HIGH && i==6){

      entry[pwcount] = '*';
      pwcount++;
      delay(250); 
      Serial.print('*');
    } 

    if(digitalRead(2)==HIGH && i==7){

      entry[pwcount] = '2';
      pwcount++;
      delay(250); 
      Serial.print('2');
    }
    if(digitalRead(3)==HIGH && i==7){

      entry[pwcount] = '5';
      pwcount++;
      delay(250); 
      Serial.print('5');
    }
    if(digitalRead(4)==HIGH && i==7){

      entry[pwcount] = '8';
      pwcount++;
      delay(250); 
      Serial.print('8');
    }  
    if(digitalRead(5)==HIGH && i==7){

      entry[pwcount] = '0';
      pwcount++;
      delay(250); 
      Serial.print('0');
    } 

    if(digitalRead(2)==HIGH && i==8){

      entry[pwcount] = '3';
      pwcount++;
      delay(250); 
      Serial.print('3');
    }
    if(digitalRead(3)==HIGH && i==8){

      entry[pwcount] = '6';
      pwcount++;
      delay(250); 
      Serial.print('6');
    }
    if(digitalRead(4)==HIGH && i==8){

      entry[pwcount] = '9';
      pwcount++;
      delay(250); 
      Serial.print('9');
    }  
    if(digitalRead(5)==HIGH && i==8){

      delay(250); 
      Serial.println('#');
      pwcount=0;
      for(k=0; k<=4; k++){
        if(k==4){
          Serial.println("Access Granted");
          myservo.write(90);
          delay(1000);
          myservo.write(0);
          break;
        }
        if(password[k] != entry[k]){
          Serial.println("Denied");

          break; 
        }
      }
      for(k=0; k<10; k++)
        entry[k]=0;
    }  
  }
}

void reset(){
  int j;
  for(j=6; j<=8; j++)
    digitalWrite(j, LOW);

}
/* ==================================================================================
 File:      keypad_password_servo.ino 
 Author:	Camilo Nuñez Fernandez
                camilo.nunez@cnf.cl
 Description: A control access with password, 4x4 keypad and a servo, for Arduino.
 ==================================================================================
 //  Ver | dd mmm yyyy | Author  | Description
 // =====|=============|=========|=====================================================
 // 1.0 | 17 Jul 2012  |   CNF   | First release 
 // ==================================================================================
 // 1.1 | 12 Ago 2014  |   CNF   | Second Version, compatible with Arduino 1.0 
 // ==================================================================================*/

#include <Password.h> //http://playground.arduino.cc/uploads/Code/Password.zip 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


Servo myservo; //declares servo
Password password = Password( "your_password" ); //password to unlock box, 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, 4, 3, 2 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 8, 7, 6, 9 };


// 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(11, OUTPUT);  //green light
  pinMode(12, 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 'A': checkPassword(); delay(1); break;
    
    case 'B': 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
    
}
}

http://www.instructables.com/id/Access-control-with-Arduino-Keypad-4x4-Servo/ :grin: