#include <Keypad.h>
#include <ESP32Servo.h>
#include <ezButton.h>

// define numero de filas
const uint8_t ROWS = 4;
// define numero de columnas
const uint8_t COLS = 4;
// define la distribucion de teclas
char keys[ROWS][COLS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};
// pines correspondientes a las filas
uint8_t colPins[COLS] = { 4, 0, 2, 15 };
// pines correspondientes a las columnas
uint8_t rowPins[ROWS] = { 19, 18, 5, 17 };
// crea objeto con los prametros creados previamente
Keypad customKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
#define BUTTON_PIN 16 // ESP32 pin GPIO21 connected to button's pin
#define SERVO_PIN  21 // ESP32 pin GPIO26 connected to servo motor's pin

ezButton button(BUTTON_PIN); // create ezButton object that attach to pin 7;
Servo myservo;                 // create servo object to control a servo

// variables will change:
int angle = 0; // the current angle of servo motor
char Str[16] = {' ', ' ', ' ', ' ', ' ', ' ', '-', '*', '*', '*', ' ', ' ', ' ', ' ', ' ', ' '};  
int character = 0;
int activated =0;

void setup() {
  Serial.begin(9600);         // initialize serial
  button.setDebounceTime(50); // set debounce time to 50 milliseconds
  myservo.attach(SERVO_PIN);    // attaches the servo on pin 9 to the servo object

  myservo.write(angle);
}

void loop() {
  button.loop(); // MUST call the loop() function first

  if (button.isPressed()) {
    // change angle of servo motor
    if (angle == 0)
      angle = 180;
    else if (angle == 180)
      angle = 0;

    // control servo motor arccoding to the angle
    Serial.print("The button is pressed => rotate servo to ");
    Serial.print(angle);
    Serial.println("°");
    myservo.write(angle);
  }
      
///////////////KEYPAD OPEN/CLOSE////////////  
  char customKey = customKeypad.getKey(); //this function reads the presed key
  
  if (customKey){

    if (character ==0)
    {  
    Serial.println(customKey);
    Str[6]= customKey;   
   
    }

    if (character ==1)
    {  
    Serial.println(customKey);
    Str[7]= customKey;   
   
    }

    if (character ==2)
    {  
    Serial.println(customKey);
    Str[8]= customKey;   
   
    }

    if (character ==3)
    {  
    Serial.println(customKey);
    Str[9]= customKey;   
   
    }

    if (character ==4)
    {  
    Serial.println(customKey);
    Str[10]= customKey;
    activated=1;
   
    }
    character=character+1;
  }

  if (activated == 1)
    {
/*Change your password below!!! 
Change each of Str[6], Str[7], Str[8], Str[9]*/

    if(Str[10]='A' && character==5 && Str[6]=='1' && Str[7]=='2' && Str[8]=='3' && Str[9]=='4' )
    {
      myservo.write(180);

      activated = 2;
      
    }
    else
    {
      character=0;
      Str[6]= '-';
      Str[7]= '*'; 
      Str[8]= '*'; 
      Str[9]= '*';
      Str[10]= ' ';
      activated = 0;
    }
  }
  if (activated == 2)
    {
    if(customKey == 'B' )
    {
      myservo.write(0);
      activated = 0;
      character=0;
      Str[6]= '-';
      Str[7]= '*'; 
      Str[8]= '*'; 
      Str[9]= '*';
      Str[10]= ' ';   
     
    }
  }  
}