#include <DHT.h>
#define DHTPIN1 25 // Pin connected to DHT sensor 1 (adjust as needed)
#define DHTPIN2 26 // Pin connected to DHT sensor 2 (adjust as needed)
#define DHTPIN3 33 // Pin connected to DHT sensor 3 (adjust as needed)
#define DHTTYPE DHT22 // DHT sensor type (DHT22)
#define FANPIN1 18 // Pin connected to fan 1
#define FANPIN2 19 // Pin connected to fan 2
#define BUTTON_PIN 27 // Pin connected to the button
DHT dht1(DHTPIN1, DHTTYPE); // DHT object for sensor 1
DHT dht2(DHTPIN2, DHTTYPE); // DHT object for sensor 2
DHT dht3(DHTPIN3, DHTTYPE); // DHT object for sensor 3
int buttonState = LOW; // Current button state
int lastButtonState = LOW; // Previous button state
int newLastButtonState = LOW;
unsigned long fanButtonTimer = 0; // Time when button was pressed
int isSensorActive = 0;
float humidity1;
float humidity2;
float humidity3;
void setup() {
Serial.begin(9600); // Start serial communication for debugging (optional)
pinMode(FANPIN1, OUTPUT); // Set fan 1 pin as output
pinMode(FANPIN2, OUTPUT); // Set fan 2 pin as output
pinMode(BUTTON_PIN, INPUT_PULLUP); // Set button pin as input with pull-up resistor
dht1.begin(); // Initialize DHT sensor 1
dht2.begin(); // Initialize DHT sensor 2
dht3.begin(); // Initialize DHT sensor 3
}
void loop() {
// Read button state and handle manual override
buttonState = digitalRead(BUTTON_PIN);
if (buttonState != lastButtonState && buttonState == LOW) { // Button state changed and Button pressed
if (fanButtonTimer == 0) { // Button pressed for the first time
fanButtonTimer = millis();
newLastButtonState = HIGH;
isSensorActive = 1; // Start timer
digitalWrite(FANPIN1, HIGH);
digitalWrite(FANPIN2, HIGH);
} else { // Button pressed again (within 5 mins)
fanButtonTimer = 0;
newLastButtonState = LOW; // Reset timer (fans turn off)
digitalWrite(FANPIN2, LOW); // Fan 2 off
digitalWrite(FANPIN1, LOW);
isSensorActive = 0; // Fan 1 off
}
}
// Turn off fans after 5 minutes if button was pressed
if (fanButtonTimer > 0 && millis() - fanButtonTimer >= 5 * 1000) {
digitalWrite(FANPIN1, LOW);
digitalWrite(FANPIN2, LOW);
newLastButtonState = LOW;
fanButtonTimer = 0;
}
lastButtonState = buttonState; // Update last button state
// // Optional: Print sensor readings for debugging (uncomment to use)
// Serial.print("Humidity 1: ");
// Serial.println(buttonState);
// Serial.println(" ");
// Serial.println(isSensorActive);
humidity3 = dht3.readHumidity();
// Check humidity only if button is not pressed
if (newLastButtonState == LOW) { // Button not pressed (active low)
if (isSensorActive == 1) {
humidity1 = dht1.readHumidity();
humidity2 = dht2.readHumidity();
Serial.print(humidity1);
Serial.print(" ");
Serial.println(humidity2);
}
// Control fans based on humidity for sensor 1
if (humidity1 > humidity3 + 10) {
digitalWrite(FANPIN1, HIGH); // Fan 1 on
} else {
digitalWrite(FANPIN1, LOW); // Fan 1 off
}
// Control fans based on humidity for sensor 2
if (humidity2 > humidity3 + 10) {
digitalWrite(FANPIN2, HIGH); // Fan 2 on
} else {
digitalWrite(FANPIN2, LOW); // Fan 2 off
}
}
delay(100); // Delay between readings (adjust if needed)
}