I ask for help to end sketch

guys I’m trying to help end my project with a door unlock the keyboard. My problem is to add a button in the sketch which will act as the servant back to the home position each time it is pressed, can anyone help?

#include <Keypad.h>
#include <Servo.h>
#define LED 13 //  Green Led
#define LED 12// Red Led
int Buzzer = 9;
Servo myservo;

const byte ROWS = 4;
const byte COLS = 3;
char keyInsert[5];
int i = 0;
int j = 0;
int s = 0;
int x = 0;
char code[5] = "1997";
char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};
byte rowPins[ROWS] = {8, 7, 6, 5};
byte colPins[COLS] = {4, 3, 2};

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

void setup() {
  Serial.begin(9600);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(9, OUTPUT);
  myservo.attach(10);
}

void loop() {  //insert PIn to verify
  char key = keypad.getKey();
  if (i == 0) {
    i++;
  }
  if (key != NO_KEY && j < 4) {
    Serial.print("*");
    //Serial.println(key);
    keyInsert[j] = key;
    j++;
  }
  if (key == '*') {  // Verifyng the code
    delay(100);
    for (s = 0; s < 4; s++) {
      if (keyInsert[s] == code[s]) {
        x++;
      }
    }
    if (x == 4) { //Password Correct
      Serial.begin(9600);
      digitalWrite(13, HIGH);
      digitalWrite(9, HIGH);
      delay(50);
      digitalWrite(9, LOW);
      delay(100);
      delay(400);
      digitalWrite(13, LOW);
      myservo.write(0);
      delay(1000);
    


    } else { //Password Wrong
      digitalWrite(12, HIGH);
      delay(400);
      x = 0;
      i = 0;
      j = 0;
      digitalWrite(12, LOW);
    }

  }
  if (key == '#') {
    x = 0;
    i = 0;
    j = 0;
  }
}

a door unlock the keyboard

What does this mean? Do you mean a keypad unlocks a door perhaps? Your code gives no real clue what it does.

which will act as the servant back to the home position each time it is pressed

I don’t really understand what you’re saying here either. Do you mean that you want the servo to ‘home’ when a key is pressed?

And a couple of programming points:-

      if (x == 4)   //Password Correct
        {
            Serial.begin(9600);         // *** You don't need this. IT's already done in 'setup()'
            digitalWrite(13, HIGH);
            digitalWrite(9, HIGH);
            delay(50);
            digitalWrite(9, LOW);
            delay(100);                  // *** Why two delays?
            delay(400);                  // *** Why two delays?
            digitalWrite(13, LOW);
            myservo.write(0);
            delay(1000);
        }

Edit: Also, this won’t work:-

#define LED 13 //  Green Led
#define LED 12// Red Led

You need to give each LED a different name.

Another thing. To make your code more readable, make good use of the names you’ve given your variables. Here’s your code, with the suggested changes:-

#include <Keypad.h>
#include <Servo.h>
#define greenLED 13     // Green Led
#define redLED 12       // Red Led
int Buzzer = 9;
Servo myservo;

const byte ROWS = 4;
const byte COLS = 3;
char keyInsert[5];
int i = 0;
int j = 0;
int s = 0;
int x = 0;
char code[5] = "1997";
char keys[ROWS][COLS] =
{
    {'1', '2', '3'},
    {'4', '5', '6'},
    {'7', '8', '9'},
    {'*', '0', '#'}
};
byte rowPins[ROWS] = {8, 7, 6, 5};
byte colPins[COLS] = {4, 3, 2};

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

void setup()
{
    Serial.begin(9600);
    pinMode(redLED, OUTPUT);
    pinMode(greenLED, OUTPUT);
    pinMode(Buzzer, OUTPUT);
    myservo.attach(10);
}

void loop()    //insert PIn to verify
{
    char key = keypad.getKey();
    if (i == 0)
        i++;
    if (key != NO_KEY && j < 4)
    {
        Serial.print("*");
        //Serial.println(key);
        keyInsert[j] = key;
        j++;
    }
    if (key == '*')    // Verifyng the code
    {
        delay(100);
        for (s = 0; s < 4; s++)
        {
            if (keyInsert[s] == code[s])
                x++;
        }
        if (x == 4)   //Password Correct
        {
            digitalWrite(greenLED, HIGH);
            digitalWrite(Buzzer, HIGH);
            delay(50);
            digitalWrite(Buzzer, LOW);
            delay(500);
            digitalWrite(greenLED, LOW);
            myservo.write(0);
            delay(1000);
        }
        else   //Password Wrong
        {
            digitalWrite(redLED, HIGH);
            delay(400);
            x = 0;
            i = 0;
            j = 0;
            digitalWrite(redLED, LOW);
        }
    }
    if (key == '#')
    {
        x = 0;
        i = 0;
        j = 0;
    }
}

And you still need to make it clear exactly what you want.

What I see is …

There is a keypad where the 4-digit secret code is “1997”.
The “*” key is like the ENTER Key to verify the Secret Code.

If the Secret Code is entered followed by “*” button then …

  1. The GREEN LED (Pin 13) is ON for about 550 milliseconds
  2. The Buzzer (Pin 9) is on for about 50 milliseconds
  3. The Lock / Servo is turned to open the door.
  4. But the variables i, j and x are not reset to 0.

If the Wrong Code is entered follow by “*” button then …

  1. The RED LED (Pin 12) is ON for 400 milliseconds
  2. The variables i, j and x are reset to 0.

If the “#” ( RE-LOCK ? ) button is pressed then …

  1. The variables i, j and x are reset to 0.
  2. But the Lock / Servo is not re-locked <<< “servant back to the home position” ?