#include <PrintPJRClcd.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define RH_SENSOR_PIN 0
#define DEHUMIDIFIER_PIN 3
#define EXHAUST_PIN 4
#define RH_SWING 10
#define TEMP_SWING 6
#define MINIMUM_ON_TIME 10 //minumum amount of time the vent can be on, in minutes
int MaxRoomTemp = 90;
int MaxRoomRH = 60;
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
PrintPJRClcd LCD = PrintPJRClcd();
float IndoorTemperature = 0.0;
int IndoorRH = 0;
int VentActivated = 0;
unsigned long TurnOffOutletIn = 0;
unsigned long tempMillis = 0;
boolean HeatTriggered = false;
boolean RHTriggered = false;
void setup(void)
{
// start serial port
Serial.begin(19200);
// Start up the OneWire library
sensors.begin();
LCD.RedrawScreen(); //blank screen
/* Turn on output pins for dehumidifier and exhaust fan */
pinMode( DEHUMIDIFIER_PIN, OUTPUT );
pinMode( EXHAUST_PIN, OUTPUT );
digitalWrite( DEHUMIDIFIER_PIN, LOW);
digitalWrite( EXHAUST_PIN, LOW);
}
void loop(void)
{
// call sensors.requestTemperatures() to issue a global temperature request
sensors.requestTemperatures(); // Send the command to get temperatures
IndoorTemperature = sensors.getTempFByIndex(0); //assign first temp
IndoorRH = map(analogRead(RH_SENSOR_PIN), 196, 822, 0, 100);
if (IndoorRH > MaxRoomRH) { // Check RH
activateVent();
}
if (IndoorTemperature > MaxRoomTemp) { //check temp
activateVent();
}
tempMillis = millis();
/* if (vent is on) && (Current temp is less than max temp - swing) && (enough time has passed) */
if ( (HeatTriggered) && ( VentActivated == 1 ) && ( int(IndoorTemperature) <= (MaxRoomTemp - TEMP_SWING ) ) && ( tempMillis > TurnOffOutletIn ) ) { deactivateVent(); };
if ( (RHTriggered) && ( VentActivated == 1 ) && ( int(IndoorRH) <= (MaxRoomRH - RH_SWING ) ) && ( tempMillis > TurnOffOutletIn ) ) { deactivateVent(); };
LCD.PrintData(0,0,IndoorTemperature);
LCD.PrintData(1,IndoorRH,0);
LCD.PrintData(2,MaxRoomRH,MaxRoomTemp);
LCD.PrintData(3,int(VentActivated),0);
}
void deactivateVent(void){
VentActivated = 0; //turn off flag
digitalWrite(EXHAUST_PIN, LOW);
}
void activateVent(void) {
VentActivated = 1; //turn on flag
digitalWrite(EXHAUST_PIN, HIGH);
TurnOffOutletIn = millis() + (MINIMUM_ON_TIME * 60000);
}
/* Analog input, HIH-4030, an RH sensor with a linear output
*
* Zero offset +0.958 V, Analog reading
* .958 volts == 0% humidity == 196
* 3.268 volts == 75.3% RH == 669
* 4.018 volts == 100% RH == 822
* slope 30.680 mV/% RH , 3060+958
* RH = (Vout - 0.958)/0.0307
* Accuracy +/- 3.5%
* +/- 0.5% Repeatability
*
* USE ONLY IN NON-CONDENSING ENVIRONMENTS
* When liquid water falls on the humidity sensor die,
* output goes to a low rail condition indicating no humidity.
*
* map(analogRead(RH_SENSOR_PIN), 196, 822, 0, 100) means:
* (RawValueFromPin,ZeroReading,100%Reading,0,100)
* It converts the reading on the pin to a percentage.
*
********/