Advertisement
insippo

Greenhouse ventilation with buttons 09.06.2017 v.0.1

Jun 9th, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.37 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 7
  12. #define win2MotorB 8
  13. #define inverter 13
  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 win1openPin A0
  38. const int win1closePin A1
  39. const int win2openPin A2
  40. const int win2closePin A3
  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. digitalWrite (win1MotorA, LOW);
  57. digitalWrite (win1MotorB, LOW);
  58. digitalWrite (win2MotorA, LOW);
  59. digitalWrite (win2MotorB, LOW);
  60. digitalWrite (inverter, LOW);
  61. pinMode (win1openPin, INPUT);
  62. pinMode (win1closePin, INPUT);
  63. pinMode (win2openPin, INPUT);
  64. pinMode (win1closePin, INPUT);
  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, 0); // motor stop
  101. digitalWrite(win1MotorA, LOW);
  102. digitalWrite(inverter, LOW);
  103. Serial.println("Win 1 is open. ");
  104. delay(1000);
  105. X = 1;
  106. }
  107.  
  108. if (tempC <= 14 && X == 1) { // if temp <= 14 1. window closes
  109.  
  110. X = 0;
  111. Serial.println("Temp on +14. Closing win 1.");
  112. digitalWrite(inverter, HIGH);
  113. digitalWrite(win1MotorA, LOW);
  114. digitalWrite(win1MotorB, HIGH);
  115. analogWrite(win1Speed, 255); // motor full speed
  116. delay(65000); // time for motor full speed
  117. 0analogWrite(win1Speed, 200); // slow speed for close
  118. delay(3000); // time for slow close
  119. analogWrite(win1Speed, 0); // motor stop
  120. digitalWrite(win1MotorB, LOW);
  121. digitalWrite(inverter, LOW);
  122. Serial.println("Win 1 is closed.");
  123. delay(1000);
  124. }
  125.  
  126. //window 2
  127.  
  128. if (tempC >= 20 && Y == 0) { // if temp => +20 2. window opens
  129. Serial.println("Temp on +20. Opening win 2.");
  130. digitalWrite(inverter, HIGH);
  131. digitalWrite(win2MotorA, HIGH); // motor direction if need other direction change
  132. digitalWrite(win2MotorB, LOW); // motor direction if need other direction change
  133. analogWrite(win2Speed, 255); // motor full speed
  134. delay(66000); // time for motor full speed
  135. //analogWrite(win2Speed, 200); // slower speed for opens window
  136. //delay(3000); // time for slow open
  137. analogWrite(win2Speed, 0); // motor stop
  138. digitalWrite(win2MotorA, LOW);
  139. digitalWrite(inverter, LOW);
  140. Serial.println("Win 2 is open.");
  141. delay(1000);
  142.  
  143. Y = 1;
  144. }
  145.  
  146. if (tempC <= 19 && Y == 1) { // if temp <= 21 2. window closes
  147.  
  148. Y = 0;
  149. Serial.print("Temp on +19. Closing win 2.");
  150. digitalWrite(inverter, HIGH);
  151. digitalWrite(win2MotorA, LOW); // motor direction if need other direction change
  152. digitalWrite(win2MotorB, HIGH); // motor direction if need other direction change
  153. analogWrite(win2Speed, 255); // motor full speed
  154. delay(65000); // time for motor full speed
  155. analogWrite(win2Speed, 200); // slow speed for close
  156. delay(3000); // time for soft close :-)
  157. analogWrite(win2Speed, 0); // motor stop
  158. digitalWrite(win2MotorB, LOW);
  159. digitalWrite(inverter, LOW);
  160. Serial.println("Window 2 closed. ");
  161. delay(1000);
  162. }
  163.  
  164.  
  165.  
  166.  
  167. }
  168.  
  169. void loop(void)
  170.  
  171.  
  172. {
  173. Serial.print("Getting temperatures...\n\r");
  174. sensors.requestTemperatures();
  175. delay(1000);
  176.  
  177. Serial.print("Temperature is: ");
  178. printTemperature(thermometer);
  179. Serial.println("\n\r");
  180. delay(500);
  181. }
  182.  
  183. {
  184. // read the state of the pushbutton value:
  185. win1openState = digitalRead(win1openPin);
  186. win1closeState = digitalRead(win1closePin);
  187. win2openState = digitalRead(win2openPin);
  188. win2closeState = digitalRead(win2closePin);
  189.  
  190. // check if the pushbutton is pressed.
  191. // if it is, the winopen or closeState is HIGH:
  192. if (win1openState == HIGH) {
  193. //open win1
  194. digitalWrite(inverter, HIGH);
  195. digitalWrite(win1MotorA, HIGH);
  196. } else {
  197. //do nothing
  198. digitalWrite(inverter, LOW);
  199. digitalWrite(win1MotorA, LOW);
  200. }
  201.  
  202. if (win1closeState == HIGH) {
  203. //close win1
  204. digitalWrite(inverter, HIGH);
  205. digitalWrite(win1MotorB, HIGH);
  206. } else {
  207. //do nothing
  208. digitalWrite(inverter, LOW);
  209. digitalWrite(win1MotorB, LOW);
  210. }
  211.  
  212. if (win2openState == HIGH) {
  213. //open win2
  214. digitalWrite(inverter, HIGH);
  215. digitalWrite(win2MotorA, HIGH);
  216. } else {
  217. //do nothing
  218. digitalWrite(inverter, LOW);
  219. digitalWrite(win2MotorA, LOW);
  220. }
  221.  
  222. if (win1closeState == HIGH) {
  223. //close win2
  224. digitalWrite(inverter, HIGH);
  225. digitalWrite(win2MotorB, HIGH);
  226. } else {
  227. //do nothing
  228. digitalWrite(inverter, LOW);
  229. digitalWrite(win2MotorB, LOW);
  230. }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement