else if
(
(f<=(TEMPPOINT) && digitalRead(PUMP)==ON && digitalRead(INTFANS)==ON && digitalRead(OUTFANS)==ON)
//if cooling unit is on turn off interior fans when I reach 68 wait minutes and then turn off pump and exterior fans
)
{
unsigned long currentMillis = millis(); //grab current time
digitalWrite(INTFANS,OFF);
((unsigned long)(currentMillis - previousMillis) >= interval))
digitalWrite(PUMP,OFF);
digitalWrite(OUTFANS,OFF);
}
Please post your entire sketch.
Often, something done wrong in one part of the sketch can prevent another part of the sketch from doing its job.
#include <DHT.h>
// Globals for DHT22 Sensor
#define DHTPIN 2 // what pin we are connected too on DHT11
#define DHTTYPE DHT22 // DHT 22
DHT dht(DHTPIN, DHT22);
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE) ; // Set the LCD I2C address
int HUMIFAN = 8; // Humidifier Fan pin 8
int OUTFANS = 10; // Exterior Cooling Fans pin 10
int PUMP = 11; // Coolant pump pin 11
int INTFANS = 7; // Interior Cooling Fans pin 7
int HEATER = 6; // Peltier Module pin 6
#define ON false // relay actually on
#define OFF true // relay actuall off
int i =0;
//humidity control points
#define HUMIPOINT 68 // Humidity setpoint
#define HDEADBAND 1.5 // Humidity deadband
//Temperature control points
#define TEMPPOINT 68 // Temperature setpoint
#define TEMPDBAND 2 // Temperature deadband
// Water Level Sensor 40mm setup
const int read = A3; // Sensor A0 pin to Arduino pin A0 changed to A3
int value; // Variable to store the incoming data
unsigned long interval=120000; // the time i need to leave cycle on for
unsigned long previousMillis=0; // millis() returns an unsigned long
void setup() {
pinMode(HUMIFAN, OUTPUT);
digitalWrite(HUMIFAN, OFF);
pinMode(OUTFANS, OUTPUT);
digitalWrite(OUTFANS, OFF);
pinMode(PUMP, OUTPUT);
digitalWrite(PUMP, OFF);
pinMode(INTFANS, OUTPUT);
digitalWrite(INTFANS, OFF);
pinMode(HEATER, OUTPUT);
digitalWrite(HEATER, OFF);
// put your setup code here, to run once:
//Begin Serial communication with Water Level Sensor
Serial.begin(9600);
Serial.println("DHT11 Test!");
dht.begin();
lcd.begin(20,4);
lcd.setCursor(0,0);
lcd.print("Temperature F:");
lcd.setCursor(0,1);
lcd.print("Humidity %:");
lcd.setCursor(0,2);
lcd.print("Water Level:");
}
void loop() {
delay(10000);
// put your main code here, to run repeatedly:
float h = dht.readHumidity();
float f = dht.readTemperature(true);
// check if returns are valid, if they areNAN (not a number) then something went wrong!
if (isnan(f) || isnan(h)) {
lcd.setCursor(0,0);
lcd.print("DHT11 Failed to Read!!");
Serial.println("DHT11 Failed to Read!!");
return;
} else {
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(f);
Serial.print(" *F");
}
lcd.setCursor(15,0);
lcd.print(f,2); // prints Temperature value on LCD to 2 decimals
lcd.setCursor(15,1);
lcd.print(h); // prints Humidity value on LCD
if (f>(TEMPPOINT + TEMPDBAND)) // if temperature is above 70 turn cooling unit on
{
digitalWrite(PUMP,ON);
digitalWrite(INTFANS,ON);
digitalWrite(OUTFANS,ON);
}
else if
((f<=(TEMPPOINT) && digitalRead(PUMP)==ON && digitalRead(INTFANS)==ON && digitalRead(OUTFANS)==ON));
//if cooling unit is on turn off interior fans when I reach 68 wait 2 minutes and then turn off pump and exterior fans
{
digitalWrite(INTFANS,OFF); [color=blue]//not sure about the code here[/color]
unsigned long currentMillis = millis(); //grab current time
if ((unsigned long)(currentMillis - previousMillis) >= interval)
{
digitalWrite(PUMP,OFF);
digitalWrite(OUTFANS,OFF);
previousMillis = millis();
}
if (f<(TEMPPOINT-TEMPDBAND)) // if temperature is below 66 turn heating unit on
{
digitalWrite(HEATER,ON);
digitalWrite(PUMP,ON);
digitalWrite(INTFANS,ON);
}
else if
(f>=(TEMPPOINT) && digitalRead(HEATER)==ON && digitalRead(PUMP)==ON && digitalRead(INTFANS)==ON)
// if heating unit is on turn it off when I reach 68
{
digitalWrite(HEATER,OFF);
digitalWrite(INTFANS,OFF);
digitalWrite(OUTFANS,ON); [color=blue]// also not sure about code in this area[/color]
unsigned long currentMillis = millis(); // grab current time
if ((unsigned long)(currentMillis - previousMillis) >=interval)
{
digitalWrite(PUMP,OFF);
digitalWrite(OUTFANS,OFF);
previousMillis = millis();
}
if (h>HUMIPOINT + HDEADBAND){
digitalWrite(HUMIFAN,OFF);
}else {
if (h<HUMIPOINT - HDEADBAND)
digitalWrite(HUMIFAN,ON);
}
{
value = analogRead(read);// Read data from Water Level Sensor analog pin and store to value variable
}
if (value<=310)
{
Serial.println(value);
lcd.setCursor(15,2);
lcd.print("Add ");
}
else if (value>310 && value<=340)
{
Serial.println(value);
lcd.setCursor(15,2);
lcd.print("Low ");
}
else if (value>340 && value<=495)
{
Serial.println(value);
lcd.setCursor(15,2);
lcd.print("Ok ");
}
else if (value>495 && value<=545)
{
Serial.println(value);
lcd.setCursor(15,2);
lcd.print("Good");
}
else if (value>545 && value<=700)
{
Serial.println(value);
lcd.setCursor(15,2);
lcd.print("Full");
}
// Try to read digital pins are On or Off
if (digitalRead(HUMIFAN)==ON && digitalRead(HEATER)==OFF && digitalRead(OUTFANS)==OFF)
{
lcd.setCursor(0,3);
lcd.print(" Humidifying ");
}
else if (digitalRead(HUMIFAN)==OFF && digitalRead(HEATER)==OFF && digitalRead(OUTFANS)==OFF)
{
lcd.setCursor(0,3);
lcd.print(" PERFECTO!! ");
}
else if (digitalRead(HUMIFAN)==OFF && digitalRead(HEATER)==OFF && digitalRead(OUTFANS)==ON)
{
lcd.setCursor(0,3);
lcd.print(" Cooling ");
}
else if (digitalRead(HUMIFAN)==OFF && digitalRead(HEATER)==ON && digitalRead(OUTFANS)==OFF)
{
lcd.setCursor(0,3);
lcd.print(" Heating ");
}
else if (digitalRead(HUMIFAN)==ON && digitalRead(HEATER)==OFF && digitalRead(OUTFANS)==ON)
{
lcd.setCursor(0,3);
lcd.print("Humidifying/Cooling ");
}
else if (digitalRead(HUMIFAN)==ON && digitalRead(HEATER)==ON && digitalRead(OUTFANS)==OFF)
{
lcd.setCursor(0,3);
lcd.print("Humidifying/Heating ");
}
}
}
}
You are setting previousMillis to millis() in 2 places in the code. I have not waded through your code but that instinctively feels wrong to me. Are you timing two separate events ? If so you need to different variables for the previous start time.
I don't see any statement of the problem. If the cycle not stopping is the problem, I would print currentMillis and previousMillis and their difference each time through the code and see what the values are. That will tell you if the difference is growing as you think it should and you can see if something is being missed.
Serial.print can be your friend!
Mike
Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor you will not have access to this useful tool. I recommend you to use the standard IDE instead.
The demo Several Things at a Time illustrates the use of millis() to manage timing. It may help with understanding the technique.
Notice how I have divided my code into several small functions. That makes development and debugging much easier.
...R