insippo

Greenhouse ventilation 09.06.2017 v.0.2

Jun 8th, 2017
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. // This is for arduino pro mini. if you use other arduino, check pwm pin numbers
  2. //Temperature sensor DS18B20. I put it inside greenhouse shady place.
  3. // Motor driver is LN-298N
  4.  
  5.  
  6. #include <OneWire.h>
  7. #include <DallasTemperature.h>
  8.  
  9. #define win1MotorA 4
  10. #define win1MotorB 5
  11. #define win1Speed 6 //has to be PWM pin
  12. #define win2MotorA 7
  13. #define win2MotorB 8
  14. #define win2Speed 9 //has to be PWM pin
  15. //#define win3Speed 10 //has to be PWM pin
  16. //#define win3MotorA 11
  17. //#define win3MotorB 12
  18. #define inverter 13
  19.  
  20.  
  21. // Data wire is plugged into pin 3 on the Arduino
  22. #define ONE_WIRE_BUS 3
  23.  
  24. // Setup a oneWire instance to communicate with any OneWire devices
  25. OneWire oneWire(ONE_WIRE_BUS);
  26.  
  27. // Pass our oneWire reference to Dallas Temperature.
  28. DallasTemperature sensors(&oneWire);
  29.  
  30. // Assign the addresses of your 1-Wire temp sensors.
  31. // See the tutorial on how to obtain these addresses:
  32. // http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
  33.  
  34. DeviceAddress thermometer = { 0x28, 0xC8, 0xDC, 0x51, 0x07, 0x00, 0x00, 0x72 };
  35.  
  36.  
  37.  
  38. int X = 0; //for win 1
  39. int Y = 0; //for win 2
  40. int Z = 0; // for door
  41. int i = 0; // for....dog
  42.  
  43. void setup(void)
  44. {
  45. digitalWrite (inverter, LOW);
  46. pinMode (win1MotorA, OUTPUT);
  47. pinMode (win1MotorB, OUTPUT);
  48. pinMode (win2MotorA, OUTPUT);
  49. pinMode (win2MotorB, OUTPUT);
  50. // pinMode (win3MotorA, OUTPUT);
  51. // pinMode (win3MotorB, OUTPUT);
  52. pinMode (win1Speed, OUTPUT);
  53. pinMode (win2Speed, OUTPUT);
  54. // pinMode (win3Speed, OUTPUT);
  55. pinMode (inverter, OUTPUT);
  56. digitalWrite (win1MotorA, LOW);
  57. digitalWrite (win1MotorB, LOW);
  58. digitalWrite (win2MotorA, LOW);
  59. digitalWrite (win2MotorB, LOW);
  60. // digitalWrite (win3MotorA, LOW);
  61. // digitalWrite (win3MotorB, LOW);
  62. digitalWrite (win1Speed, LOW);
  63. digitalWrite (win2Speed, LOW);
  64. // digitalWrite (win3Speed, LOW);
  65.  
  66.  
  67.  
  68. // start serial port
  69. Serial.begin(9600);
  70. // Start up the library
  71. sensors.begin();
  72. // set the resolution to 10 bit (good enough?)
  73. sensors.setResolution(thermometer, 10);
  74.  
  75. }
  76.  
  77. void printTemperature(DeviceAddress deviceAddress)
  78. {
  79. float tempC = sensors.getTempC(deviceAddress);
  80. if (tempC == -40.00) {
  81. Serial.print("Error getting temperature");
  82. } else {
  83. Serial.print("C: ");
  84. Serial.print(tempC);
  85. delay(1000);
  86. }
  87.  
  88.  
  89.  
  90. //Window 1
  91.  
  92. if (tempC >= 15 && X == 0) { // if temp => +15 1. window opens
  93. Serial.println("Temp on +15. Opening win 1.");
  94. for(i = 0; i < 256; i++)
  95. digitalWrite(inverter, HIGH);
  96. digitalWrite(win1MotorA, HIGH); // motor direction if need other direction change
  97. digitalWrite(win1MotorB, LOW); // motor direction if need other direction change
  98. analogWrite(win1Speed, i); // motor full speed
  99. delay(66000); // time for motor full speed
  100. // analogWrite(win1Speed, 200); // slower speed for opens window
  101. //delay(3000); // time for slow open
  102. analogWrite(win1Speed, 0); // motor stop
  103. digitalWrite(win1MotorA, LOW);
  104. digitalWrite(inverter, LOW);
  105. Serial.println("Win 1 is open. ");
  106. delay(1000);
  107. X = 1;
  108. }
  109.  
  110. if (tempC <= 14 && X == 1) { // if temp <= 14 1. window closes
  111.  
  112. X = 0;
  113. Serial.println("Temp on +14. Closing win 1.");
  114. digitalWrite(inverter, HIGH);
  115. digitalWrite(win1MotorA, LOW);
  116. digitalWrite(win1MotorB, HIGH);
  117. analogWrite(win1Speed, 255); // motor full speed
  118. delay(65000); // time for motor full speed
  119. 0analogWrite(win1Speed, 200); // slow speed for close
  120. delay(3000); // time for slow close
  121. analogWrite(win1Speed, 0); // motor stop
  122. digitalWrite(win1MotorB, LOW);
  123. digitalWrite(inverter, LOW);
  124. Serial.println("Win 1 is closed.");
  125. delay(1000);
  126. }
  127.  
  128. //window 2
  129.  
  130. if (tempC >= 20 && Y == 0) { // if temp => +20 2. window opens
  131. Serial.println("Temp on +20. Opening win 2.");
  132. digitalWrite(inverter, HIGH);
  133. digitalWrite(win2MotorA, HIGH); // motor direction if need other direction change
  134. digitalWrite(win2MotorB, LOW); // motor direction if need other direction change
  135. analogWrite(win2Speed, 255); // motor full speed
  136. delay(66000); // time for motor full speed
  137. //analogWrite(win2Speed, 200); // slower speed for opens window
  138. //delay(3000); // time for slow open
  139. analogWrite(win2Speed, 0); // motor stop
  140. digitalWrite(win2MotorA, LOW);
  141. digitalWrite(inverter, LOW);
  142. Serial.println("Win 2 is open.");
  143. delay(1000);
  144.  
  145. Y = 1;
  146. }
  147.  
  148. if (tempC <= 19 && Y == 1) { // if temp <= 21 2. window closes
  149.  
  150. Y = 0;
  151. Serial.print("Temp on +19. Closing win 2.");
  152. digitalWrite(inverter, HIGH);
  153. digitalWrite(win2MotorA, LOW); // motor direction if need other direction change
  154. digitalWrite(win2MotorB, HIGH); // motor direction if need other direction change
  155. analogWrite(win2Speed, 255); // motor full speed
  156. delay(65000); // time for motor full speed
  157. analogWrite(win2Speed, 200); // slow speed for close
  158. delay(3000); // time for soft close :-)
  159. analogWrite(win2Speed, 0); // motor stop
  160. digitalWrite(win2MotorB, LOW);
  161. digitalWrite(inverter, LOW);
  162. Serial.println("Window 2 closed. ");
  163. delay(1000);
  164. }
  165.  
  166. /* //window 3
  167.  
  168.  
  169. if (tempC >= 25 && Z == 0) { // if temp => +25 3. window opens
  170. digitalWrite(inverter, HIGH);
  171. digitalWrite(win3MotorA, HIGH); // motor direction if need other direction change
  172. digitalWrite(win3MotorB, LOW); // motor direction if need other direction change
  173. analogWrite(win3Speed, 255); // motor full speed
  174. delay(6000); // time for motor full speed
  175. analogWrite(win3Speed, 200); // slower speed for opens window
  176. delay(3000); // time for slow open
  177. analogWrite(win3Speed, 0); // motor stop
  178. digitalWrite(inverter, LOW);
  179. Z = 1;
  180. }
  181.  
  182. if (tempC <= 24 && Z == 1) { // if temp <= 24 3. window closes
  183.  
  184. Z = 0;
  185. digitalWrite(inverter, HIGH);
  186. digitalWrite(win1MotorA, LOW);
  187. digitalWrite(win1MotorB, HIGH);
  188. analogWrite(win1Speed, 255); // motor full speed
  189. delay(6000); // time for motor full speed
  190. analogWrite(win1Speed, 50); // slow speed for close
  191. delay(3000); // time for slow close
  192. analogWrite(win1Speed, 0); // motor stop
  193. digitalWrite(win1MotorB, LOW);
  194. digitalWrite(inverter, LOW);
  195.  
  196. }
  197.  
  198. */
  199.  
  200.  
  201.  
  202.  
  203.  
  204. }
  205.  
  206. void loop(void)
  207.  
  208.  
  209. {
  210. Serial.print("Getting temperatures...\n\r");
  211. sensors.requestTemperatures();
  212. delay(1000);
  213.  
  214. Serial.print("Temperature is: ");
  215. printTemperature(thermometer);
  216. Serial.println("\n\r");
  217. delay(500);
  218. }
Advertisement
Add Comment
Please, Sign In to add comment