My little code got massive with a lot of help from this forum.
Thanks everyone that helps out.
My code turns on and off relays that control dew point and temp. It now runs one setting for four days then changes the dew point for four days.
How can i make it go back to the original dew point after another 4 days. So four days at 12 then four days at 11, then back to 12 indefinitely?
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
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);
}
#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 intakefan = 2; // Intake fan for Humid box and dry box
int humidv = 3; // Valve for humud intake
int dryv = 4; // dry box valve
int exfan = 5; // exhaust fan
int exv = 6; // exhaust valve
int returnfan = 7; // fan to bring air from inside box
int returnv = 8; // return valve, air from inside box
//int lock = 9; // Electric lock
int fridge = 13; // relay to fridge 240v
int DHTpower = 12; // constant 5v to DHT11
unsigned long startMillis; //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 30000000; //the value is a number of milliseconds
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16x2 display
void setup() {
pinMode(intakefan, OUTPUT);
pinMode(fridge, OUTPUT);
pinMode(humidv, OUTPUT);
pinMode(dryv, OUTPUT);
pinMode(exfan, OUTPUT);
pinMode(exv, OUTPUT);
pinMode(returnfan, OUTPUT);
pinMode(returnv, OUTPUT);
pinMode(DHTpower, OUTPUT);
digitalWrite(intakefan, LOW);
digitalWrite(humidv, LOW);
digitalWrite(dryv, LOW);
digitalWrite(exfan, LOW);
digitalWrite(exv, LOW);
digitalWrite(returnfan, LOW);
digitalWrite(returnv, LOW);
digitalWrite(DHTpower, HIGH);
digitalWrite(fridge, LOW);
Serial.begin(9600);
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.clear(); // Clear the LCD screen
lcd.setCursor(3, 0);
lcd.print("The Green");
lcd.setCursor(4, 1);
lcd.print("Ninja");
delay(2000); // Display the startup message for 2 seconds
lcd.clear();
dht.begin();
startMillis = millis(); //initial start time
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
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 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) {
digitalWrite(fridge, LOW);
}
if (f > 21) {
digitalWrite(fridge, HIGH);
}
// first 4 days
currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (currentMillis - startMillis <= period) //test less than period
if (currentDew > 11.5 && (currentDew < 12.5))
; // Level to aim for dew point 12
{
digitalWrite(intakefan, LOW);
digitalWrite(humidv, LOW);
digitalWrite(dryv, LOW);
digitalWrite(exfan, LOW);
digitalWrite(exv, LOW);
digitalWrite(returnfan, LOW);
digitalWrite(returnv, LOW);
lcd.print("Dew point OK");
lcd.setCursor(0, 1);
lcd.print(dewPoint(t, h));
lcd.setCursor(5, 0);
lcd.print("c");
delay(2000);
lcd.clear();
lcd.print("Temperature");
lcd.setCursor(0, 1);
lcd.print(dht.readTemperature());
lcd.setCursor(5, 0);
lcd.print("c");
delay(2000);
lcd.clear();
lcd.print("Humidity");
lcd.setCursor(0, 1);
lcd.print(dht.readHumidity());
lcd.setCursor(5, 0);
lcd.print("%");
delay(2000);
lcd.clear();
lcd.print("Dry Cycle");
delay(2000);
}
if (currentDew > 12.5)
;
{ // High dew point, send to dry box
digitalWrite(humidv, LOW);
digitalWrite(dryv, HIGH);
digitalWrite(exv, HIGH);
digitalWrite(returnv, HIGH);
lcd.clear();
lcd.print("Dew Point High");
lcd.setCursor(0, 1);
lcd.print(dewPoint(t, h));
lcd.setCursor(5, 0);
lcd.print("c");
delay(2000);
lcd.clear();
lcd.print("Temperature");
lcd.setCursor(0, 1);
lcd.print(dht.readTemperature());
lcd.setCursor(5, 0);
lcd.print("c");
delay(2000);
lcd.clear();
lcd.print("Dry Box On");
lcd.setCursor(0, 1);
lcd.print("Fans On");
digitalWrite(intakefan, HIGH);
digitalWrite(exfan, HIGH);
digitalWrite(returnfan, HIGH);
delay(2000);
lcd.clear();
lcd.print("Humidity");
lcd.setCursor(0, 1);
lcd.print(dht.readHumidity());
lcd.setCursor(5, 0);
lcd.print("%");
delay(2000);
}
if (currentDew < 11.5)
;
{ // Dew point low, send to humid box
digitalWrite(humidv, HIGH);
digitalWrite(dryv, LOW);
digitalWrite(exv, HIGH);
digitalWrite(returnfan, LOW);
digitalWrite(returnv, LOW);
lcd.print("Dew point LOW");
lcd.setCursor(0,1);
lcd.print(dewPoint(t, h));
lcd.setCursor(5,0);
lcd.print("c");
delay(2000);
digitalWrite(intakefan, HIGH);
digitalWrite(exfan, HIGH);
lcd.clear();
lcd.print("Humid Box On");
lcd.setCursor(0, 1);
lcd.print("Fans On");
delay(2000);
lcd.clear();
lcd.print("Dew Point Low");
lcd.setCursor(0, 1);
lcd.print(dewPoint(t, h));
delay(2000);
lcd.clear();
lcd.print("Temperature");
lcd.setCursor(0, 1);
lcd.print(dht.readTemperature());
delay(2000);
lcd.clear();
lcd.print("Humidity");
lcd.setCursor(0, 1);
lcd.print(dht.readHumidity());
lcd.clear();
//
// after 4 days // //
if (currentMillis - startMillis >= period) //test whether the period has elapsed
if (currentDew > 10.5 && (currentDew < 11.5))
; // Level to aim for dew point 11
{
digitalWrite(intakefan, LOW);
digitalWrite(humidv, LOW);
digitalWrite(dryv, LOW);
digitalWrite(exfan, LOW);
digitalWrite(exv, LOW);
digitalWrite(returnfan, LOW);
digitalWrite(returnv, LOW);
lcd.print("Dew point OK");
lcd.print(dewPoint(t, h));
lcd.clear();
lcd.print("Temperature");
lcd.setCursor(0, 1);
lcd.print(dht.readTemperature());
delay(2000);
lcd.clear();
lcd.print("Humidity");
lcd.setCursor(0, 1);
lcd.print(dht.readHumidity());
}
if (currentDew > 11.5)
;
{ // High dew point, send to dry box
digitalWrite(humidv, LOW);
digitalWrite(dryv, HIGH);
digitalWrite(exv, HIGH);
digitalWrite(returnv, HIGH);
lcd.clear();
lcd.print("after 4 days");
lcd.setCursor(0, 1);
lcd.print(dewPoint(t, h));
delay(2000);
lcd.clear();
lcd.print("Temperature");
lcd.setCursor(0, 1);
lcd.print(dht.readTemperature());
delay(2000);
lcd.clear();
lcd.print("Dry Box On");
lcd.setCursor(0, 1);
lcd.print("Fans On");
digitalWrite(intakefan, HIGH);
digitalWrite(exfan, HIGH);
digitalWrite(returnfan, HIGH);
delay(2000);
lcd.clear();
lcd.print("Humidity");
lcd.setCursor(0, 1);
lcd.print(dht.readHumidity());
}
if (currentDew < 10.5)
;
{ // Dew point low, send to humid box
digitalWrite(humidv, HIGH);
digitalWrite(dryv, LOW);
digitalWrite(exv, HIGH);
digitalWrite(returnfan, LOW);
digitalWrite(returnv, LOW);
lcd.print("Dew point LOW");
lcd.print(dewPoint(t, h));
delay(2000);
digitalWrite(intakefan, HIGH);
digitalWrite(exfan, HIGH);
lcd.clear();
lcd.print("Humid Box On");
lcd.setCursor(0, 1);
lcd.print("Fans On");
delay(2000);
lcd.clear();
lcd.print("Dew Point Low");
lcd.setCursor(0, 1);
lcd.print(dewPoint(t, h));
delay(2000);
lcd.clear();
lcd.print("Temperature");
lcd.setCursor(0, 1);
lcd.print(dht.readTemperature());
delay(2000);
lcd.clear();
lcd.print("Humidity");
lcd.setCursor(0, 1);
lcd.print(dht.readHumidity());
lcd.clear();
}
}
}