const int botonr= 34;
const int botonv= 32;
const int ledv= 21;
const int ledr= 19;
const int leda= 18;
bool statusv= 0;
bool alarmar= 0;
bool alarmaa= 0;
bool calarmaa= 0;
bool calarmar= 0;
bool restricciona= 0;
bool restriccionr= 0;
bool statusf= 0;
int contadort=0;
int contadorh=0;
float buscadort= 0;
float buscadorh= 0;
float anclat=0;
float anclah=0;



#include "DHTesp.h"

const int DHT_PIN = 15;

DHTesp dhtSensor;

void setup() {
  Serial.begin(9600);
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  pinMode(botonv, INPUT_PULLUP);
  pinMode(botonr, INPUT_PULLUP);
  pinMode(ledv, OUTPUT);
  pinMode(ledr, OUTPUT);
  pinMode(leda, OUTPUT);
}

void loop() {
  TempAndHumidity  data = dhtSensor.getTempAndHumidity();
  float h= data.humidity;
  float t= data.temperature;

if (digitalRead(botonv)==LOW){
  statusv=HIGH;
  digitalWrite(ledv, statusv);
}

if (statusv==HIGH && statusf==LOW){
  digitalWrite(ledv, statusv);
  if ((h>50)&&(h<75)){
    alarmaa=1;
  }
  if (alarmaa==HIGH){
    digitalWrite(leda, HIGH);
    delay(100);
    digitalWrite(leda, LOW);
    delay(100);
    calarmaa=1;
  }
  if ((calarmaa==1)&&(restricciona==0)){
    contadorh= contadorh+1;
    restricciona= 1;
  }
  if ((h<50)||(h>75)){
    alarmaa=0;
    restricciona= 0;
    calarmaa= 0;
  }
  if (alarmaa==LOW){
    digitalWrite(leda, LOW);
  }
  if (t>25){
    alarmar=1;
  }
  if (alarmar==HIGH){
    digitalWrite(ledr, HIGH);
    delay(100);
    digitalWrite(ledr, LOW);
    delay(100);
    calarmar=1;
  }
  if ((calarmar==1)&&(restriccionr==0)){
    contadort= contadort+1;
    restriccionr= 1;
  }
  if (t<25){
    alarmar=0;
    restriccionr=0;
    calarmar=0;
  }
  if (alarmar==LOW){
    digitalWrite(ledr, LOW);
  }
  if (digitalRead(botonr)==LOW){
    statusv=LOW;
    statusf=HIGH;
    digitalWrite(ledv, statusv);
  }
}
  if (statusf==HIGH){
    if ((h>50)&&(h<75)){
      digitalWrite(leda, HIGH);

    }
    if ((h<50)||(h>75)){
      digitalWrite(leda, LOW);
    }
    if (t>25){
      digitalWrite(ledr, HIGH);
    }
    if (t<25){
      digitalWrite(ledr, LOW);
    }
    delay(300);
    if ((digitalRead(ledr)==LOW && digitalRead(leda)==LOW)){
      statusf=LOW;
      statusv=HIGH;
    }
  }
  buscadort=t;
  buscadorh=h;

  if (buscadort>anclat){
    anclat=buscadort;
  }
  if (buscadorh>anclah){
    anclah=buscadorh;
  }
  Serial.print("Botonr :   ");
  Serial.println(digitalRead(botonr));
  Serial.print("N° alarmas de temperatura:  ");
  Serial.println(contadort);
  Serial.print("N° alarmas de humedad:  ");
  Serial.println(contadorh);
  Serial.print("Mayor temperatura registrada:   ");
  Serial.println(anclat);
  Serial.print("Mayor humedad registrada:   ");
  Serial.println(anclah);
  delay(50);
  
  delay(500);
}