Advertisement
insippo

Greenhouse ventilation 09.06.2017 v.0.3

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