Hello,
I am currently working on a project to automate heating and ventilation control of a small greenhouse. One issue I am having is working through the control logic for night vs. day. For the same project, some folks use a photocell sensor to judge night vs day, but this seems that it can be a bit troublesome.
My thought was to use the "hour" value from the RTC to define night and day for my code logic would be pretty simple. I am not worried about setting this as a constant even thought length of day changes throughout the year, don't need that much accuracy in the time i will actually be growing.
So how can I use the RTC to set a variable for hours (0-24) to be able to do this?
Here is the code I have been working on, so far it only has very simple logic for fan and vent control but still working on adding more:
// include the library code:
#include <LiquidCrystal.h> //library for LCD
#include <DHT.h> //Library for temp sensors
#include <RunningAverage.h> //Library for calculating temp average vs instantaneous
//#include <Arduino.h> //
#include <Wire.h> //
#include <DS3231.h> //Needed for real time clock
// define inputs and outputs
#define DHTPIN 9 //Internal temp DHT22 sensor
#define DHTPIN2 2 //External temp DHT22 sensor
#define DHTTYPE DHT22 //sets to use correct DHT library
#define DHTTYPE2 DHT22 //sets to use correct DHT library
//set variables
DHT dht (DHTPIN, DHTTYPE); //sets internal temp/humidity as dht
DHT dhttwo (DHTPIN2, DHTTYPE2); //sets extermal temp/humidity as dhttwo
//code to set up RTC
DS3231 clock; //sets up clock
RTCDateTime dt; //sets dt as time variable
int Hour;
//code to setup running average calculation
RunningAverage internalHumidity (5); //internal hum average
RunningAverage internalTemp(5); //internal temp average
RunningAverage externalHumidity(5); //external hum average
RunningAverage externalTemp(5); //external temp average
int itempRaw = 0; //sets internal temp Raw
int ihumRaw = 0; //sets internal humidityRaw
int etempRaw = 0; //sets external temp Raw
int ehumRaw = 0; //sets external humidity Raw
int tempI = 0; //variable for averaged internal temp
int tempE = 0; //variable for averaged external temp
int humidityI = 0; //variable for averaged internal humidity
int humidityE = 0; //variable for averaged external humidity
const byte nsum = 5; //
//set variable type
int chk; //
//greenhouse setpoints
int heatTempV = 70; //setpoint temp in F when vent would open
int heatTempF = 71; //setpoint temp in F when fan turns on
int humV = 40; //setpoint humidity in % when vent opens
int humF = 60; //setpoint humidity in % when fan kicks on
//digital outputs
int fan = 0; //this variable is for displaying via LCD or serial fan status
int vent = 0; //this variable is for displaying via LCD or serial fan status
const int rs = 3, en = 4, d4 = 5, d5 = 6, d6 = 7, d7 = 8; //set LCD pins
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); //start LCD
void setup() {
//setting up outputs
pinMode (43, OUTPUT); //output trigger for vent
pinMode (41, OUTPUT); //output trigger for fan
lcd.begin(20, 4); //starts LCD
Serial.begin(9600); //opens serial port
dht.begin (); //starts int temp sensor
dhttwo.begin (); //starts ext temp sensor
clock.begin (); //starts rtc
//clock.setDateTime (__DATE__, __TIME__); //sets clock from computer, only need to do this once unless battery dies on RTC or time is wrong
}
void loop() {
delay(500);
//Setting up the values to be read
ihumRaw = dht.readHumidity(); //reads internal humidity
itempRaw = dht.readTemperature (true); //sets up to read int temp in F, for C remove "true"
ehumRaw = dhttwo.readHumidity (); //sets up to read ext humidity
etempRaw = dhttwo.readTemperature (true); //sets up to read ext temp in F, for C remove "true"
//setting how large the variables can be
ihumRaw = constrain (ihumRaw, 0, 100); //limits value to 0-100
ehumRaw = constrain (ehumRaw,0,100); //limits value to 0-100
itempRaw = constrain (itempRaw,-50,100); //limits value to -50 to 100
etempRaw = constrain (etempRaw,-50,100); //limits value to -50 to 100
//assinging the actual values to the variables
if (ehumRaw > 1)
externalHumidity.addValue(ehumRaw);
if (etempRaw > 1)
externalTemp.addValue(etempRaw);
if (ihumRaw > 1)
internalHumidity.addValue(ihumRaw);
if (itempRaw > 1)
internalTemp.addValue(itempRaw);
//calculating the average and setting it to variables to be used in further logic and displayed
humidityI = internalHumidity.getAverage();
humidityE = externalHumidity.getAverage();
tempI = internalTemp.getAverage();
tempE = externalTemp.getAverage();
//Calculate heat index
float hif = dht.computeHeatIndex (tempE, humidityE);
//setting up clock to get time
dt = clock.getDateTime (); //sets dt to get the clock time
/*______________________________________________Serial debugging_________________________________________________________
//Serial print code for debugging
Serial.println(clock.dateFormat("M jS y, h:ia", dt));
Serial.print("Raw Int Humidity: ");
Serial.print(ihumRaw);
Serial.print(" %, Raw Int Temp: ");
Serial.print (itempRaw);
Serial.println(" Farenheit");
Serial.print("Avg Int Humidity: ");
Serial.print (humidityI);
Serial.print(" %, Avg Int Temp: ");
Serial.print (tempI);
Serial.println(" Farenheit");
Serial.print("Raw Ext Humidity: ");
Serial.print(ehumRaw);
Serial.print(" %, Raw Ext Temp: ");
Serial.print (etempRaw);
Serial.println(" Farenheit");
Serial.print("Avg Ext Humidity: ");
Serial.print (humidityE);
Serial.print(" %, Avg Ext Temp: ");
Serial.print (tempE);
Serial.print(" Farenheit");
Serial.println(" ");
Serial.println(" ");
*/
//_________________________________________________LCD Display Code_________________________________________
lcd.clear ();
lcd.setCursor(0,0);
lcd.print("IT:");
lcd.print(tempI);
lcd.print(" ");
lcd.print((char)223);
lcd.print("F |");
lcd.print(clock.dateFormat(" h:ia", dt));
lcd.setCursor(0,1);
lcd.print("IH:");
lcd.print(humidityI);
lcd.print(" % | ");
lcd.print("VENT:");
if (vent == 1)
{
lcd.print("OPN");
}
else if (vent == 0)
{
lcd.print("CSD");
}
lcd.setCursor(0,2);
lcd.print("ET:");
lcd.print(tempE);
lcd.print(" ");
lcd.print((char)223);
lcd.print("F | ");
lcd.print("FAN :");
if (fan == 1)
{
lcd.print("ON");
}
else if (fan == 0)
{
lcd.print("OFF");
}
lcd.setCursor(0,3);
lcd.print("EH:");
lcd.print(humidityE);
lcd.print(" % |");
lcd.print(" WATR:OFF");
delay (1000);
//_______________________________________Logic Control___________________________________________________
//vent logic
if (humidityE > humV + 1)
{
digitalWrite (43, HIGH);
vent = 1;
}
else if (humidityE < humV + 1)
{
digitalWrite (43, LOW);
vent = 0;
}
//fan logic
if (humidityE > humF + 1)
{
digitalWrite (41, HIGH);
fan = 1;
}
else if (humidityE < humF + 1)
{
digitalWrite (41, LOW);
fan = 0;
}
}