 	int hIn = dhtIn.readHumidity();
 	int hOut = dhtOut.readHumidity();
 
+	// check for reading errors
 	if (isnan(tIn) || isnan(tOut) || isnan(hIn) || isnan(hOut)) {
 		Serial.println("Error while reading sensors");
 		return;

// inside sensor
DHT dhtIn(SENSOR_INSIDE_PIN, SENSOR_TYPE);
// outside
DHT dhtOut(SENSOR_INSIDE_PIN, SENSOR_TYPE);

void setup() {
	Serial.begin(9600);
	dhtIn.begin();
	dhtOut.begin();
	pinMode(FAN_PIN, OUTPUT);

	
	Serial.println("Humidity based fan control");
	Serial.println("Inside | Outside | State");
}

	float humidIn = absoluteHumidity(hIn, tIn);
	float humidOut = absoluteHumidity(hOut, tOut);

	// simple state machine to provide easy extension and modification
	switch (state) {
		case STATE_IDLE:
			if (humidIn > humidOut && tOut > MIN_TEMP) {
				state = STATE_ACTIVE;
			}
			break;
		case STATE_ACTIVE:
			if (humidIn <= humidOut || tOut <= MIN_TEMP) {
				state = STATE_IDLE;
			}
			break;
	}

	// Relay module works inversed
	digitalWrite(FAN_PIN, !state);

	/********************************
	 * Display output               *
	 ********************************/
	lcd.clear();

	// inside humidity
	lcd.setCursor(0, 0);
	lcd.print("in: ");
	// correct number alignment
	if (humidIn < 10) {
		lcd.print(" ");
	}
	lcd.print(humidIn);
	
	// outside humidity
	lcd.setCursor(0, 1);
	lcd.print("out:");
	// correct number alignment
	if (humidOut < 10) {
		lcd.print(" ");
	}
	lcd.print(humidOut);

	// active state
	if (state == STATE_ACTIVE) {
		lcd.setCursor(10, 0);
		lcd.print("active");
	}
	// outside lower than min temperature
	if (tOut < MIN_TEMP) {
		lcd.setCursor(12, 1);
		lcd.print("cold");
	}

	// serial data output
	Serial.print(humidIn);
	Serial.print(" | ");
	Serial.print(humidOut);
	Serial.print(" | ");
	Serial.println(state);

	// sensors can only be read every 2 seconds
	delay(SENSOR_TIMEOUT);
}

