// request to all devices on the bus
Serial.print("Requesting temperatures... ");
float DHT22_t = dht1.readTemperature();
float DHT22_h = dht1.readHumidity();
float DHT21_t = dht2.readTemperature();
float DHT21_h = dht2.readHumidity();
//dew point mathinside DHT22
double a = 17.271;
double b = 237.7;
double h2 = (DHT22_h);
double temp1 = (a * DHT22_t);
double temp2 = ( b + DHT22_t );
double temp3 = (temp1 / temp2);
double temp5 = (h2 / 100);
double temp4 = log((double) temp5);
double temp6 = (temp4 + temp3);
double temp7 = (temp4 - temp3);
double semi1 = (b * temp6);
double semi2 = (a - temp7);
double ITd = (semi1 / semi2);
double Itf = ( DHT22_t * 1.8) + 32;
double Idf = ( ITd * 1.8) + 32;
//dew point math outside DHT21
double a1 = 17.271;
double b1 = 237.7;
double h21 = (DHT21_h);
double temp11 = (a1 * DHT21_t);
double temp21 = ( b1 + DHT21_t);
double temp31 = (temp1 / temp2);
double temp51 = (h21 / 100);
double temp41 = log((double) temp51);
double temp61 = (temp41 + temp31);
double temp71 = (temp41 - temp31);
double semi11 = (b1 * temp61);
double semi21 = (a1 - temp71);
double OTd = (semi11 / semi21);
double Otf = ( DHT21_t * 1.8) + 32;
double Odf = ( OTd * 1.8) + 32;
// converting all DHT info from float #'s to int's...
int i = DHT22_h;
int T = Itf;
int I = DHT21_h;
int t = Otf;
int D = Idf;
int d = Odf;
//good air outside, run fan as long as no interfeirence with inside dewpoint
if (t < templow && t > temphigh)
{
( tempbit = 1);
}
//bad air outside do not run fan
if (t > templow && t < temphigh)
{
(tempbit = 0);
}
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(DHT22_t) || isnan(DHT22_h))
{
Serial.println("Failed to read from DHT #1");
digitalWrite (ouTnan, HIGH);
}
else
{
Serial.println ();
Serial.println ();
Serial.print ("Humidity outside: ");
Serial.print(DHT21_h);
Serial.print(" %\t");
Serial.println ();
Serial.print ("rounded humidity outside:");
Serial.print ( I);
Serial.println ();
Serial.print ("Temperature outside: ");
Serial.print(Otf);
Serial.println("*F");
Serial.println ();
Serial.print ("rounded temp outside");
Serial.print (t);
Serial.println ();
Serial.print ( " Outside dewpointF:");
Serial.print ( Odf);
Serial.print ("*F ");
Serial.println ();
Serial.print ( "rounded Dew point outside");
Serial.print ( d);
digitalWrite (ouTnan, LOW);
}
if (isnan(DHT21_t) || isnan(DHT21_t)) {
Serial.println("Failed to read from DHT #2");
digitalWrite (iNnan, HIGH);
}
else
{
Serial.println ();
Serial.print ("Humidity inside:");
Serial.print(DHT22_h);
Serial.print(" %\t ");
Serial.println ();
Serial.print ( "rounded indoor humidity");
Serial.print ( i);
Serial.println ();
Serial.print ("Temperature inside:");
Serial.print(Itf);
Serial.println("*F");
Serial.println ();
Serial.print ("rounded temp inside:");
Serial.print (T);
Serial.println ();
Serial.print ( "Inside Dewpoint:");
Serial.print ( Idf);
Serial.print ("*F");
Serial.println ();
Serial.print ("Rounded Dew point Inside ");
Serial.print (D);
Serial.println ();
Serial.print (" temp Bit Status");
Serial.print (tempbit);
Serial.println ();
Serial.print ("temp Bit High: ");
Serial.print (temphigh);
Serial.println ();
Serial.print ("TEMP BIT LOW: ");
Serial.print (templow);
Serial.println ();
// indoor humidity is less than 40, indoor humidity is less than 70, exterior dew pointmust be larger than interior temp, outdoor temp must be above 40°
if (( i < 40 ) && (i < 70) && ( d < T ) && (tempbit == 0) && (t < 40))
{
digitalWrite ( fan, LOW);
digitalWrite (fanled, LOW);
digitalWrite (off, HIGH);
digitalWrite (toohumidout, LOW);
digitalWrite (toohumidin , LOW);
digitalWrite (toocold , HIGH);
Serial.print ("zero");
}