Advertisement
auntie

Arduino Water and Light Control for Plants

Mar 5th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. #include <AccelStepper.h>
  2.  
  3. // Define a stepper for lamp motor
  4. // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5; we just used 3 and 4 without extra declaration
  5. AccelStepper stepper;
  6.  
  7. // lamp motor variables
  8. int lightsensor = A0; //grünes Kabel
  9.  
  10. int lightvalue = 0;
  11. int lightmin = 600;
  12. int lightmax = 935;
  13.  
  14. int lightPos = 0;
  15. int maxPos = 30000;
  16. int minPos = 0;
  17.  
  18. int lightTimeRead = 0;
  19.  
  20. // in7, in8 Steuerung, in6 pwm
  21. // water pump variables
  22. int watersensor = A5; //blaues Kabel
  23. int in7Pump = 7;
  24. int in8Pump = 8;
  25. int pwmPump = 6;
  26.  
  27. //important Parameters for pump
  28. int waterSensorPower = 10; //
  29.  
  30. int dryness = 0;
  31. int drysoil = 500;
  32. int wetsoil = 30;
  33.  
  34. //pumplogic/timer variables
  35. boolean pumpActive = false;
  36. int actualTime = 0;
  37. int pumpTime = 0;
  38. long lastTime = 0;
  39. int pumpTimeRead = 0;
  40. int lastHumidityRead = 0;
  41.  
  42. void setup()
  43. {
  44. // setup for stepper motor
  45. stepper.setMaxSpeed(2000);
  46. stepper.setAcceleration(60);
  47.  
  48. // setup for water pump
  49. pinMode(in8Pump, OUTPUT);
  50. pinMode(in7Pump, OUTPUT);
  51. pinMode(pwmPump, OUTPUT);
  52. pinMode(waterSensorPower, OUTPUT);
  53.  
  54. digitalWrite(in8Pump, HIGH);
  55. digitalWrite(in7Pump,LOW);
  56. digitalWrite(waterSensorPower, LOW);
  57.  
  58. // for reading the actual sensor values
  59. Serial.begin(9200);
  60. }
  61.  
  62. // moves light up
  63. void lightUp() {
  64. stepper.move(500);
  65. stepper.run();
  66. if(lightTimeRead < actualTime) {
  67. lightTimeRead = actualTime;
  68. lightPos += 500;
  69. }
  70. }
  71.  
  72. // moves light down
  73. void lightDown() {
  74. stepper.move(-500);
  75. stepper.run();
  76. if(lightTimeRead < actualTime) {
  77. lightTimeRead = actualTime;
  78. lightPos -= 500;
  79. }
  80. }
  81.  
  82. // timer for light
  83. void lightTimeCounter(int currentTime) {
  84. int delta = currentTime - lastTime;
  85.  
  86. if(delta > 1000) {
  87. lastTime = currentTime;
  88. actualTime++;
  89. Serial.println(lightvalue);
  90. }
  91. }
  92.  
  93. // activates pump
  94. void startPumping() {
  95. if(!pumpActive) {
  96. pumpTime = 5;
  97. pumpActive = true;
  98. analogWrite(pwmPump,255);
  99. }
  100. }
  101.  
  102. // stops pump
  103. void stopPumping() {
  104. if(pumpActive && pumpTime <= 0) {
  105. analogWrite(pwmPump,0);
  106. }
  107. }
  108.  
  109. // before that pump is locked and humidity is not measured
  110. void unlockPump() {
  111. if(pumpActive && pumpTime < -60)
  112. pumpActive = false;
  113. }
  114.  
  115. // timer for pump
  116. void pumpTimeCounter() {
  117. if(pumpTimeRead < actualTime && pumpActive) {
  118. pumpTimeRead = actualTime;
  119. pumpTime--;
  120. }
  121. }
  122.  
  123. // catch possible error because of an time overflow
  124. void catchOverflow(int currentTime) {
  125. int delta = currentTime - lastTime;
  126. if(delta < 0) {
  127. lastTime = currentTime;
  128. }
  129. }
  130.  
  131. void loop()
  132.  
  133. {
  134. lightvalue = analogRead(lightsensor);
  135.  
  136. //moves the light up if to dark
  137. if(lightvalue < lightmin) {
  138. lightDown();
  139. }
  140.  
  141. // moves the light down if to bright
  142. if (lightvalue > lightmax) {
  143. lightUp();
  144. }
  145.  
  146. // light stops moving if light value is ok
  147. if(lightvalue > lightmin && lightvalue < lightmax){
  148. stepper.move(0);
  149. stepper.run();
  150. }
  151.  
  152. // sets timelimit for light measurement
  153. lightTimeCounter(millis());
  154.  
  155. // activates pump if soil is too dry
  156. int deltaHum = actualTime - lastHumidityRead;
  157. if(deltaHum > 10) {
  158. lastHumidityRead = actualTime;
  159. digitalWrite(waterSensorPower, HIGH);
  160. dryness = analogRead(watersensor);
  161.  
  162. if(dryness > drysoil && dryness < 900) {
  163. startPumping();
  164. }
  165. // stops measurement
  166. digitalWrite(waterSensorPower, LOW);
  167. }
  168.  
  169. // blocks pump for a while until soil has soaked water
  170. pumpTimeCounter();
  171.  
  172. // stops the pump
  173. stopPumping();
  174.  
  175. //humidity can be measured again
  176. unlockPump();
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement