Hello,
This program is to automate a small greenhouse that I am working on. It consists of a fan, a pair of lights (on separate relays), and a humidifier. The Uno is connected to an 8 channel relay, though only four are used. I am using a DHT11 sensor to monitor the humidity and to toggle the humidifier on or off.
The problem that I am running into is that when I use large numbers as the const unsigned long for the millis integer value, the component (ie. fan or lights) will turn on but not back off. From what I can tell, values under a minute in length work. I have spent hours trying to figure it out to no avail, so I hope that one of you will be able to find the issue. I have posted the code below. Thanks in advance!
#include <dht.h>
dht DHT;
const unsigned long eventTime_lightOn = 16UL*60*60*1000; //16 hrs
const unsigned long eventTime_lightOff = 8UL*60*60*1000; //8 hrs
const unsigned long eventTime_fanOn = 15UL*1000*60; //15 mins
const unsigned long eventTime_fanOff = 30*; //30 mins
unsigned long previousTime_humDelay = 0;
unsigned long previousTime_light = 0;
unsigned long previousTime_fan = 0;
int hum_relayPosition = 0;
int fan_relayPosition = HIGH;
int light_relayPosition = LOW;
int fanInterval = 0;
int lightInterval = 0;
#define DHT11_PIN A0
#define ledStripRelay 2 //light
#define humidifierRelay 3 //humidifier
#define topLightRelay 4 //top light
#define fanRelay 5 //fan
void setup() {
Serial.begin(9600); //Set rate for communicating with phone
delay(500);//Delay to let system boot
Serial.print("Current humidity = ");
Serial.print(DHT.humidity);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(DHT.temperature);
Serial.println("C ");
pinMode(ledStripRelay, OUTPUT); //Set ledStripRelay as an output
pinMode(humidifierRelay, OUTPUT); //Set humidifierRelay as an output
pinMode(topLightRelay, OUTPUT); //Set ledStripRelay as an output
pinMode(fanRelay, OUTPUT); //Set humidifierRelay as an output
digitalWrite(ledStripRelay,light_relayPosition);
digitalWrite(humidifierRelay,hum_relayPosition);
digitalWrite(topLightRelay,light_relayPosition);
digitalWrite(fanRelay,fan_relayPosition);
}
void loop() {
DHT.read11(DHT11_PIN);
unsigned long currentTime = millis();
// Serial.print("Current humidity = ");
//Serial.print(DHT.humidity);
//Serial.print("% ");
/*Humidifier Program*/
if((unsigned long) (currentTime - previousTime_humDelay>= 5000)){
previousTime_humDelay = currentTime;
// Serial.print("Current humidity = ");
// Serial.print(DHT.humidity);
//Serial.print("% ");
if ((hum_relayPosition == 0) && (DHT.humidity <= 85)){
digitalWrite(humidifierRelay,LOW); //high means on. if the humidity is below 60, the relay will turn on.
delay(100);
digitalWrite(humidifierRelay,HIGH);
hum_relayPosition = 1;
//Serial.print("Humidity On ");
}
else if ((hum_relayPosition == 1) && (DHT.humidity >= 90)){
digitalWrite(humidifierRelay,LOW);//low means off. if the humidity is above 70, the relay will turn off.
delay(100);
digitalWrite(humidifierRelay,HIGH);
delay(100);
digitalWrite(humidifierRelay,LOW);
delay(100);
digitalWrite(humidifierRelay,HIGH);
delay(100);
digitalWrite(humidifierRelay,LOW);
delay(100);
digitalWrite(humidifierRelay,HIGH);
delay(100);
digitalWrite(humidifierRelay,LOW);
delay(100);
digitalWrite(humidifierRelay,HIGH);
hum_relayPosition = 0;
//Serial.print("Humidity OFF ");
}}
/*Lights Program*/
if ((unsigned long) (currentTime - previousTime_light>= lightInterval )) {
previousTime_light = currentTime;
if(light_relayPosition == HIGH){
light_relayPosition = !light_relayPosition;
lightInterval = eventTime_lightOn;
//Serial.print("LIGHTS OFF");
}
else {
light_relayPosition = !light_relayPosition;
lightInterval = eventTime_lightOff;
//Serial.print("LIGHTS On ");
}
digitalWrite(ledStripRelay,light_relayPosition);
digitalWrite(topLightRelay,light_relayPosition);
}
/*Fan Program*/
if ((unsigned long)(currentTime - previousTime_fan >= fanInterval)){
previousTime_fan = currentTime;
if (fan_relayPosition == HIGH) {
fan_relayPosition = LOW;
fanInterval = eventTime_fanOn;
Serial.print("Fan On ");
}
else {
fan_relayPosition = HIGH;
fanInterval = eventTime_fanOff;
Serial.print("Fan Off ");
}
digitalWrite(fanRelay,fan_relayPosition);
}
}