#include "DHT.h"
// Definición de pines
#define DHTPIN 32 // Pin para el DHT11
#define DHTTYPE DHT11
#define LDRPIN 34 // Pin para el LDR
#define LM35PIN 35 // Pin para el LM35
#define POTPIN 34 // Pin para el potenciómetro
// Inicialización de objetos y variables
DHT dht(DHTPIN, DHTTYPE);
int ledPin = 2; // Pin del LED
int ldrValue = 0; // Variable para el valor del LDR
int lm35RawValue = 0; // Variable para el valor crudo del LM35
float lm35Temperature = 0.0f; // Variable para la temperatura del LM35
int potValue = 0; // Variable para el valor del potenciómetro
float dhtTemperature = 0.0f; // Variable para la temperatura del DHT11
float dhtHumidity = 0.0f; // Variable para la humedad del DHT11
// Variables para almacenar los valores anteriores
int lastLdrValue = -1;
float lastLm35Temperature = -1.0f;
float lastDhtTemperature = -1.0f;
float lastDhtHumidity = -1.0f;
int lastPotValue = -1;
// Variables para manejar el tiempo de lectura
unsigned long previousMillis = 0;
const long interval = 1000; // Intervalo de lectura en ms (1 segundo)
void setup() {
Serial.begin(115200);
dht.begin();
pinMode(ledPin, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
// Solo se lee si ha pasado el intervalo definido
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Lectura del LDR
ldrValue = analogRead(LDRPIN);
// Lectura del LM35
lm35RawValue = analogRead(LM35PIN);
float voltageValue = lm35RawValue * 3.3 / 4095.0; // Conversión a voltaje
lm35Temperature = voltageValue * 100; // Conversión a grados Celsius
// Lectura del potenciómetro
potValue = analogRead(POTPIN);
// Lectura del DHT11
dhtHumidity = dht.readHumidity();
dhtTemperature = dht.readTemperature();
// Verificación de fallos en la lectura del DHT11
if (isnan(dhtHumidity) || isnan(dhtTemperature)) {
Serial.println("Fallo al leer del sensor DHT!");
return; // Salir si hay error
}
// Impresión de los datos solo si hay cambios
bool changed = false; // Variable para detectar cambios
if (ldrValue != lastLdrValue) {
changed = true; // LDR ha cambiado
lastLdrValue = ldrValue; // Actualizar el valor anterior
}
if (lm35Temperature != lastLm35Temperature) {
changed = true; // LM35 ha cambiado
lastLm35Temperature = lm35Temperature; // Actualizar el valor anterior
}
if (potValue != lastPotValue) {
changed = true; // Potenciómetro ha cambiado
lastPotValue = potValue; // Actualizar el valor anterior
}
if (dhtTemperature != lastDhtTemperature) {
changed = true; // DHT11 temperatura ha cambiado
lastDhtTemperature = dhtTemperature; // Actualizar el valor anterior
}
if (dhtHumidity != lastDhtHumidity) {
changed = true; // DHT11 humedad ha cambiado
lastDhtHumidity = dhtHumidity; // Actualizar el valor anterior
}
// Si hubo un cambio, imprimir los nuevos valores
if (changed) {
Serial.print(ldrValue); // Luz del LDR
Serial.print(","); // Separador
Serial.print(lm35Temperature); // Temperatura del LM35
Serial.print(","); // Separador
Serial.print(dhtTemperature); // Temperatura del DHT11
Serial.print(","); // Separador
Serial.print(dhtHumidity); // Humedad del DHT11
Serial.print(","); // Separador
Serial.println(potValue); // Valor del potenciómetro
}
// Control del LED basado en el valor del potenciómetro
if (potValue >= 2048) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
}