Advertisement
Guest User

Arduino Greenhouse Messy

a guest
Jan 14th, 2013
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. #include <DHT22.h>
  2. // Only used for sprintf
  3. #include <stdio.h>
  4.  
  5. // Connect a 4.7K resistor between VCC and the data pin (strong pullup)
  6. #define DHT22_PIN 12 //blue/white
  7.  
  8. // Setup a DHT22 instance
  9. DHT22 myDHT22(DHT22_PIN);
  10.  
  11. //set variables
  12. //int tempF
  13. int HeaterPin = 8; // Heater Transistor is connected to Pin 2 orange/white
  14. int FanPin = 7; // Fan Transistor connected to Pin 3 green/white
  15.  
  16. float temperature;
  17. float humidity;
  18.  
  19. // For creating light averages
  20. float readingsT[10];
  21. int iT = 0;
  22. int readingsTcount = 0;
  23. float avgT;
  24. int readingsTtotalcount; //
  25.  
  26. //heating logic
  27. int heatingup = 0;
  28.  
  29. //fan logic
  30. int fanup = 0; //0=off 1 =on
  31. int timer; //counter
  32. int cycle; //0=off 1 =on
  33.  
  34. void setup(void)
  35. {
  36. // start serial port
  37. Serial.begin(9600);
  38. //Serial.println("Moms Greenhouse");
  39. }
  40.  
  41. void loop(void)
  42. {
  43.  
  44.  
  45. DHT22_ERROR_t errorCode;
  46.  
  47. // The sensor can only be read from every 1-2s, and requires a minimum
  48. // 2s warm-up after power-on.
  49. delay(2000);
  50.  
  51. //Serial.print("Requesting data...");
  52. errorCode = myDHT22.readData();
  53. switch(errorCode)
  54. {
  55. case DHT_ERROR_NONE:
  56.  
  57. ////uncomment for debug
  58. //Serial.print("Got Data ");
  59. //Serial.print(myDHT22.getTemperatureC());
  60. //Serial.print("C ");
  61. //Serial.print(myDHT22.getHumidity());
  62. //Serial.println("%");
  63. // Alternately, with integer formatting which is clumsier but more compact to store and
  64. // can be compared reliably for equality:
  65. //
  66. //char buf[128];
  67. //sprintf(buf, "Integer-only reading: Temperature %hi.%01hi C, Humidity %i.%01i %% RH",
  68. // myDHT22.getTemperatureCInt()/10, abs(myDHT22.getTemperatureCInt()%10),
  69. // myDHT22.getHumidityInt()/10, myDHT22.getHumidityInt()%10);
  70. //Serial.println(buf);
  71.  
  72.  
  73.  
  74. //Begin Greenhouse Code -----------------------------------------------------------------------
  75.  
  76. temperature = myDHT22.getTemperatureCInt()/10 + (myDHT22.getTemperatureCInt()%10 * .1);
  77. //Serial.println(avgT);
  78.  
  79. readingsT[readingsTcount] = temperature;
  80. readingsTcount++;
  81.  
  82. if (readingsTcount == 10) { readingsTcount = 0; }
  83.  
  84. if (readingsTtotalcount > 9) {
  85. avgT = (readingsT[0] + readingsT[1] + readingsT[2] + readingsT[3] + readingsT[4] + readingsT[5] + readingsT[6] + readingsT[7] + readingsT[8] + readingsT[9])/10;
  86. } else {
  87. avgT = 5555;
  88. readingsTtotalcount++;
  89. }
  90.  
  91. if (avgT < 20) { //if temperature is below 20c start the heating up cycle
  92. heatingup = 1; // Turn on Heater
  93. }
  94.  
  95. if(avgT < 24.5 && heatingup == 1) { //if temperature is below 24 and we're in a heating up cycle, keep the heater on
  96. heatingup = 1; // Turn on Heater
  97. }
  98.  
  99. if(avgT > 24.5) { //if temperature is above 24 turn off the heating up cycle
  100. heatingup = 0; // Turn off Heater
  101. }
  102.  
  103.  
  104. if (heatingup == 1) {
  105. analogWrite(HeaterPin,255); // Turn on Heater
  106. } else if (heatingup == 0) {
  107. analogWrite(HeaterPin,0); // Turn off Heater
  108. }
  109.  
  110. // fan logic ----------------------
  111. // if humidity is above 60 - fan on 30 min rest 30 min
  112. // if humidity is below 60 - pulse it 10 min rest 50 min
  113.  
  114. humidity = myDHT22.getHumidityInt()/10 + (myDHT22.getHumidityInt()%10 * .1);
  115.  
  116. timer = timer + 2; //add 2 seconds to timer
  117.  
  118. if (humidity <= 60) { //if humidity <= 60
  119. if (timer > (45*60)) { cycle = 1; } // minutes
  120. if (timer > 3600) { cycle = 0; timer = 0; }
  121. }
  122.  
  123. if (humidity > 60) { // if humidity > 60
  124. if (timer > (30*60)) { cycle = 1; } // minutes
  125. if (timer > 3600) { cycle = 0; timer = 0; }
  126. }
  127.  
  128. if (cycle == 1) { fanup = 1; } else { fanup = 0; } //if cycle is on, turn fan on, if cycle is off, turn fan off
  129.  
  130. if (fanup == 1) {
  131. analogWrite(FanPin,255); // Turn on Fan
  132. } else if (fanup == 0) {
  133. analogWrite(FanPin,0); // Turn off Fan
  134. }
  135.  
  136. // end fan logic ----------------------
  137.  
  138.  
  139.  
  140. // Serial.print(Lightreading); Serial.println(" Light");
  141. // Serial.print(temperature); Serial.println(" Current Temp");
  142. // Serial.print(avgT); Serial.println(" Average Temp");
  143. // Serial.print(readingsTcount); Serial.println(" Integer");
  144. // Serial.print(heatingup); Serial.println(" Heater Pin");
  145. // Serial.println(" ");
  146.  
  147. //End Greenhouse Code -----------------------------------------------------------------------
  148.  
  149. break;
  150. case DHT_ERROR_CHECKSUM:
  151. Serial.print("check sum error ");
  152. Serial.print(myDHT22.getTemperatureC());
  153. Serial.print("C ");
  154. Serial.print(myDHT22.getHumidity());
  155. Serial.println("%");
  156. break;
  157. case DHT_BUS_HUNG:
  158. Serial.println("BUS Hung ");
  159. break;
  160. case DHT_ERROR_NOT_PRESENT:
  161. Serial.println("Not Present ");
  162. break;
  163. case DHT_ERROR_ACK_TOO_LONG:
  164. Serial.println("ACK time out ");
  165. break;
  166. case DHT_ERROR_SYNC_TIMEOUT:
  167. Serial.println("Sync Timeout ");
  168. break;
  169. case DHT_ERROR_DATA_TIMEOUT:
  170. Serial.println("Data Timeout ");
  171. break;
  172. case DHT_ERROR_TOOQUICK:
  173. Serial.println("Polled too quick ");
  174. break;
  175. }
  176.  
  177.  
  178.  
  179.  
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement