Introduction: Nerf Sentry Gun

I started out with the goal of using a semi-automatic nerf gun to create a sentry gun. The idea was that if someone walks in front of the gun and is within a certain distance they would be pelted by nerf bullets.  This Idea came out of a game of Humans Vs. Zombies, I thought that it would be a lot easier if i had one of my guns programmed to shoot if someone was too close to a certain building. Thus I have started working on building a Nerf Sentry Gun.

Round 1: Rotating Movement and LED On/OFF

I started out by deciding to create the sweeping motion of the gun and then use a light turning on and off to mimic when the gun would be on/off (firing and not firing).
 I used a Vex Ultrasonic Sensor to determine distance from gun to nearest object and I used a servo motor to control the motion of the gun. 


Round 2: Adding in Gun and having to Change Motion Controller

I have Started working on adding in the gun to the circuit and product, however I have found that the Servo Motor is too weak to efficiently move the Gun back and forth. Thus I have had to change my motor from a Servo to a 12V Geared Dc Motor ( a Windshield Wiper Motor). Explanations on how to do so will be added when final product is working properly. 

Step 1: What You Will Need

Here is what you will need in order to make the First step of creating an Arduino Controlled Sentry Gun

1 Vex Ultrasonic Sensor
1 Servo Motor
1 LED
1 Arduino MC with Breadboard
1 470 Ohm Resistor1
10-15 Jumper Wires

Step 2: Circuitry


To Start off you need to connect arduino to breadboard, to do so connect the 5V pin on the arduino to the red rail on the breadboard and the ground pin on arduino to blue rail on breadboard.
Next connect your Ultrasonic Sensor, Servo Motor and LED to the Arduino

The Servo motor has a 3 pin connector that connects to the breadboard. the white wire pin connects to any of the PWM digital pins on the arduino ( I used pin 9 ). The red wire pin connects to 5V the red rail on bread board and the black wire connects to ground.


The Vex ultrasonic Sensor has two 3 pin connections an INPUT signal and an OUTPUT signal. Both of these connections wire up identically to the servo motor, except the white wire pin can connect to any unused digital pins. Just remember which pin is the Input signal and which one is the OUTPUT signal.  I used pin 7 as OUTPUT and Pin 12 as INPUT.

The LED connects to Ground on one side and any digital pin on the other. Use a 470Ohm resistor to connect led to the digital pin.

Circuit Diagram is Attached for your convenience

Step 3: The Code!!

Now all you need to do is write the code to make all of these different components work together.

First you begin with the Servo Sweep code from the Arduino Playground, which allows the servo to continually sweep in the way 


then you create a subroutine that finds the time it takes for an ultrasonic signal to be sent out and return to the input side of the sensor. Then it sets this value to a variable
here is the example code 
 of how to do it

digitalWrite(Trig_pin, LOW);   
         delayMicroseconds(2);
         digitalWrite(Trig_pin, HIGH);
          delayMicroseconds(5);
         digitalWrite(Trig_pin, LOW);
      duration = pulseIn(Echo_pin,HIGH);
          Serial.println(duration, DEC);  
         delay(50);


Then within the for loops of the sweep code create a while loop. that says when then duration value is below a certain number
the servo pauses, LED Turns on, and you check the sensor value again. 

Completely commented Code of my project is attached

/*
Sentry Gun
An Ultrasonic Sensor is continually scanning to find the distance to the nearest object
based on this value the Servo motor stops(gun stops sweeping) and LED turns on,
then checks sensor value again

Circuit:
Servo Motor is connected to Digital Pin 9 and Power Ground
LED is connected to digital pin 13
Input of Ultrasonic sensor is connected to pin 12
Output is connected to digital pin 7;

Created November 2013 by Krishna Peri
modified December 2013 by KRishna Peri
*/

#include <Servo.h>
#include <SoftwareSerial.h>


//const int Trig_pin =  7;   // Triggers Pulsoe for  Ultrasonic Sensor
const int Echo_pin= 12; // Recevies Echo for  Ultrasonic Sensor
int ledpin = 13; // led pin command pin
//long duration; // Time it takes for pulse to bounce back to RIGHT Ultrasonic Sensor
unsigned int duration = 1; // distance value for ultrasonic
Servo motor;               // Servo motor that controls
int pos = 0;    // variable to store the servo position
int Dcmotor = 10; // does nothing right now
void setup() {

  Serial.begin(9600);           // Set up Serial library at 9600 bps

  motor.attach(9);         // Servo motor is attached to pin 9
  // initialize the pulse pin as output:
  pinMode(Trig_pin, OUTPUT);        
  // initialize the echo_pin pin as an input:
  pinMode(Echo_pin, INPUT);
  pinMode(ledpin,OUTPUT);
  pinMode(Dcmotor,OUTPUT);
}


void loop()
  {    
      for(pos = 0; pos < 180; pos += 2)         // runs servo in steps of 5 from 0 to 180 
      {
        motor.write(pos);              
          delay(15); 
          Sensor ();
while (duration >10 && duration<400) {  // if sensor detects something to close
            delayMicroseconds(30); //pauses servo
            digitalWrite(ledpin,HIGH);  // turns on led
             Sensor();   // checks if object is still to close
   
  }
  digitalWrite(ledpin,LOW);  // turns off led
      }
  
      for(pos = 180; pos>=1; pos-=2)   // same as previous for loop except is running in opposite direction  
     {     
           motor.write(pos);             
           delay(15);
           Sensor ();
         
            while (duration >10 && duration<400) {
              digitalWrite(ledpin,HIGH);
            delayMicroseconds(30);
              Sensor();
   
   }
   digitalWrite(ledpin,LOW);
 
         }
      }
   
  void Sensor() {    // checks distance from sensor to nearest object straight ahead
    digitalWrite(Trig_pin, LOW);   
         delayMicroseconds(2); // delays
         digitalWrite(Trig_pin, HIGH);  //starts signal
          delayMicroseconds(5); // delay
         digitalWrite(Trig_pin, LOW); //stops signal
      duration = pulseIn(Echo_pin,HIGH);  // checks what the value is
          Serial.println(duration, DEC);   //prints out value
         delay(50);  // delays
           
           
    }

Step 4: Final Product

The Final Product should Function as Shown in the Video Below!!

Comments

About This Instructable

5,128views

33favorites

License:

More by sixhitter21:Nerf Sentry Gun
Add instructable to: