Advertisement
Kravv

Garduino

Jul 15th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //--------------Primer Codigo-------------------
  2.  
  3. int moistureSensor = 0;
  4. int lightSensor = 1;
  5. int tempSensor = 2;
  6. int moisture_val;
  7. int light_val;
  8. int temp_val;
  9.  
  10. void setup() {
  11. Serial.begin(9600); //open serial port
  12.  
  13. }
  14.  
  15. void loop() {
  16. moisture_val = analogRead(moistureSensor); // read the value from the moisture-sensing probes
  17. Serial.print("moisture sensor reads ");
  18. Serial.println( moisture_val );
  19. delay(500);
  20. light_val = analogRead(lightSensor); // read the value from the photosensor
  21. Serial.print("light sensor reads ");
  22. Serial.println( light_val );
  23. delay(500);
  24. temp_val = analogRead(tempSensor);
  25. Serial.print("temp sensor reads ");
  26. Serial.println( temp_val );
  27. delay(1000);
  28.  
  29. }
  30.  
  31. //-------------Segundo Codigo--------------
  32.  
  33. int moistureSensor = 0;
  34. int lightSensor = 1;
  35. int tempSensor = 2;
  36. int moisture_val;
  37. int light_val;
  38. int temp_val;
  39.  
  40. void setup() {
  41. Serial.begin(9600); //open serial port
  42. pinMode (2, OUTPUT);
  43. pinMode (7, OUTPUT);
  44. pinMode (8, OUTPUT);
  45. digitalWrite (2, LOW);
  46. digitalWrite (7, LOW);
  47. digitalWrite (8, LOW);
  48. }
  49.  
  50. void loop() {
  51.  
  52. moisture_val = analogRead(moistureSensor); // read the value from the moisture sensor
  53. Serial.print("moisture sensor reads ");
  54. Serial.println( moisture_val );
  55. if (moisture_val < 850)
  56. {
  57. Serial.println("turning on pump");
  58. digitalWrite (7, HIGH);
  59. delay(2000);
  60. }
  61. if (moisture_val > 850)
  62. {
  63. Serial.println("turning off pump");
  64. digitalWrite (7, LOW);
  65. delay(2000);
  66. }
  67.  
  68. light_val = analogRead(lightSensor); // read the value from the photosensor
  69. Serial.print("light sensor reads ");
  70. Serial.println( light_val );
  71. if (light_val < 850)
  72. {
  73. Serial.println("turning on lights");
  74. digitalWrite (8, HIGH);
  75. delay(2000);
  76. }
  77. if (light_val > 850)
  78. {
  79. Serial.println("turning off lights");
  80. digitalWrite (8, LOW);
  81. delay(2000);
  82. }
  83.  
  84. temp_val = analogRead(tempSensor);
  85. Serial.print("temp sensor reads ");
  86. Serial.println( temp_val );
  87. if (temp_val < 920)
  88. {
  89. Serial.println("turning on low-temperature LED");
  90. digitalWrite (2, HIGH);
  91. delay(2000);
  92. }
  93. if (temp_val > 920)
  94. {
  95. Serial.println("turning off low-temperature LED");
  96. digitalWrite (2, LOW);
  97. delay(2000);
  98. }
  99. }
  100.  
  101. //-----------------Codigo Final------------------
  102.  
  103. //include the datetime library, so our garduino can keep track of how long the lights are on
  104. #include <DateTime.h>
  105.  
  106. //define analog inputs to which we have connected our sensors
  107. int moistureSensor = 0;
  108. int lightSensor = 1;
  109. int tempSensor = 2;
  110.  
  111. //define digital outputs to which we have connecte our relays (water and light) and LED (temperature)
  112. int waterPump = 7;
  113. int lightSwitch = 8;
  114. int tempLed = 2;
  115.  
  116. //define variables to store moisture, light, and temperature values
  117. int moisture_val;
  118. int light_val;
  119. int temp_val;
  120.  
  121. //decide how many hours of light your plants should get daily
  122. float hours_light_daily_desired = 14;
  123.  
  124. //calculate desired hours of light total and supplemental daily based on above values
  125. float proportion_to_light = hours_light_daily_desired / 24;
  126. float seconds_light = 0;
  127. float proportion_lit;
  128.  
  129. //setup a variable to store seconds since arduino switched on
  130. float start_time;
  131. float seconds_elapsed;
  132. float seconds_elapsed_total;
  133. float seconds_for_this_cycle;
  134.  
  135. void setup() {
  136. //open serial port
  137. Serial.begin(9600);
  138. //set the water, light, and temperature pins as outputs that are turned off
  139. pinMode (waterPump, OUTPUT);
  140. pinMode (lightSwitch, OUTPUT);
  141. pinMode (tempLed, OUTPUT);
  142. digitalWrite (waterPump, LOW);
  143. digitalWrite (lightSwitch, LOW);
  144. digitalWrite (tempLed, LOW);
  145.  
  146. //establish start time
  147. start_time = DateTime.now();
  148. seconds_elapsed_total = 0;
  149.  
  150. }
  151. void loop() {
  152. // read the value from the moisture-sensing probes, print it to screen, and wait a second
  153. moisture_val = analogRead(moistureSensor);
  154. Serial.print("moisture sensor reads ");
  155. Serial.println( moisture_val );
  156. delay(1000);
  157. // read the value from the photosensor, print it to screen, and wait a second
  158. light_val = analogRead(lightSensor);
  159. Serial.print("light sensor reads ");
  160. Serial.println( light_val );
  161. delay(1000);
  162. // read the value from the temperature sensor, print it to screen, and wait a second
  163. temp_val = analogRead(tempSensor);
  164. Serial.print("temp sensor reads ");
  165. Serial.println( temp_val );
  166. delay(1000);
  167. Serial.print("seconds total = ");
  168. Serial.println( seconds_elapsed_total );
  169. delay(1000);
  170. Serial.print("seconds lit = ");
  171. Serial.println( seconds_light);
  172. delay(1000);
  173. Serial.print("proportion desired = ");
  174. Serial.println( proportion_to_light);
  175. delay(1000);
  176. Serial.print("proportion achieved = ");
  177. Serial.println( proportion_lit);
  178. delay(1000);
  179.  
  180. //turn water on when soil is dry, and delay until soil is wet
  181. if (moisture_val < 850)
  182. {
  183. digitalWrite(waterPump, HIGH);
  184. }
  185.  
  186. while (moisture_val < 850)
  187. {
  188. delay(10000);
  189. //thanks to JoshTW for the following, important correction
  190. moisture_val = analogRead(moistureSensor);
  191. }
  192.  
  193. digitalWrite(waterPump, LOW);
  194.  
  195. //update time, and increment seconds_light if the lights are on
  196. seconds_for_this_cycle = DateTime.now() - seconds_elapsed_total;
  197. seconds_elapsed_total = DateTime.now() - start_time;
  198. if (light_val > 900)
  199. {
  200. seconds_light = seconds_light + seconds_for_this_cycle;
  201. }
  202.  
  203. //cloudy days that get sunny again: turn lights back off if light_val exceeds 900. this works b/c the supplemental lights aren't as bright as the sun:)
  204. if (light_val > 900)
  205. {
  206. digitalWrite (lightSwitch, LOW);
  207. }
  208.  
  209. //turn off lights if proportion_lit>proportion_to_light, and then wait 5 minutes
  210. if (proportion_lit > proportion_to_light)
  211. {
  212. digitalWrite (lightSwitch, LOW);
  213. delay (300000);
  214. }
  215.  
  216. //figure out what proportion of time lights have been on
  217. proportion_lit = seconds_light/seconds_elapsed_total;
  218.  
  219. //turn lights on if light_val is less than 900 and plants have light for less than desired proportion of time, then wait 10 seconds
  220. if (light_val < 900 and proportion_lit < proportion_to_light)
  221. {
  222. digitalWrite(lightSwitch, HIGH);
  223. delay(10000);
  224. }
  225.  
  226. //turn on temp alarm light if temp_val is less than 850 (approximately 50 degrees Fahrenheit)
  227. if (temp_val < 850)
  228. {
  229. digitalWrite(tempLed, HIGH);
  230. }
  231.  
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement