Advertisement
insippo

Greenhouse ventilation 09.06.2017 v.0.4

Jun 9th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.52 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 //12 to 24v
  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. // variables will change:
  32. int winAopenState = 0;
  33. int winAcloseState = 0;
  34. int winBopenState = 0;
  35. int winBcloseState = 0;
  36. int X = 0; //for win 1
  37. int Y = 0; //for win 2
  38.  
  39.  
  40. const int winAopenPin = 9;
  41. const int winAclosePin = 10;
  42. const int winBopenPin = 11;
  43. const int winBclosePin = 12;
  44.  
  45.  
  46.  
  47.  
  48. void setup(void)
  49. {
  50.   pinMode (win1MotorA, OUTPUT);
  51.   pinMode (win1MotorB, OUTPUT);
  52.   pinMode (win2MotorA, OUTPUT);
  53.   pinMode (win2MotorB, OUTPUT);
  54.   pinMode (inverter,   OUTPUT);
  55.   pinMode (winAopenPin, INPUT);
  56.   pinMode (winAclosePin, INPUT);
  57.   pinMode (winBopenPin, INPUT);
  58.   pinMode (winBclosePin, INPUT);
  59.   digitalWrite (win1MotorA, LOW);
  60.   digitalWrite (win1MotorB, LOW);
  61.   digitalWrite (win2MotorA, LOW);
  62.   digitalWrite (win2MotorB, LOW);
  63.   digitalWrite (inverter,   LOW);
  64.  
  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.     digitalWrite(inverter, HIGH);
  95.     digitalWrite(win1MotorA, HIGH); // motor direction if need other direction  change
  96.     digitalWrite(win1MotorB, LOW); // motor direction if need other direction  change
  97.     delay(66000); // time for motor
  98.     digitalWrite(win1MotorA, LOW);
  99.     digitalWrite(inverter, LOW);
  100.     Serial.println("Win 1 is open. ");
  101.     delay(1000);
  102.     X = 1;
  103.   }
  104.  
  105.   if (tempC <= 14 && X == 1) { // if temp <= 14  1. window closes
  106.  
  107.     X = 0;
  108.     Serial.println("Temp on +14. Closing win 1.");
  109.     digitalWrite(inverter, HIGH);
  110.     digitalWrite(win1MotorA, LOW);
  111.     digitalWrite(win1MotorB, HIGH);
  112.     delay(65000); // time for motor  full speed
  113.     digitalWrite(win1MotorB, LOW);
  114.     digitalWrite(inverter, LOW);
  115.     Serial.println("Win 1 is closed.");
  116.     delay(1000);
  117.   }
  118.  
  119.   //window 2
  120.  
  121.   if (tempC >= 20 && Y == 0) { // if temp => +20 2. window opens
  122.     Serial.println("Temp on +20. Opening win 2.");
  123.     digitalWrite(inverter, HIGH);
  124.     digitalWrite(win2MotorA, HIGH); // motor direction if need other direction  change
  125.     digitalWrite(win2MotorB, LOW); // motor direction if need other direction  change
  126.     delay(66000); // time for motor  full speed
  127.     digitalWrite(win2MotorA, LOW);
  128.     digitalWrite(inverter, LOW);
  129.     Serial.println("Win 2 is open.");
  130.     delay(1000);
  131.  
  132.     Y = 1;
  133.   }
  134.  
  135.   if (tempC <= 19 && Y == 1) { // if temp <= 21  2. window closes
  136.  
  137.     Y = 0;
  138.     Serial.print("Temp on +19. Closing win 2.");
  139.     digitalWrite(inverter, HIGH);
  140.     digitalWrite(win2MotorA, LOW); // motor direction if need other direction  change
  141.     digitalWrite(win2MotorB, HIGH); // motor direction if need other direction  change
  142.     delay(65000); // time for motor  full speed
  143.     digitalWrite(win2MotorB, LOW);
  144.     digitalWrite(inverter, LOW);
  145.     Serial.println("Window 2 closed. ");
  146.     delay(1000);
  147.   }
  148.  
  149.  
  150.  
  151.  
  152. }
  153.  
  154. void loop(void) {
  155.  
  156.  
  157. {
  158.   Serial.print("Getting temperatures...\n\r");
  159.   sensors.requestTemperatures();
  160.   delay(1000);
  161.  
  162.   Serial.print("Temperature is: ");
  163.   printTemperature(thermometer);
  164.   Serial.println("\n\r");
  165.   delay(500);
  166. }
  167.  
  168.  
  169.  
  170. {
  171. // read the state of the pushbutton value:
  172. winAopenState = digitalRead(winAopenPin);
  173.  
  174. // check if the pushbutton is pressed.
  175. // if it is, the winopen or closeState is HIGH:
  176. if (winAopenState == HIGH) {
  177.   //open win1
  178.   digitalWrite(inverter, HIGH);
  179.   digitalWrite(win1MotorA, HIGH);
  180. }else{
  181.   //do nothing
  182.   digitalWrite(inverter, LOW);
  183.   digitalWrite(win1MotorA, LOW);
  184. }
  185. winAcloseState = digitalRead(winAclosePin);
  186.  
  187. if (winAcloseState == HIGH) {
  188.   //close win1
  189.   digitalWrite(inverter, HIGH);
  190.   digitalWrite(win1MotorB, HIGH);
  191. } else {
  192.   //do nothing
  193.   digitalWrite(inverter, LOW);
  194.   digitalWrite(win1MotorB, LOW);
  195. }
  196. winBopenState = digitalRead(winBopenPin);
  197.  
  198. if (winBopenState == HIGH) {
  199.   //open win2
  200.   digitalWrite(inverter, HIGH);
  201.   digitalWrite(win2MotorA, HIGH);
  202. } else {
  203.   //do nothing
  204.   digitalWrite(inverter, LOW);
  205.   digitalWrite(win2MotorA, LOW);
  206. }
  207. winBcloseState = digitalRead(winBclosePin);
  208.  
  209. if (winBcloseState == HIGH) {
  210.   //close win2
  211.   digitalWrite(inverter, HIGH);
  212.   digitalWrite(win2MotorB, HIGH);
  213. } else {
  214.   //do nothing
  215.   digitalWrite(inverter, LOW);
  216.   digitalWrite(win2MotorB, LOW);
  217. }
  218.  
  219.  
  220. }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement