loading
Haven't you ever wanted to be able to press a button to ward away unwanted intruders? Well now you can with just a few  simple steps, but first you'll need some supplies.

You will need:
- An Arduino board (any model should work)
- A rubber band gun (I just made one out of some wood scraps and a cloth-line clip used as a light trigger)
- A battery pack (at least 9 volts)
- An infrared remote and reciever
- A small servo motor
- Rubber bands
- And tape

Submitted by Powell Middle School for the Instructables Sponsorship Program.

Step 1: Wiring Up the Arduino

1st- hook up the 9g Servo motor to the number 9 pin.
2nd- hook up the infrared receiver to the number 11 pin.
3rd- feel semi-accomplished and make sure it looks something like this..

Step 2: Programming the Arduino

1st-You will need to include the IR library in Arduino (Here is a link)

2nd- Put in the code (Attached)

3rd- Use the #1 button to open the servo and the #2 button to close it


Here is the code-

#include <Servo.h>
#include <Ultrasonic.h>
#include "IRremote.h"
 
#define TRIGGER_PIN  12 // Ultrasonic sensor TRIGGER
#define ECHO_PIN     13 // Ultrasonic sensor ECHO
#define DISTANCE 20   // in Centimeters
#define WAITING 5000 // in milliseconds so this is 5 second.
#define SERVO_PIN 9
#define CLOSE_POS 90 // servo in CLOSE/UP position
#define OPEN_POS 180 // servo in OPEN/BACK position

  /*-----( Declare Constants )-----*/
  int receiver = 11; // pin 1 of IR receiver to Arduino digital pin 11
 
  /*-----( Declare objects )-----*/
  IRrecv irrecv(receiver);           // create instance of 'irrecv'
  decode_results results;            // create instance of 'decode_results'
  /*-----( Declare Variables )-----*/


//Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);
Servo myservo;  // create servo object to control the PEZ servo

void setup()
{
  Serial.begin(9600);
  myservo.attach(SERVO_PIN);  // attaches the servo to pin
  myservo.write(CLOSE_POS);   // turn servo to CLOSE/UP position
    Serial.println("IR Receiver Raw Data + Button Decode Test");
    irrecv.enableIRIn(); // Start the receiver
}

void loop()
{

   if (irrecv.decode(&results)) // have we received an IR signal?

  {
//    Serial.println(results.value, HEX);  UN Comment to see raw values
    translateIR();
    irrecv.resume(); // receive the next value
  } 
}
//  float cmMsec, inMsec;
//  long microsec = ultrasonic.timing();
//
//  cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
//  inMsec = ultrasonic.convert(microsec, Ultrasonic::IN);
//
//// this is for debugging
//  Serial.print("MS: ");
//  Serial.print(microsec);
//  Serial.print(", CM: ");
//  Serial.print(cmMsec);
//  Serial.print(", IN: ");
//  Serial.println(inMsec);
//  delay(1000);
//

 
 
//  void setup()   /*----( SETUP: RUNS ONCE )----*/
//
//    //Serial.begin(9600);
//    Serial.println("IR Receiver Raw Data + Button Decode Test");
//    irrecv.enableIRIn(); // Start the receiver
// 
//  }/*--(end setup )---*/
// 
 
//  void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
//  {
//  if (irrecv.decode(&results)) // have we received an IR signal?
//
//  {
////    Serial.println(results.value, HEX);  UN Comment to see raw values
//    translateIR();
//    irrecv.resume(); // receive the next value
//  } 
//}/* --(end main loop )-- */

/*-----( Declare User-written Functions )-----*/
void translateIR() // takes action based on IR code received

// describing Car MP3 IR codes

{
 
  switch(results.value)

  {

  case 0xFF30CF: 
    Serial.println(" 1              ");
    myservo.write(OPEN_POS);              // tell servo to go to OPEN/BACK
    break;

  case 0xFF18E7: 
    Serial.println(" 2              ");
    myservo.write(CLOSE_POS);              // tell servo to go to CLOSE/UP
    break;

  default:
    Serial.println(" other button   ");

  }

  delay(500);
}
 
//    myservo.write(CLOSE_POS);              // tell servo to go to CLOSE/UP
//    delay(15);             // waits 15ms for the servo to reach the position
// 
//
//
//    myservo.write(OPEN_POS);              // tell servo to go to OPEN/BACK
//    delay(WAITING);                       // waits sometime to allow grabbing of candy pellet
// 
//
//}

Step 3: Hooking Up the Gun

1st-  Tape the servo to the top of the gun so that the arm moves up when the 1 button is pressed.

2nd- tie/tape a string to the servo arm and gun trigger

3rd- Attach the Arduino and the battery pack to the side of the gun it should look somewhat nice, like this...

Step 4: Test!

Test it! I will attach a video of it operating.
<p>hi im trying to controll my arduino to move a servo. </p><p>Could you help me put in the code?</p><p>The controll gives off the infrared as i press left: FF22DD and right FFC23D. Please help me figure this out.<br><br> #include &lt;Servo.h&gt;<br> #include &lt;IRremote.h&gt;<br><br> unsigned long Value1 = 0xFF22DD; // where XXXXXXXX is on our your remote's values<br> unsigned long Value2 = 0xFFC23D; // where XXXXXXXX is another button on your remote<br><br> int RECV_PIN = 11;<br> IRrecv irrecv(RECV_PIN);<br> decode_results results;<br><br> Servo servo1;<br><br> // the setup routine runs once when you press reset:<br> void setup() { <br><br> Serial.begin(9600);<br> irrecv.enableIRIn(); // Start the receiver<br><br> // initialize the digital pin as an output.<br><br> servo1.attach(10); // attack servo to digital pin 10<br> }<br><br> // the loop routine runs over and over again forever:<br> void loop() {<br><br> if (irrecv.decode(&amp;results)) {<br> <br> Serial.println(results.value, HEX);<br> irrecv.resume(); // Receive the next value<br> }<br><br> if(results.value == Value1) {<br> servo1.write(160);<br> }<br> else if (results.value == Value2){<br> servo1.write(75);<br> }<br> } </p>

About This Instructable

2,918views

7favorites

License:

More by powellcubs:Java jump minigame Homemade LED Light The "Mister Fantastic" Sensor 
Add instructable to: