3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Squirt - Arduino, motion activated water cannon

Step 3The Code

The Code
The arduino code (or "sketch") runs when turned on. Essentially the PIR looks for motion. If it's detected it waits a random length of time between half and 3 seconds, moves the servo arm to a random position 30 degrees either side of center, fires the pump for a half second and returns the servo to center. It then waits for another random length of time and repeats. It fires 3 times then checks the PIR again to see if it detects further motion. See sketch below:

#include <ServoTimer1.h>

/*
  • "Squirt". Jonathan Robson Feb 2009.
*
*/

int transistorPin = 8; // transistor base connected to pin 8
ServoTimer1 servo1; // defines the servo

long randOff = 0; // Initialise a variable for the OFF time between shots
long randNumber; // Initialise a variable for servo position angle and delay between shots

void setup()
{
servo1.attach(10); //servo on pin 10
pinMode(8, OUTPUT); // set the transistor pin 8 as output to pump
pinMode(5, INPUT); // set the PIR pin 5 as input
digitalWrite(8, LOW); // defines LOW as movement
randomSeed (analogRead (0)); // randomize

}

int pinin = 0;
long countint = 0;

void loop()
{
pinin = digitalRead(5); // reads the PIR sensor
while (pinin == 0)
{
pinin = digitalRead(5);
}
servo1.write(90); //sets servo to center
randOff = random (500, 3000); // generate OFF time between 1/2 and 3 seconds
delay(randOff); // waits for a random time while OFF
servo1.write(randNumber = random(60, 120)); // servo to random position within 30 degrees of center
delay(400); //gives servo time to get there
digitalWrite(transistorPin, HIGH); // turns pump on
delay(500); //fires pump for 1/2 second
digitalWrite(transistorPin, LOW); // turns pump off
servo1.write(90); // moves servo back to center

randOff = random (500, 3000); // generate new OFF time between 1/2 and 3 seconds and repeat
delay(randOff);
servo1.write(randNumber = random(60, 120));
delay(400);
digitalWrite(transistorPin, HIGH);
delay(500);
digitalWrite(transistorPin, LOW);
servo1.write(90);

randOff = random (500, 3000); // generate OFF time between 1/2 and 3 seconds and repeat
delay(randOff);
servo1.write(randNumber = random(60, 120));
delay(400);
digitalWrite(transistorPin, HIGH);
delay(500);
digitalWrite(transistorPin, LOW);
servo1.write(90);

delay(3000); // gives the PIR time to "settle" before reading again

}
« Previous StepDownload PDFView All StepsNext Step »
2 comments
Dec 1, 2009. 8:11 PMloususi says:
seriously ... this is incredible ... looks like i gotta get me the materials and start building my twist on this project ... thank you SO much

know anything about bioSensors?
Feb 27, 2009. 2:04 PMjoemartens says:
That is awesome! Just what I was looking for.

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
8
Followers
5
Author:Jonathan Robson
Science museum exhibit design and fabrication manager, stagehand, ballet shoe maker, theatre production manager, project manager and art director of the Beijing Aquarium rainforest exhibit, film extra...
more »