/* Arduino Projects for Dummies * by Brock Craft * * Chapter 10: Building and Automated Garden * A system that detects soil moisture and * triggers a solenoid valve * * v0.1 30.04.2013 */ int sensorPin = A0; // The input pin for the moisture sensor int valvePin = 9; // The output pin to actuate the valve const int dryThreshold = 950; // Set this threshold based upon your sensor tests // 1023 = bone dry, < 100 totally submerged const long sampleInterval = 600; // Sample every 10 minutes const int irrigationTime = 5000; // Duration to let the water flow boolean DEBUG=true; // Set to true if you want to read output on the Serial port void setup() { if(DEBUG){Serial.begin(9600);} pinMode(sensorPin, INPUT); // Declare the sensor pin as an OUTPUT: pinMode(valvePin, OUTPUT); // Declare the valve pin as an OUTPUT: digitalWrite(valvePin, LOW); // Make sure the valve is closed. } void loop() { int sensorValue = analogRead(sensorPin); // read the value from the sensor: if(DEBUG){ Serial.print("Sensor value: "); Serial.println(sensorValue); } if (sensorValue>dryThreshold){ digitalWrite(valvePin, HIGH); // Open the water valve delay(irrigationTime); // Keep it open for the irrigation time digitalWrite(valvePin, LOW); // Close the valve } delay(sampleInterval); // wait until the next time to take a reading }