1. #include <PrintPJRClcd.h>
  2.  
  3. #include <OneWire.h>
  4. #include <DallasTemperature.h>
  5.  
  6. // Data wire is plugged into port 2 on the Arduino
  7. #define ONE_WIRE_BUS 2
  8. #define RH_SENSOR_PIN 0
  9. #define DEHUMIDIFIER_PIN 3
  10. #define EXHAUST_PIN 4
  11. #define RH_SWING 10
  12. #define TEMP_SWING 6
  13. #define MINIMUM_ON_TIME 10 //minumum amount of time the vent can be on, in minutes
  14. int MaxRoomTemp = 90;
  15. int MaxRoomRH = 60;
  16.  
  17. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  18. OneWire oneWire(ONE_WIRE_BUS);
  19.  
  20. // Pass our oneWire reference to Dallas Temperature.
  21. DallasTemperature sensors(&oneWire);
  22.  
  23. PrintPJRClcd LCD = PrintPJRClcd();
  24.  
  25. float IndoorTemperature = 0.0;
  26. int IndoorRH = 0;
  27. int VentActivated = 0;
  28. unsigned long TurnOffOutletIn = 0;
  29. unsigned long tempMillis = 0;
  30. boolean HeatTriggered = false;
  31. boolean RHTriggered = false;
  32.  
  33.  
  34. void setup(void)
  35. {
  36. // start serial port
  37. Serial.begin(19200);
  38. // Start up the OneWire library
  39. sensors.begin();
  40.  
  41. LCD.RedrawScreen(); //blank screen
  42.  
  43. /* Turn on output pins for dehumidifier and exhaust fan */
  44. pinMode( DEHUMIDIFIER_PIN, OUTPUT );
  45. pinMode( EXHAUST_PIN, OUTPUT );
  46. digitalWrite( DEHUMIDIFIER_PIN, LOW);
  47. digitalWrite( EXHAUST_PIN, LOW);
  48.  
  49.  
  50. }
  51.  
  52. void loop(void)
  53. {
  54. // call sensors.requestTemperatures() to issue a global temperature request
  55. sensors.requestTemperatures(); // Send the command to get temperatures
  56. IndoorTemperature = sensors.getTempFByIndex(0); //assign first temp
  57. IndoorRH = map(analogRead(RH_SENSOR_PIN), 196, 822, 0, 100);
  58.  
  59. if (IndoorRH > MaxRoomRH) { // Check RH
  60. activateVent();
  61. }
  62. if (IndoorTemperature > MaxRoomTemp) { //check temp
  63. activateVent();
  64. }
  65.  
  66. tempMillis = millis();
  67. /* if (vent is on) && (Current temp is less than max temp - swing) && (enough time has passed) */
  68. if ( (HeatTriggered) && ( VentActivated == 1 ) && ( int(IndoorTemperature) <= (MaxRoomTemp - TEMP_SWING ) ) && ( tempMillis > TurnOffOutletIn ) ) { deactivateVent(); };
  69. if ( (RHTriggered) && ( VentActivated == 1 ) && ( int(IndoorRH) <= (MaxRoomRH - RH_SWING ) ) && ( tempMillis > TurnOffOutletIn ) ) { deactivateVent(); };
  70.  
  71. LCD.PrintData(0,0,IndoorTemperature);
  72. LCD.PrintData(1,IndoorRH,0);
  73. LCD.PrintData(2,MaxRoomRH,MaxRoomTemp);
  74. LCD.PrintData(3,int(VentActivated),0);
  75.  
  76.  
  77.  
  78. }
  79.  
  80. void deactivateVent(void){
  81. VentActivated = 0; //turn off flag
  82. digitalWrite(EXHAUST_PIN, LOW);
  83. }
  84. void activateVent(void) {
  85. VentActivated = 1; //turn on flag
  86. digitalWrite(EXHAUST_PIN, HIGH);
  87. TurnOffOutletIn = millis() + (MINIMUM_ON_TIME * 60000);
  88. }
  89.  
  90.  
  91.  
  92. /* Analog input, HIH-4030, an RH sensor with a linear output
  93. *
  94. * Zero offset +0.958 V, Analog reading
  95. * .958 volts == 0% humidity == 196
  96. * 3.268 volts == 75.3% RH == 669
  97. * 4.018 volts == 100% RH == 822
  98. * slope 30.680 mV/% RH , 3060+958
  99. * RH = (Vout - 0.958)/0.0307
  100. * Accuracy +/- 3.5%
  101. * +/- 0.5% Repeatability
  102. *
  103. * USE ONLY IN NON-CONDENSING ENVIRONMENTS
  104. * When liquid water falls on the humidity sensor die,
  105. * output goes to a low rail condition indicating no humidity.
  106. *
  107. * map(analogRead(RH_SENSOR_PIN), 196, 822, 0, 100) means:
  108. * (RawValueFromPin,ZeroReading,100%Reading,0,100)
  109. * It converts the reading on the pin to a percentage.
  110. *
  111. ********/
  112.  
  113.  
  114.