Advertisement
seston

kastmine1

Dec 20th, 2015
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. // Code for the Plant Watering project found here: https://www.manylabs.org/docs/project/plantWatering/
  2. // Copyright ManyLabs 2013
  3. // MIT license
  4.  
  5. // devices on analog pins
  6. #define MOISTURE_PIN 0
  7. #define WATER_PIN 2
  8.  
  9. // devices on digital pins
  10. #define BUTTON_PIN 2
  11. #define RELAY_PIN 4
  12. #define BUZZER_PIN 6
  13.  
  14. // turn on pump if moisture sensor value is below this number (i.e. the soil is too dry)
  15. #define MOISTURE_THRESHOLD 100
  16.  
  17. // turn on buzzer if water sensor value is below this number (i.e. the water level is too low)
  18. #define WATER_THRESHOLD 100
  19.  
  20. // this sets the number of seconds between reads of the moisture/water sensors
  21. // longer delays will help prevent corrosion, but will cause the pump to run for longer
  22. // check the project documentation for more information
  23. #define SENSOR_UPDATE_DELAY_SECONDS 10
  24. // this counter is used so that we reduce the frequency of reading the moisture/water
  25. // sensors (in order to reduce corrosion)
  26. int sensorCounter = 0;
  27.  
  28. // most recent sensor values
  29. int moisture = 0;
  30. int water = 0;
  31.  
  32. // run once on startup
  33. void setup() {
  34.  
  35. // set up input/output pins
  36. pinMode( BUTTON_PIN, INPUT );
  37. pinMode( RELAY_PIN, OUTPUT );
  38. pinMode( BUZZER_PIN, OUTPUT );
  39. pinMode( A1 + MOISTURE_PIN, OUTPUT );
  40. pinMode( A1 + WATER_PIN, OUTPUT );
  41.  
  42. // prepare serial connection back to computer
  43. Serial.begin( 9600 );
  44. }
  45.  
  46. // keep running forever (until power goes out)
  47. void loop() {
  48.  
  49. // read sensors
  50. int button = digitalRead( BUTTON_PIN );
  51. if (sensorCounter == 0 || button) {
  52. moisture = readSensor( MOISTURE_PIN );
  53. water = readSensor( WATER_PIN );
  54. }
  55.  
  56. // if water level is too low, turn on buzzer
  57. if (water < WATER_THRESHOLD) {
  58. digitalWrite( BUZZER_PIN, HIGH );
  59. } else {
  60. digitalWrite( BUZZER_PIN, LOW );
  61. }
  62.  
  63. // if there is enough water and moisture level is too low
  64. // or button is pressed, turn on pump
  65. if (water > WATER_THRESHOLD && moisture < MOISTURE_THRESHOLD) {
  66. digitalWrite( RELAY_PIN, HIGH );
  67. } else {
  68. digitalWrite( RELAY_PIN, LOW );
  69. }
  70.  
  71. // display current sensor values
  72. Serial.print( "moisture: " );
  73. Serial.print( moisture, DEC );
  74. Serial.print( " water: " );
  75. Serial.print( water, DEC );
  76. Serial.println();
  77.  
  78. // update counter
  79. sensorCounter++;
  80. if (sensorCounter == SENSOR_UPDATE_DELAY_SECONDS)
  81. sensorCounter = 0;
  82.  
  83. // sleep for one second
  84. delay( 1000 );
  85. }
  86.  
  87. // read the value of a soil moisture sensor
  88. int readSensor( int analogPin ) {
  89. digitalWrite( A1 + analogPin, HIGH );
  90. delay( 1 ); // 1 millisecond
  91. int value = analogRead( analogPin );
  92. digitalWrite( A1 + analogPin, LOW );
  93. return value;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement