Introduction: Automatic Water Gun

This is the instruction you will get the basic knowledge to make an automatic water gun that will follow a target to a certain degree and fire when the target is close enough. This project ended up needing a lot of fine tuning on specific parts of the casing and the range of motion and range of fire I wanted it to have. This is why I very much consider this guide as a stepping stone to guide someone on their way to making something similar since I do believe this project to be highly customizable in some ways. This instruction will just follow loosely what I have done to make this idea work the way I wanted it to. 

I would also like to mention that I do not consider this to be the final build of this project and that there can be many aspects improved. but for now it showcases my ideas perfectly.



Supplies

  • Arduino Uno + USB-cabel
  • 10 LED (red, yellow, green, bleu)
  • 2x 10kOhm resistance
  • 10x 330Ohm-resistance
  • 1x mini servo
  • 1x breadboard + wires
  • Hc-SR04 Ultrasonic distance sensor
  • pumping motor
  • tubing
  • water container (jar, bottel, etc)
  • casing (can be anything that seems fit)
  • (optional) The end of a piercing fluid bottle to control water flow.




wokwi link to the project scematics and code :

https://wokwi.com/projects/393504943001112577



The code:


#include <Servo.h> // include servo library

int trigPin1 = 9; // Trigger ultrasonic sensor 1
int echoPin1 = 8; // Echo ultrasonic sensor 1
int trigPin2 = 11; // Trigger ultrasonic sensor 2
int echoPin2 = 12; // Echo ultrasonic sensor 2
int pumpPin = 13; // Activating pump
int servoPos; // Position of servo
long duration1, cm1; // duration and distance in cm sensor 1
long duration2, cm2; // duration and distance in cm sensor 2
long objDistanceNew, objDistanceOld; // Distance of object to project
long distanceDiff; // Difference between old and new objDistance

Servo servo1;

void setup() {
//Serial Port begin
Serial.begin (9600);
//Define inputs and outputs
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(pumpPin, OUTPUT);

servo1.attach(3); //Attach the servo to pin 3
}

void loop() {
// The ultrasonic sensor 1 is triggered by a HIGH pulse of 10 microseconds
// We give a short LOW pulse beforehand to ensure a clean HIGH pulse.

digitalWrite(trigPin1, LOW);
delayMicroseconds(5);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);

// Read the signal from the ultrasonic sensor: a HIGH pulse which duration is the
// time in microseconds from the trigger sent to the reception of the echo reflected by an object.

pinMode(echoPin1, INPUT);
duration1 = pulseIn(echoPin1, HIGH);


delayMicroseconds(250); //delay to prevent mixed ultrasonic signals

digitalWrite(trigPin2, LOW);
delayMicroseconds(5);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);

pinMode(echoPin2, INPUT);
duration2 = pulseIn(echoPin2, HIGH);


// Convert the time into a distance
cm1 = (duration1/2) / 29.15; // Divide halve a duration by 29.15 (or divide by 0.0343)to get distance in cm.
cm2 = (duration2/2) / 29.15; // distance to an object = (speed of sound in air * time)/2
// speed of sound in air (20 degrees Celsius) = 343 m/s


// now we will calculate the position of the object between the 2 ultrasonic sensors.
// we rule out distances over 180 cm, to ensure the object is near.
// The distance decides the position of the object.
objDistanceNew = cm1-cm2;
if(cm1 < 180 and cm2 < 180) {
distanceDiff = objDistanceNew-objDistanceOld;

if(distanceDiff < 30 and distanceDiff > -30){ // To prevent large movement of the servo, we only move when difference between new and old distance is small
int servoPos = map(objDistanceNew, -40, 40, 10, 170); // scale distance to use it with the servo (value between 10 and 170 to prevent water on equipment)
servo1.write(servoPos);
delay(15);
Serial.print("Sensor 1: ");
Serial.print(cm1);
Serial.print("cm / Sensor 2: ");
Serial.print(cm2);
Serial.print("cm / distance diff: ");
Serial.print(distanceDiff);
Serial.print(" servopos: ");
Serial.print(servoPos);
Serial.println(); // printing the ouput to the serial monitor for testing purposes
}




objDistanceOld = objDistanceNew; // set old distance
}


// With the object between 3 and 20 cm, the pump is activated for 500 microseconds. we rule out smaller than 3 to rule out wrong measurements

if((cm1<20 and cm1>3) or (cm2<20 and cm2>3)) {
digitalWrite(pumpPin, HIGH);
delay(500);
digitalWrite(pumpPin, LOW);

Serial.print("Afstand voor pomp, cm1: ");
Serial.print(cm1);
Serial.print("cm / cm2: ");
Serial.print(cm2);
Serial.print("cm");
Serial.println(); // printing the ouput to the serial monitor for testing purposes
} else {
digitalWrite(pumpPin, LOW);
}

delay(250);
}

Step 1: The Idea

Every project starts with an idea. This idea started with a school project. For my study at the Hku we have a course called “if this then that” which is a course where we pretty much get the freedom to make anything we want with an arduino. Which is exactly what I did. After some thinking and talking with people I've come up with the idea of a water gun that would follow you and would, when close enough, spray water at you. This immediately became the project I would end up finalizing as it seems too fun not to. 


I began by sketching the project and then doing research about similar projects. After this I was ready to look at what exactly I would need to make this idea a reality. After ordering the opponents I started to get to work with some prototypes.



Step 2: Tracking

During the first steps we decided to check out what exactly the sensors could do and figure out how they worked. We did this by looking up some basic code and tweaking it to see the full potential of the sensors. After a lot of testing and trial and error we were able to connect two sensors to the arduino uno and then read the output from the sensors.while the actual code in the specific steps taken during this arent of importance. The reason i mention this as a step and encourage anyone who wants to follow this guide to do the same and to really start to understand how exactly the sensors work as it will prove extremely useful in the future. 


in the video linked you will see that after both sensors could simultaneously give out reading this output could be send to the servo to respond to the readings the way it responded during this phase was purely my moving when an object got closer again this was not the final idea but helped us get a better understanding of the technology we were working with.   

During this step code form https://docs.arduino.cc/learn/electronics/servo-motors/ and others form the site where extremely helpful in starting up the code and tweaking it to the desired outcome.

Step 3: Paper Prototyping

Paper prototyping or better said “cardboard prototyping” was the next step. to see what was needed for the casing we decided to place the project in a cardboard box to test out the set up and fully calibrate the servo motor to the sensors. The way it was set up ended up to be pretty tricky as it involved quite a lot of trial and error for the map of the servo. We needed to make sure the right sensors and input would react properly to the degree of the servo arm. We ended up doing this by taking 180 degrees and making sensor 1 on 0 degrees and sensor 2 on 180 degrees with the middle being 90 degrees. This ended up working and allowed us to have a fluid and accurate “tracking” of the servo arm to the target.

Step 4: Getting More Solid

After this the most logical thing was to switch the cardboard casting to a wooden one. This gave the structure more of a solid state and made the project feel all the more real. after spending what seemed like hours to remap and re calibrate the sensors the but finally it seemed to be working the same way it did with the cardboard box this meant that we could now easily attach the pumping motor and the tubing to make it so that instead of a light turning on the motor would start pumping the water. this obviously also came with some issues as it would require its own power source to run and a modulator to get the power to flow through everything the way it was supposed to. This is sadly where I suspect the next thing went wrong.

Step 5: Everything Fell Apart

As mentioned in the previous step, I suspect that the soldering went so wrong because of the power flow. While it could have also been due to my amatuer soldering skills and the half broken soldering iron I used , sadly the project just would not work properly anymore after soldering. If I had more time this would have definitely been something i would have wanted to get back to but sadly due to lack of time and a lot of misplanning i was not able to do so before the deadline.

Step 6: Gotta Keep Going

so looking at the time i had left and the time it would take to properly find out the fault in the soldering we decided it was best to go back the the set up we had with the breadboard. however this did not seem to work properly and had left us with a servo that wouldn't respond anymore. this is why we then replaced the servo with another servo motor this one being a little bit more powerful https://components101.com/motors/mg996r-servo-motor-datasheet

however this also came with some problems on its own since it had to be recalibrated and had to be retested to then alter the code and the map that was before strictly measured for the previous servo. After hours and even days of trying this we figured it out and would then also fix some of the irregular readings done by the sensors that were due to the faulty placement in the casing. After many panic attacks and cups of coffee trying to figure all the problems out it finally worked again. Then after some well needed rest the project was finally working now on to the decoration.



Step 7: The Final Product

With all the calibration, coding and fine tuning done it was finally time to decorate. With the idea mimicking the age old classic clown trick of watering a flower I decided to pay homage to this in the project by placing the end of the tubing that was getting a regulated flow of water due to an old end of a drip bottle for piercing aftercare in an old plastic flower. After this some improvements to the casing and the stability of the final product allowed some decorations that would really trick the “target” to get closer to the product. 


Despite all the hardships I am very proud of how this project turned out. if i magically had more time i would have definitely gone back to the soldering and improved upon the casing. but honestly this project was my first time ever working with an arduino and i have had a lot of fun creating something like this out of what seemed like nothing but some wires and other components. I'm very happy with the making of this project and I hope that if you are possibly wanting to try this yourself I would highly recommend it and also highly recommend making some changes of your own to this project as this is genuinely the fun of doing this kind of thing.