Doorlock.. Proplem with the servo motor

You mean ( 9, min, max,)

Just change it to:
myservo.attach(9);
and set the limits of travel where you write to the servos.

That sets the servo to 90° and reduces the angle to 0°, 10° at a time. But the for loop runs at full speed so you will not see the steps.

That sets the servo to 0° and increases the angle to 90°, 10 steps at a time. Again, the for loop is running at full speed so you will not see the steps.

Change the 90 and 0 to the limits (positions) that you want for open and closed.

So know this numbers will be changed to what?

See edited post #23.

Sorry for being an idiot but i don't understand what you meen by that

The function servo.write takes the position that you want the servo to go to as its argument. I don't know why you are using for loops to move the servos, you could just send them to the required positions.

void ServoClose()
{
  for (pos = 90; pos >= 0; pos -= 10) { 
    myservo.write(pos);
  }
}

void ServoOpen()
{
  for (pos = 0; pos <= 90; pos += 10) {
    myservo.write(pos);  
  }
}

Could be just

void ServoClose()
{  
    myservo.write(0);
}

and

void ServoOpen()
{
    myservo.write(90);    
}

Adjust the 0 and 90 till you get the travel that you want.

1 Like

Ok i will try

When i done it.... It gives me an error here... Saying not declared in the scope

Is Open the name of the function?

If you make changes in your code, please post the new version so that we can keep up.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

  • List item

Love that servo linkage - if you moved the bendy wire to the end of the arm the travel will be longer ( as per my previous post)

It is at the end

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

#define Password_Length 5

Servo myservo;
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);

int pos = 0;

char Data[Password_Length];
char Master[Password_Length] = "1234";
byte data_count = 0, master_count = 0;

bool Pass_is_good;
bool door = false;
char customKey;


/*---preparing keypad---*/

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};


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

Keypad customKeypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);


/*--- Main Action ---*/
void setup()
{
  myservo.attach(9, 2000, 2400);
  ServoClose();
  lcd.begin(16, 2);
  lcd.print("Protected Door");
  loading("Loading");
  lcd.clear();
}


void loop()
{
  if (door == true)
  {
    customKey = customKeypad.getKey();
    if (customKey == '#')
    {
      lcd.clear();
      ServoClose();
      lcd.print("Door is closed");
      delay(3000);
      door = false;
    }
  }
  else
    Open();

}

void loading (char msg[]) {
  lcd.setCursor(0, 1);
  lcd.print(msg);

  for (int i = 0; i < 9; i++) {
    delay(1000);
    lcd.print(".");
  }
}

void clearData()
{
  while (data_count != 0)
  { 
    Data[data_count--] = 0;
  }
  return;
}

void ServoClose()
{
    myservo.write(0);
  
}

void ServoOpen()
{
    myservo.write(90);  
  }

void Open()
{
  lcd.setCursor(0, 0);
  lcd.print("Enter Password");
  
  customKey = customKeypad.getKey();
  if (customKey)
  {
    Data[data_count] = customKey;
    lcd.setCursor(data_count, 1);
    lcd.print(Data[data_count]);
    data_count++;
  }

  if (data_count == Password_Length - 1)
  {
    if (!strcmp(Data, Master))
    {
      lcd.clear();
      ServoOpen();
      lcd.print(" Door is Open ");
      door = true;
      delay(5000);
      loading("Waiting");
      lcd.clear();
      lcd.print(" Time is up! ");
      delay(1000);
      ServoClose();
      door = false;      
    }
    else
    {
      lcd.clear();
      lcd.print(" Wrong Password ");
      door = false;
    }
    delay(1000);
    lcd.clear();
    clearData();
  }
}

my new code

no massage error. but after uploading this code the servo doesnt move now

I would suggest that you move the keypad from pins 0 and 1 to other free pins so that you can open the serial port to use for debugging. Put in some serial prints to follow program execution and variable values.

ammmm. it would be a really good idea...but the problem is that i will have to make another code and another circuit and this is something i am not perfect in it...to clear it up and explain it : the lock is working but not perfect so i want to improve it not adding something that would break it ,,,,lets think again in box: how to make the servo rotate more

I wired up the project with my Uno, keypad, 1602 LCD and servo. It seems to work fine for me. When the correct password is entered, the servo moves about 45° and when it times out the servo moves back to the starting point. What is wrong with how it is working?

The speed that the servo moves is dependent on load and supply voltage. To get the servo to move faster, lighten the load or increase the supply voltage to the maximum allowed by the servo. You are powering the servo with an external power source and not the Arduino, right?

And this is in the code so you are limiting the servo travel.

do you ask me to delete this part?

or to make it 9,min,max

are you using the new code?

if you want to get the 90° travel.

As far as I know. The code is from post #35.