n00bsauce Sentry Gun

Adam Dewicki

Seamus Kaye

Phillip Birch

Description

The n00bsauce Sentry Gun is the next evolution in home and/or business defense.With the fact that it is fully automatic (for one shot), and auto fires and tracks a target, it alleviates the need for any personnel to work it. It combines two servo motors, one to turn the nerf gun and one to fire it, all on a tripod. It uses two photo resistors to track movement and a ping device to fire the gun.

Program Description

This part of the code sets the variables for the rest of the code.

const int pingPin = 14;

Servo myservo; // create servo object to control a servo

Servo myservo2; // create servo object to control a servo

int val; // declraes val to be a variable

int servoPin = 9;// variable to read the value from the analog pin

int servoPin2 = 3; //variable to read the value from the analog pin

int myAngle = 40; // sets myAngle to 40

int val2 = 0; // declares val2 as a variable

int potPin = 2; //potPin set in pin 2

int treshold; // declares treshold to be a variable

This tells the ping what to do and how often to read, and it also tells the photo resistors when to turn and when it there is light or not.

void loop()

{

long duration, inches, cm; //read in inches and cm

pinMode(pingPin, OUTPUT); // Declares ping to be an output

digitalWrite(pingPin, LOW); //off

delayMicroseconds(2); //wait two microseconds

digitalWrite(pingPin, HIGH);//on

delayMicroseconds(5);//wait five mircoseconds

digitalWrite(pingPin, LOW);//off

pinMode(pingPin, INPUT); //declares ping as an input

duration = pulseIn(pingPin, HIGH); //

inches = microsecondsToInches(duration); // reads microseconds as inches

cm = microsecondsToCentimeters(duration); // reads microseconds as cenetemeters

Tells the serial monitor what to read, what to read them as, and how often to read it.

Serial.print(inches);

Serial.print("in, ");

Serial.print(cm); // sets the serial moniter to read inches as in and centimeters as

Serial.print("cm"); // cm when the serial moniter reads it.

Serial.println();

}

long microsecondsToInches(long microseconds)

{

return microseconds / 74 / 2; // sets the rate at which the serial monitor reads the

// information being sent to it

}

long microsecondsToCentimeters(long microseconds)

{

return microseconds / 29 / 2;

}

Complete Program

#include <Servo.h>

const int pingPin = 14;

Servo myservo; // create servo object to control a servo

Servo myservo2; // create servo object to control a servo

int val; // declraes val to be a variable

int servoPin = 9;// variable to read the value from the analog pin

int servoPin2 = 3; //variable to read the value from the analog pin

int myAngle = 40; // sets myAngle to 40

int val2 = 0; // declares val2 as a variable

int potPin = 2; //potPin set in pin 2

int treshold; // declares treshold to be a variable

void setup()

{ pinMode(servoPin,OUTPUT); declares servoPin to be an output

Serial.begin(9600); // tell the board to begin serial communication

myservo.attach(9); // attaches the servo on pin 9 to the servo object

myservo2.attach(3); // attaches the servo on pin 3 to the servo object

Serial.begin (9600);

treshold = analogRead(potPin); //sets serial to 9600 bits per second

}

void loop()

{

long duration, inches, cm; //read in inches and cm

pinMode(pingPin, OUTPUT); // Declares ping to be an output

digitalWrite(pingPin, LOW); //off

delayMicroseconds(2); //wait two microseconds

digitalWrite(pingPin, HIGH);//on

delayMicroseconds(5);//wait five mircoseconds

digitalWrite(pingPin, LOW);//off

pinMode(pingPin, INPUT); //declares ping as an input

duration = pulseIn(pingPin, HIGH); //

inches = microsecondsToInches(duration); // reads microseconds as inches

cm = microsecondsToCentimeters(duration); // reads microseconds as cenetemeters

val2 = analogRead(potPin); // sets photoresitors as variable val2

if (val2 > treshold + 3 && myAngle<70){ // makes the photoresitors turn the servo motor towards the darker area

myAngle = myAngle + 5;

}

if (val2 < treshold - 3 && myAngle>10){

myAngle = myAngle -5;

}

myservo2.write(myAngle);

Serial.println(val2);

if (cm <= 90) {

myservo.write(5); // makes the other servo turn and fire the gun

myservo.write(90);

delay(100);

} else if (cm > 90) { // if distance is 90cm or less then it will turn the trigger servo

myservo.write(5);

}

Serial.print(inches);

Serial.print("in, ");

Serial.print(cm); // sets the serial moniter to read inches as in and centimeters as

Serial.print("cm"); // cm when the serial moniter reads it.

Serial.println();

}

long microsecondsToInches(long microseconds)

{

return microseconds / 74 / 2; // sets the rate at which the serial monitor reads the

// information being sent to it

}

long microsecondsToCentimeters(long microseconds)

{

return microseconds / 29 / 2;

}

Problems Encountered and Solved

There were a number of problems that we encountered while working on our project that we had to solve. The thing we had the most problems with was the code and having to go over it again and again to figure out what was wrong and fix it. Another big problem we encountered which our entire project relied on was the Nerf gun. The first gun we had was a six shooter, the trigger on the gun ended up being to hard to pull back and so the servo wasn't strong enough to fire the gun. We ended up going back to plan A where Mr. Dickie origionally gave us a small Nerf gun that fires one plastic dart; you touch the trigger just the slightest and it fires, therefore the servo was able to fire it. The result of changing the guns though was a shorter distance in which we can shoot. So we fixed being able to shoot the gun, but then we weren't able to shoot as far. Another problem we came across was the photo resistors won't work unless the ping is plugged in, so there was no solution to that except to have both plugged in at the same time. We had a problem with the photo resistor where when the gun would turn, the gun would cast a shadow on the photo resistor and make it turn back. To solve this, we moved the photo resistors farther apart from each other and shortened the turning radius of the servo that turns the Nerf gun.

Potential Improvements

If we had the money we would use a better gun, better photo resitors, and more wires.