]> git.piffa.net Git - arduino/blob - books/pdummies/APFD_Chapter10_Automated_Garden/APFD_Chapter10_Automated_Garden.ino
first commit
[arduino] / books / pdummies / APFD_Chapter10_Automated_Garden / APFD_Chapter10_Automated_Garden.ino
1 /* Arduino Projects for Dummies
2  * by Brock Craft 
3  *
4  * Chapter 10: Building and Automated Garden
5  * A system that detects soil moisture and 
6  * triggers a solenoid valve
7  *
8  * v0.1 30.04.2013
9 */
10
11 int sensorPin = A0; // The input pin for the moisture sensor
12 int valvePin = 9;   // The output pin to actuate the valve
13
14 const int dryThreshold = 950;  // Set this threshold based upon your sensor tests
15                                // 1023 = bone dry, < 100 totally submerged
16 const long sampleInterval = 600;  // Sample every 10 minutes
17 const int irrigationTime = 5000;  // Duration to let the water flow
18 boolean DEBUG=true;  // Set to true if you want to read output on the Serial port
19
20 void setup() {
21   if(DEBUG){Serial.begin(9600);}
22   
23   pinMode(sensorPin, INPUT);   // Declare the sensor pin as an OUTPUT:
24   pinMode(valvePin, OUTPUT);   // Declare the valve pin as an OUTPUT:
25   digitalWrite(valvePin, LOW); // Make sure the valve is closed.
26 }
27
28 void loop() {
29
30   int sensorValue = analogRead(sensorPin);    // read the value from the sensor:
31
32   if(DEBUG){
33     Serial.print("Sensor value: ");  
34     Serial.println(sensorValue);
35   }
36   
37   if (sensorValue>dryThreshold){
38     digitalWrite(valvePin, HIGH); // Open the water valve
39     delay(irrigationTime);        // Keep it open for the irrigation time
40     digitalWrite(valvePin, LOW);  // Close the valve
41   }
42   delay(sampleInterval);  // wait until the next time to take a reading
43 }
44