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

#define DHTPIN 12
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

char code[4];
char password[] = "1434"; // Corregido el valor de la contraseña
int cont = 0;
const byte rows = 4;
const byte cols = 4;
char hexaKeys[rows][cols] = {
 {'1','2','3','A'},
 {'4','5','6','B'},
 {'7','8','9','C'},
 {'*','0','#','D'}
};
byte rowPins[rows] = {9, 8, 7, 6};
byte colPins[cols] = {5, 4, 3, 2};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, rows, cols);
Servo servomotor1;
Servo servomotor2;

void setup() 
{
  servomotor1.attach(11);
  servomotor2.attach(10);
  Serial.begin(9600);
  dht.begin();
}

void loop() 
{
  char customKey = customKeypad.getKey();
  delay(100);

  float temperatura = dht.readTemperature();

  servomotor1.write(0);
  servomotor2.write(0);

  if (customKey != NO_KEY)
  {
    code[cont] = customKey;
    Serial.print(code[cont]);
    cont++;
    if (cont == 4)
    {
      if (code[0] == password[0] && code[1] == password[1] && code[2] == password[2] && code[3] == password[3])
      {
        digitalWrite(13, !digitalRead(13)); 
        Serial.println("Correct password");
        
        if (temperatura < 0)
        {
          servomotor1.write(90);
          delay(7000);
          servomotor1.write(0);
        }
        else if (temperatura > 0) // Usando else if para asegurarse de que solo se active un servo
        {
          servomotor2.write(90);
          delay(7000);
          servomotor2.write(0);
        }
      }
      else
      {
        Serial.println("Incorrect password");
      }
      cont = 0;
    }
  }
}