Advertisement
Guest User

Greenhouse heater

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