I am working on my first Arduino project and while it generally has been going smoothly I ran into a roadblock I just can't figure out how to solve. Any thoughts or suggestions would be very welcome.
Goal: I would like to turn an exhaust fan on and off in order to keep the humidity and temperature in my shed close to ambient. As designed I am using a Nano-Every to read the temp and humidity from two DHT-11 sensors, one inside and one outside the shed. The existing code then smooths the reading to give a rolling average of the conditions, then finds the delta between the two. If the delta is greater than 10 degrees or 10% RH, it turns on a relay for five minutes.
The issue is that everything is working except the delay on the timer. Yes I can get it to work by just adding a delay, but then because of the way the rolling average is computer it takes a long time for it to finally turn off.
What I am hoping is to find some way to keep the relay on for the five minutes, while the Arduino is still logging the change in humidity and temperature.
//Global Configuration
//Sensor
#include "DHT.h"
#define DHT1PIN 2 //Inside Sensor 1
#define DHT2PIN 4 //Outside Sensor 2
#define DHT1TYPE DHT11
#define DHT2TYPE DHT11
DHT dht1(DHT1PIN, DHT1TYPE);
DHT dht2(DHT2PIN, DHT2TYPE);
//Smoothing
float TA1 = 10; //nominal start values
float TA2 = 10;
float HA1 = 50;
float HA2 = 50;
//relay
int relay_pin = 11;
void setup() {
//Serial
Serial.begin(9600);
Serial.println("DHTxx test!");
//Sensor
dht1.begin();
dht2.begin();
//relay
pinMode(relay_pin, OUTPUT);
digitalWrite(relay_pin, HIGH);
}
void loop() {
delay(1000);
//Sensor
float h1 = dht1.readHumidity();
float t1 = dht1.readTemperature();
float h2 = dht2.readHumidity();
float t2 = dht2.readTemperature();
//Serial print
if (isnan(t1) || isnan(h1)) {
Serial.println("Failed to read from DHT #1");
} else {
Serial.print("Humidity 1: ");
Serial.print(h1);
Serial.print(" %\t");
Serial.print("Temperature 1: ");
Serial.print(t1);
Serial.println(" *C");
}
if (isnan(t2) || isnan(h2)) {
Serial.println("Failed to read from DHT #2");
} else {
Serial.print("Humidity 2: ");
Serial.print(h2);
Serial.print(" %\t");
Serial.print("Temperature 2: ");
Serial.print(t2);
Serial.println(" *C");
}
//Smoothing
// Sensor 1 inside shed
// Sensor 2 outside shed
{
HA1 = (HA1*.95)+(h1*.05); //Weight of new vs old
Serial.print("H1 Average: ");
Serial.print(HA1);
Serial.print(" %\t");
TA1 = (TA1*.95)+(t1*.05);
Serial.print("T1 Average: ");
Serial.print(TA1);
Serial.println(" *C");
HA2 = (HA2*.95)+(h2*.05);
Serial.print("H2 Average ");
Serial.print(HA2);
Serial.print(" %\t");
TA2 = (TA2*.95)+(t2*.05);
Serial.print("T2 Average ");
Serial.print(TA2);
Serial.println(" *C");
}
int Hdelta = (HA1-HA2);
int Tdelta = (TA1-TA2);
Serial.print("H∆ ");
Serial.print(Hdelta);
Serial.println("%\t");
Serial.print("T∆ ");
Serial.print(Tdelta);
Serial.println(" *C");
Serial.println();
//relay control
{
if (Hdelta > 10 || Tdelta > 10) {
digitalWrite(relay_pin, LOW);
delay(60000);}
else
digitalWrite(relay_pin, HIGH);
}
}