Delay and Millis together

My code has 7sec delays while valves open before the fans start.

Im trying to use millis to have fans start for 30sec every 6 hours.

Problem is delay and millis dont seem to mix, How do i keep the 7 sec delay and not effect the milis timiing?

#include <DS3232RTC.h>

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define DHTPIN 11  // what pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11  // DHT 11
#define DHTTYPE DHT22  // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
//DHT dht(DHTPIN, DHTTYPE, 30);
int humidfan = 2;          //  Intake fan for Humid box
int humidv = 3;            //  Valve for humud intake
int freshfan = 4;          // fresh fan
int freshv = 5;            //  fresh valve, air from outside
int exfan = 6;             // exhaust fan
int exv = 7;               // exhaust valve
int dryfan = 8;            // intake fan for dry box
int dryv = 9;              // dry box valve
const int buttonPin = 10;  // button to reset cycle
// DHT pin 11
int DHTpower = 12;  // constant 5v to DHT11
int fridge = 13;    // relay to fridge 240v

int buttonState = 0;  // variable for reading the pushbutton

float lowDry, highDry;
float lowCure, highCure;
//bool cycle;

unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 30000;  //the value is a number of milliseconds

LiquidCrystal_I2C lcd(0x27, 20, 4);  // Set the LCD address to 0x27 for a 20x4 display


double dewPoint(double celsius, double humidity) {
  // (1) Saturation Vapor Pressure = ESGG(T)
  double RATIO = 373.15 / (273.15 + celsius);
  double RHS = -7.90298 * (RATIO - 1);
  RHS += 5.02808 * log10(RATIO);
  RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1 / RATIO))) - 1);
  RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1);
  RHS += log10(1013.246);

  // factor -3 is to adjust units - Vapor Pressure SVP * humidity
  double VP = pow(10, RHS - 3) * humidity;

  // (2) DEWPOINT = F(Vapor Pressure)
  double T = log(VP / 0.61078);  // temp var
  return (241.88 * T) / (17.558 - T);
}

void setup() {
  pinMode(humidfan, OUTPUT);
  pinMode(dryfan, OUTPUT);
  pinMode(fridge, OUTPUT);
  pinMode(humidv, OUTPUT);
  pinMode(dryv, OUTPUT);
  pinMode(exfan, OUTPUT);
  pinMode(exv, OUTPUT);
  pinMode(freshfan, OUTPUT);
  pinMode(freshv, OUTPUT);
  pinMode(DHTpower, OUTPUT);
  pinMode(buttonPin, INPUT);

  digitalWrite(humidfan, LOW);
  digitalWrite(dryfan, LOW);
  digitalWrite(humidv, LOW);
  digitalWrite(dryv, LOW);
  digitalWrite(exfan, LOW);
  digitalWrite(exv, LOW);
  digitalWrite(freshfan, LOW);
  digitalWrite(freshv, LOW);
  digitalWrite(DHTpower, HIGH);
  digitalWrite(fridge, HIGH);
  digitalWrite(buttonPin, HIGH);

  Serial.begin(9600);
  lcd.init();       // Initialize the LCD
  lcd.backlight();  // Turn on the backlight
  lcd.clear();      // Clear the LCD screen
  lcd.setCursor(4, 0);
  lcd.print("The Green");
  lcd.setCursor(5, 1);
  lcd.print("Ninja");
  delay(2000);  // Display the startup message for 2 seconds
  lcd.clear();
  dht.begin();

  startMillis = millis();  //initial start time
  lowCure = 10.5;
  highCure = 11.5;  // aim for 11

  lowDry = 11.5;
  highDry = 12.5;  // aim for 12

  if (buttonState = 1; (digitalRead(buttonPin))) {
    lowCure = lowDry;
    highCure = highDry;
    lcd.setCursor(0, 3);
    lcd.print("Dry Cycle / Storage");
    delay(2000);
  } else {
    lowDry = lowCure;
    highDry = highCure;
    lcd.setCursor(0, 3);
    lcd.print("Cure Cycle       ");
    delay(2000);
  }
}

void loop() {

  currentMillis = millis();

  if (currentMillis - startMillis >= period)
  digitalWrite(exv, HIGH);
  digitalWrite(freshv, HIGH);
  digitalWrite(humidv, LOW);
  digitalWrite(dryv, LOW);
  digitalWrite(fridge, HIGH);
  //lcd.setCursor("0, 2");
  lcd.print("Exhaust fans on");
  digitalWrite(freshfan, HIGH);
  digitalWrite(exv, HIGH);
  delay(30000);
  currentMillis = millis();

  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  float f = dht.readTemperature(true);
  float currentDew = dewPoint(t, h);  // Dew Point for setting pins high


  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    lcd.clear();
    lcd.print("Failed to read");
    lcd.setCursor(0, 1);
    lcd.print("from DHT sensor");
    return;
  }

  // Compute heat index
  // Must send in temp in Fahrenheit!
  float hi = dht.computeHeatIndex(f, h);
  float hiDegC = dht.convertFtoC(hi);


  if (f < 19.5) {
    digitalWrite(fridge, LOW);  // Fridge off when set low
  }

  if (f > 21) {
    digitalWrite(fridge, HIGH);
  }

  if (currentDew > lowDry && (currentDew < highDry)) {  // Level to aim for dew point
    dewPointOk(t, h);
  }
  if (currentDew >= highDry) {  //   High dew point, send to dry box
    dewPointHigh(t, h);
  }
  if (currentDew <= lowDry) {  // Dew point low, send to humid box
    dewPointLow(t, h);
  }
}

void dewPointOk(float t, float h) {
  digitalWrite(humidfan, LOW);
  digitalWrite(humidv, LOW);
  digitalWrite(dryfan, LOW);
  digitalWrite(dryv, LOW);
  digitalWrite(exfan, LOW);
  digitalWrite(exv, LOW);
  digitalWrite(freshfan, LOW);
  digitalWrite(freshv, LOW);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Dew Pnt Ok");
  lcd.setCursor(14, 0);
  lcd.print(dewPoint(t, h));
  lcd.setCursor(19, 0);
  lcd.print("c");

  lcd.setCursor(0, 1);
  lcd.print("Temp");
  lcd.setCursor(14, 1);
  lcd.print(dht.readTemperature());
  lcd.setCursor(19, 1);
  lcd.print("c");

  lcd.setCursor(0, 2);
  lcd.print("Humidity");
  lcd.setCursor(14, 2);
  lcd.print(dht.readHumidity());
  lcd.setCursor(19, 2);
  lcd.print("%");

  lcd.setCursor(0, 3);
  lcd.print("Valves Closed");
}

void dewPointHigh(float t, float h) {
  digitalWrite(humidv, LOW);
  digitalWrite(humidfan, LOW);
  digitalWrite(dryv, HIGH);
  digitalWrite(exv, LOW);
  digitalWrite(exfan, LOW);
  digitalWrite(freshv, LOW);
  digitalWrite(freshfan, LOW);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Dew Pnt High");
  lcd.setCursor(14, 0);
  lcd.print(dewPoint(t, h));
  lcd.setCursor(19, 0);
  lcd.print("c");
  lcd.setCursor(0, 1);
  lcd.print("Temp");
  lcd.setCursor(14, 1);
  lcd.print(dht.readTemperature());
  lcd.setCursor(19, 1);
  lcd.print("c");
  lcd.setCursor(0, 2);
  lcd.print("Humidity");
  lcd.setCursor(14, 2);
  lcd.print(dht.readHumidity());
  lcd.setCursor(19, 2);
  lcd.print("%");
  lcd.setCursor(0, 3);
  lcd.print("Dry Box On");
  delay(7000);
  digitalWrite(dryfan, HIGH);
}

void dewPointLow(float t, float h) {  // humid box on
  digitalWrite(humidv, HIGH);
  digitalWrite(dryv, LOW);
  digitalWrite(exv, LOW);
  digitalWrite(exfan, LOW);
  digitalWrite(freshv, LOW);
  digitalWrite(freshfan, LOW);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Dew Pnt Low");
  lcd.setCursor(14, 0);
  lcd.print(dewPoint(t, h));
  lcd.setCursor(19, 0);
  lcd.print("%");
  lcd.setCursor(0, 1);
  lcd.print("Temp");
  lcd.setCursor(14, 1);
  lcd.print(dht.readTemperature());
  lcd.setCursor(19, 1);
  lcd.print("c");
  lcd.setCursor(0, 2);
  lcd.print("Humidity");
  lcd.setCursor(14, 2);
  lcd.print(dht.readHumidity());
  lcd.setCursor(19, 2);
  lcd.print("%");
  lcd.setCursor(0, 3);
  lcd.print("Humid Box On");
  delay(7000);
  digitalWrite(humidfan, HIGH);
}

Don't do it....pretty simple I'd have thought.

awesome thanks, that fixed it...

You can mix whatever you want.

The sketch and your description are together not enough to understand what you want.

Can you use brackets and simple code ?
Right-click on the code in the Arduino IDE and then select "Format Document".
Then check every bracket, every indent, every space, every comma. Make it look good.


This is a bug:

  if (buttonState = 1; (digitalRead(buttonPin))) {

When turning on all compiler warnings, the compiler does not like that line: "warning: init-statement in selection statements only available with -std=c++1z or -std=gnu++1z".
Use simple code, use more lines if you need to. The function digitalRead() does not return a 1, it returns a HIGH or LOW.

// plain and simple code
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {

It is unclear what you want here:

  if (currentMillis - startMillis >= period)
  digitalWrite(exv, HIGH);
  digitalWrite(freshv, HIGH);
  digitalWrite(humidv, LOW);
  ...

Only the first line is part of the if-statement.
You have this:

  if (currentMillis - startMillis >= period)
  {
    digitalWrite(exv, HIGH);
  }

  digitalWrite(freshv, HIGH);
  digitalWrite(humidv, LOW);
  ...

Please show in the sketch how you are going to use 6 hours with millis(). It has to be 6 hours as a unsigned long.

// 6 hours in milliseconds is:
//   1000 for seconds
//   60 for minutes
//   60 for hours
//   6 for 6 hours.
unsigned long sixHours = 1000UL * 60UL * 60UL * 6UL;  // UL means "Unsigned Long"

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.