dual purpose motion/drought activated sprinkler/laser cannon

Hello, I have an area of my yard I want to keep my cats and dogs out of, I also frequently water this garden area, motion activated sprinklers are like $50.00, so I wanted to build a motion activated sprinkler using an uno board. I ordered a parallax PIR , a 12v 3/4" solenoid for the garden hose, an adjustable arduino moisture sensor (high == dry), a 5v relay and a sprinkler with a spike to stick in the ground.

I want the sprinkler to fire momentarily on motion and to fire when the hygrometer signal is high, so the damn dogs and cats stay away and the area stays as wet as I want. I took code for "button" and code for PIR and adapted and melded the two, successfully I hope, it compiles fine, but I have no idea if it'll work like I planned. any input?

/* 
 * //////////////////////////////////////////////////
 * //making sense of the Parallax PIR sensor's output
 * //////////////////////////////////////////////////
 *
 * Switches a LED according to the state of the sensors output pin.
 * Determines the beginning and end of continuous motion sequences.
 *
 * @author: Kristian Gohlke / krigoo (_) gmail (_) com / http://krx.at
 * @date:   3. September 2006 
 *
 * kr1 (cleft) 2006 
 * released under a creative commons "Attribution-NonCommercial-ShareAlike 2.0" license
 * http://creativecommons.org/licenses/by-nc-sa/2.0/de/
 *
 *
 * The Parallax PIR Sensor is an easy to use digital infrared motion sensor module. 
 * (http://www.parallax.com/detail.asp?product_id=555-28027)
 *
 * The sensor's output pin goes to HIGH if motion is present.
 * However, even if motion is present it goes to LOW from time to time, 
 * which might give the impression no motion is present. 
 * This program deals with this issue by ignoring LOW-phases shorter than a given time, 
 * assuming continuous motion is present during these phases.
 *  
 */

/////////////////////////////
//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;        

//the time when the sensor outputs a low impulse
long unsigned int lowIn;         

//the amount of milliseconds the sensor has to be low 
//before we assume all motion has stopped
long unsigned int pause = 5000;  

boolean lockLow = true;
boolean takeLowTime;  

int pirPin = 7;      //the digital pin connected to the PIR sensor's output
int relayPin = 8;    // designate pin 8 to control the solenoid relay
int moisturePin = 4; // designates pin 4 as hygrometer
int moistureState = 0; // variable for reading the moisturePin status


/////////////////////////////
//SETUPdigitalRead
void setup(){
  Serial.begin(9600);
  pinMode(pirPin, INPUT);
  pinMode(relayPin, OUTPUT);
  pinMode(moisturePin, INPUT);
  digitalWrite(pirPin, LOW);

  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
  }

////////////////////////////
//LOOP
void loop(){

     if(digitalRead(pirPin) == HIGH){
       digitalWrite(relayPin, HIGH);   //the solenoid relay energizes
       if(lockLow){  
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;            
         Serial.println("---");
         Serial.print("motion detected at ");
         Serial.print(millis()/1000);
         Serial.println(" sec"); 
         delay(50);
         }         
         takeLowTime = true;
       }

     if(digitalRead(pirPin) == LOW){       
       digitalWrite(relayPin, LOW);  //the solenoid relay de-energizes

       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause, 
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){  
           //makes sure this block of code is only executed again after 
           //a new motion sequence has been detected
           lockLow = true;                        
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
           }
           
      if(digitalRead(moisturePin) == HIGH){
        digitalWrite(relayPin, HIGH);  //if moisture sensor is high energize solenoid
        
        }

      if(digitalRead(moisturePin) == LOW){
      digitalWrite(relayPin, LOW);    //if moisture sensor is low de-energize solenoid
       }
     }
  }

P.S. this is my first project of this nature, prior to this I was just following tutorials, so please keep the flames on low :grin:

P.P.S Sorry, no laser cannon, made you look...I feel like if it was a laser cannon people would be more willing to help lol, so just pretend

Ok, so I figured out how to use a transistor, diode and relay to do it without an MCU, but whats the fun in that, I want a COMPUTERIZED sprinkler

but I have no idea if it'll work like I planned. any input?

Give it a try? Folks here will help debug if it doesn't work.

wildbill:

but I have no idea if it'll work like I planned. any input?

Give it a try? Folks here will help debug if it doesn't work.

Thanks, just waiting on some parts :slight_smile:

Id like to start by saying this is an amazing idea. Ordering the parts specified now. I am very very new to this, and as such had a couple problems. The first of which may be the system I am using but when I had put the code in the boolean command you had down was not working correctly. I had fixed this by typing in bool instead as I had seen it done like that once or twice. Also I was wondering if you had a circuit diagram or a page that shows how all this gets together. Again amazing idea I can't wait to see what it can do.