Introduction: Arduino Laser Tag - Duino Tag

Duino tagger- General introduction

Duino tag is a laser tag system based around the arduino.

Finally a laser tag system that can be tweaked modded and hacked until you have the perfect laser tag system for office ordnance, woodland wars and suburban skirmishes.

Laser tag is combat game like paintball or airsoft without the pain, it uses infrared light (IR) to simulate the tagging / shooting of other players or targets.

I have been working on this project for a while, but don't see it as over, I just though it was time to get more people involved. Hopefully this instructable will be near enough finished in time for me to enter it in the arduino competition, although I expect the instructable will need editing and tweaking for some time to come. 


This instructable aims to provide you with the information you will need to go out and build your own duino tagger.
This instructable focuses on how to build a duino tagger by modifying a light gun but with a bit of extra work you could build you own gun from scratch.

This instructable does not look in too much detail at the software / code side of the project, although a working code based on the miles tag protocol is provided.

For those wishing to learn about duino tagger programming I suggest you start at the excellent tutorials found at A Terrible Idea.

Thoes experience arduino users will probably find the overview page (Step 1) and code pages (Step 8) the most useful, newer arduino users may need to take a closer look at the instructable and the links provided.

I hope many of you will find this instructable useful and will go on to build your own duino taggers. There is much scope for improving and upgrading this system outlined here. If you do go on to improve on this duinotagger please share your work and hopefully in time the system will evolve into a much richer gaming experience.  


Youtube videos of my duino taggers:


This video shows me using the second duino tagger I made to shoot at a talcapult target I have been working on. I hope to make an instructable about the talcapult soon.





Step 1: Overview

Overview


Key parts of a duino tagger:



IR Transmitter system
What - Transistor amplifier, high power IR LED and a lens to give a well focused IR beam.
Why - To give the tagger means of tagging / shooting other players as well as communicating game information. The transmitter amplifies the transmission signal from the arduino and transmits it using an IR LED, lenses are used to make the signal very directional in order to make the guns behave like guns and make long range transmissions possible.

Sound
What - Peizo Sounder (& mini sound recorder / playback electronics from greetings card)
Why - It’s nice to have some audio feedback from the system. Sound effects to let you know when the tagger has been shot or run out of ammunition or lives.

Receivers
What - Standard IR receiver module and power regulation capacitor
Why - So the gun knows when it has been shot. This design in this instructable looks at using 3 sensors 1 on the gun as well as front and rear head sensors.

Visual Effects and lights
What - LED’s and bar graph driver circuits
Why - It is nice to get visual feedback on game information, eg lights to let you know when you have been shot. 




Cost
To make this section internationally friendly; at the time of writing: £1 GBP = $ 1.6 USD = 1.1 EUR

Assuming you already own an arduino the basic extra components for this project can be bought for around  £10.

Now is a great time to try to build a duino tagger as it is easy to get hold of cheap second hand light guns. Light guns are the guns that you can use with computer games, many of them don’t work with modern LCD screens and so there are a lot of them going cheap on ebay and else where. I bought two light guns each cost about £4 one off ebay one from a charity shop. Light guns are a great starting point for this project as they provide the optics, switches and case required for the build.


You will need:

Arduino
Light Gun
Coloured LED’s (preferably 30mA+)
IR sensors
At least 2x IR LED’s matched to the IR receiver  (preferably 100mA+)
Peizo sounder
Power transistor /MOSFET
A few electronics basics: solder, resistors , capacitors.

You may also want
Scrap plastic
LED bar graph driver chips
More LED’s
Record your own message greetings card
Hats / helmets / headbands to mount sensors on

Step 2: Basics

Basics

Page to try to help the electronics noobs.

Some basics which are useful at several points in the design of the duino tagger.

R = Resistor
C = Capacitor
V = Voltage
I = Current


Using LED's
LED's must be used with a resistor to limit the current through them.
Choose the current the LED will work at
R=(V supply -V led) /I
Not: You will probably just be able to copy the resistor values I used.

Buttons
The buttons (triggers etc) in this project are assumed to give a low voltage when pressed. The standard circuit for this is show in the circuit diagram provided. 


Filters
R C (Resistor Capacitor) circuits can come in useful at a few places in duino tagger, either for smoothing out changes in the power supply voltage or to turn PWM signals into analogue levels, not as complicated as it might sound:

To use the water analogy for electricity and current the resistor acts like a narrow pipe (imagine a straw) only allowing slow water (current) flow. The capacitor acts like a big water tank / bucket. The low pass filter in the diagram acts like a straw and bucket combo: Even if you suddenly change the flow of water in or out of the bucket the level (voltage) will only change slowly.

Cut off frequency for low pass: f = 1/(2pi*R*C)




Step 3: Modding the Light Gun


Modding the light gun


The details of the light gun modifications will depend on the light gun.
Here are some general guidelines and ideas:

Useful parts to leave in the:
Switches
Trigger
LED's
Lense


Adding storage space to your light gun: Fitting things in can be difficult, you might also not wish to put your arduino inside the gun. I wanted to be able to easily remove my arduinos from my duino taggers so I could use them in other projects.
On one of my duino taggers (MK1 gun) I cut holes in the case for the arduino nano pins to go through and mounted a socket on the inside of the gun so the arduino plugged on the outside of the gun. On my Mk2 gun I added an external case for the arduino and tried to make it look like an ammunition clip. The case / ammo clip was made from plastic I got from a cheap ring binder folder and its cover is held in place by a strong magnet.


Step 4: Transmitter

IR transmitter

You will need:
IR LED: Look for an LED with a narrow viewing angle (to ensure as much of the IR light makes it through the optics).
Higher power the better.
Pick LED’s with a wavelength that matches your IR receivers.
Get some spare LED’s as they are operated above their rated current so may burn out.

You can just attach an IR LED (with a current limiting resistor) to an output pin on the arduino and it will be able transmit, but the range of the duino tagger won’t be very impressive. In order to improve the range of the duino tagger amplification is needed.

The basic amplifier I made was a simple transistor circuit (circuit diagram provided), you may wish to consider a mosfet instead of the transistor.

Current through the LED: I aimed for about 300mA current through the IR LED. This is more than the rated current for the LED, but the LED seems to be able to cope because you are only pulsing the high current for a short time. If you are using the 5V supply from the arduino to power the LED it can only provide about 400/500mA so using 300mA for the IR transmitter Led does not leave too much for the other LED’s and speakers etc, so bare this in mind when designing building your duino tagger, it might be a good idea to add a voltage regulator capable of supplying higher currents.


Optics

You will need to use a lense to focus the IR beam, you can use the lense that came with the light gun. You can use a red LED to work out where to put the IR led to get a good focused beam.
For more details see the miles tag site.


Step 5: Receiver

Receivers

The receivers are for detecting when the tagger has been shot and receiving game data.

The receivers are standard IR remote control receivers like you would find in a TV remote.

There are quite a few different receivers you can choose from I went for the cheapest I could find the main things to consider are:

Will you be able to find a matching LED, one that works at the same light wavelength that the sensor is optimised for, If you don't match the LED and receiver the tagger range will be reduced.

If you want to be able to use your duino tagger to be compatible with any other duino tag, laser tag or miles tag systems you will need be working at the same modulation frequency and light wavelength.


Most of the IR receivers work in a very similar way and you will be able to wire then up the same as the circuit diagrams in the pictures.

The output pin of the receiver drops to a low voltage when a signal is being received. The outputs from multiple sensor can be connected and one sensor receiving will still cause the combined output signal voltage to drop.

The receivers work on there own and can be connected directly to the arduino, but they are much more reliable when used with capacitors to regulate the power supply and pull up resistors.







Step 6: Sound Effects

Audio / Sound Effects

Adding sounds to the system makes for a better game experience. It is nice to have sounds for the:

Fire, been shot, start, dead / out of lives sound, out of ammo..........

Ideally to add sounds you would use a fully featured audio shield like the wave shield.
keep me updated if you try this.


For simplicity and to save on build cost I chose just to use a piezo buzzer, this works well for most of the system tones, but it is tricky to use the piezo to make a good gun shot noise.
If you are interested in the details of making sounds on an arduino usining a piezo buzzer have a look at the melody example.

One simple and cheap way of adding better sound effects to your duinotagger is to use the electronics form a “record your own message” card.  You can get the cards for £2/3 off ebay.
Mini instructable.
1.    Remove the electronics from the greetings card.
2.    Play around with the electronics for a while to get an idea for what it all does.
3.    The card circuit will play the recorded sound as soon as it is powered. The card circuit draws about 50mA so can be powered direct from an arduino output, ideally you would power it at 3V to the card circuit (the same as the button cells that powered it previously), but I did not encounter any problems powering it with the 5v direct form the arduino. If you found that 5v was damaging the circuit you could diodes to reduce the voltage (just make sure you pick ones able to cope with 50mA). 
4.    Connect the arduino output to where the positive on the batteries originally powering the circuit would have been and connect the arduino ground to where the battery negative was. You should now find when the arduino output goes high the sound recorded on the card circuit is played.

Methods of recording the sound to the card circuit, you could just use the microphone (peizo transducer) that came with it or for slightly better sound quality you could use a potential divider (eg 10k pot) to connect it up to a computer and record directly to the circuit.


You may wish to consider adding an audio amplifier to make the sound effects a bit more impressive.

Step 7: Visual Effects / Lighting


Display / Visual Feedback

It is nice to have visual feedback for things like ammunition and lives there are a few ways you could do this: the main being using LED’s or an LCD to display the information.

I chose to uses LED’s, and there is provision for using LED’s in the code provided.
The code varies two of the arduino’s PWM pins( ammoPin and lifePin) dependant on the  ammunition and life.

The PWM pins can be used to power LEDs and the more life or ammo the player has the brighter the LEDs will be alternatively the PWM output can be used to drive an bar graph driver chip like the LM3914N
The PWM output can not be used to directly drive a bar graph driver chip, the PWM signal needs to be smoothed to give an analogue output, this can easily be done using an RC low pass filter.


Step 8: The Code

The Code


Code Notes:
I am not going to go into much detail about how the code works hopefully the fairly extensive comments provided in the code will be enough to explain it, if not the there are already good tutorials out there.

Limitations:
The code only works is set up in a way that it can only do one thing at a time and until the dual core arduino is easiest just to cope with that.
This means when one tagger is shooting it will not notice if it gets shot/tagged.  You could complicate the code by using interrupts , but in the real world it is not really a big problem, the shooting or getting shot happen so quickly that they very rarely interfere with each other.
In future iterations of the code I will probably make it so that the tagger checks to see if it has been tagged in-between each bit it transmits, making it very very unlikely that it would miss an incoming message (hit / tag). I have played a few games using this code and similar code and it seems to work well.

------------------------------------------------------------------------------------------------------------------


//      Start of code (copy and paste into arduino sketch)
//
//                       Duino Tag release V1.01
//          Laser Tag for the arduino based on the Miles Tag Protocol.
//           By J44industries:     www.J44industries.blogspot.com
// For information on building your own Duino Tagger go to: https://www.instructables.com/member/j44/
//
// Much credit deserves to go to Duane O'Brien if it had not been for the excellent Duino Tag tutorials he wrote I would have never been able to write this code.
// Duane's tutorials are highly recommended reading in order to gain a better understanding of the arduino and IR communication. See his site http://aterribleidea.com/duino-tag-resources/
//
// This code sets out the basics for arduino based laser tag system and tries to stick to the miles tag protocol where possible.
// Miles Tag details: http://www.lasertagparts.com/mtdesign.htm
// There is much scope for expanding the capabilities of this system, and hopefully the game will continue to evolve for some time to come.
// Licence: Attribution Share Alike: Give credit where credit is due, but you can do what you like with the code.
// If you have code improvements or additions please go to http://duinotag.blogspot.com
//



// Digital IO's
int triggerPin             = 3;      // Push button for primary fire. Low = pressed
int trigger2Pin            = 13;     // Push button for secondary fire. Low = pressed
int speakerPin             = 4;      // Direct output to piezo sounder/speaker
int audioPin               = 9;      // Audio Trigger. Can be used to set off sounds recorded in the kind of electronics you can get in greetings card that play a custom message.
int lifePin                = 6;      // An analogue output (PWM) level corresponds to remaining life. Use PWM pin: 3,5,6,9,10 or 11. Can be used to drive LED bar graphs. eg LM3914N
int ammoPin                = 5;      // An analogue output (PWM) level corresponds to remaining ammunition. Use PWM pin: 3,5,6,9,10 or 11.
int hitPin                 = 7;      // LED output pin used to indicate when the player has been hit.
int IRtransmitPin          = 2;      // Primary fire mode IR transmitter pin: Use pins 2,4,7,8,12 or 13. DO NOT USE PWM pins!! More info: http://j44industries.blogspot.com/2009/09/arduino-frequency-generation.html#more
int IRtransmit2Pin         = 8;      // Secondary fire mode IR transmitter pin:  Use pins 2,4,7,8,12 or 13. DO NOT USE PWM pins!!
int IRreceivePin           = 12;     // The pin that incoming IR signals are read from
int IRreceive2Pin          = 11;     // Allows for checking external sensors are attached as well as distinguishing between sensor locations (eg spotting head shots)
// Minimum gun requirements: trigger, receiver, IR led, hit LED.

// Player and Game details
int myTeamID               = 1;      // 1-7 (0 = system message)
int myPlayerID             = 5;      // Player ID
int myGameID               = 0;      // Interprited by configureGane subroutine; allows for quick change of game types.
int myWeaponID             = 0;      // Deffined by gameType and configureGame subroutine.
int myWeaponHP             = 0;      // Deffined by gameType and configureGame subroutine.
int maxAmmo                = 0;      // Deffined by gameType and configureGame subroutine.
int maxLife                = 0;      // Deffined by gameType and configureGame subroutine.
int automatic              = 0;      // Deffined by gameType and configureGame subroutine. Automatic fire 0 = Semi Auto, 1 = Fully Auto.
int automatic2             = 0;      // Deffined by gameType and configureGame subroutine. Secondary fire auto?

//Incoming signal Details
int received[18];                    // Received data: received[0] = which sensor, received[1] - [17] byte1 byte2 parity (Miles Tag structure)
int check                  = 0;      // Variable used in parity checking

// Stats
int ammo                   = 0;      // Current ammunition
int life                   = 0;      // Current life

// Code Variables
int timeOut                = 0;      // Deffined in frequencyCalculations (IRpulse + 50)
int FIRE                   = 0;      // 0 = don't fire, 1 = Primary Fire, 2 = Secondary Fire
int TR                     = 0;      // Trigger Reading
int LTR                    = 0;      // Last Trigger Reading
int T2R                    = 0;      // Trigger 2 Reading (For secondary fire)
int LT2R                   = 0;      // Last Trigger 2 Reading (For secondary fire)

// Signal Properties
int IRpulse                = 600;    // Basic pulse duration of 600uS MilesTag standard 4*IRpulse for header bit, 2*IRpulse for 1, 1*IRpulse for 0.
int IRfrequency            = 38;     // Frequency in kHz Standard values are: 38kHz, 40kHz. Choose dependant on your receiver characteristics
int IRt                    = 0;      // LED on time to give correct transmission frequency, calculated in setup.
int IRpulses               = 0;      // Number of oscillations needed to make a full IRpulse, calculated in setup.
int header                 = 4;      // Header lenght in pulses. 4 = Miles tag standard
int maxSPS                 = 10;     // Maximum Shots Per Seconds. Not yet used.
int TBS                    = 0;      // Time between shots. Not yet used.

// Transmission data
int byte1[8];                        // String for storing byte1 of the data which gets transmitted when the player fires.
int byte2[8];                        // String for storing byte1 of the data which gets transmitted when the player fires.
int myParity               = 0;      // String for storing parity of the data which gets transmitted when the player fires.

// Received data
int memory                 = 10;     // Number of signals to be recorded: Allows for the game data to be reviewed after the game, no provision for transmitting / accessing it yet though.
int hitNo                  = 0;      // Hit number
// Byte1
int player[10];                      // Array must be as large as memory
int team[10];                        // Array must be as large as memory
// Byte2
int weapon[10];                      // Array must be as large as memory
int hp[10];                          // Array must be as large as memory
int parity[10];                      // Array must be as large as memory


void setup() {
  // Serial coms set up to help with debugging.
  Serial.begin(9600);              
  Serial.println("Startup...");
  // Pin declarations
  pinMode(triggerPin, INPUT);
  pinMode(trigger2Pin, INPUT);
  pinMode(speakerPin, OUTPUT);
  pinMode(audioPin, OUTPUT);
  pinMode(lifePin, OUTPUT);
  pinMode(ammoPin, OUTPUT);
  pinMode(hitPin, OUTPUT);
  pinMode(IRtransmitPin, OUTPUT);
  pinMode(IRtransmit2Pin, OUTPUT);
  pinMode(IRreceivePin, INPUT);
  pinMode(IRreceive2Pin, INPUT);

  frequencyCalculations();   // Calculates pulse lengths etc for desired frequency
  configureGame();           // Look up and configure game details
  tagCode();                 // Based on game details etc works out the data that will be transmitted when a shot is fired


  digitalWrite(triggerPin, HIGH);      // Not really needed if your circuit has the correct pull up resistors already but doesn't harm
  digitalWrite(trigger2Pin, HIGH);     // Not really needed if your circuit has the correct pull up resistors already but doesn't harm

  for (int i = 1;i < 254;i++) { // Loop plays start up noise
    analogWrite(ammoPin, i);
    playTone((3000-9*i), 2);
  } 

  // Next 4 lines initialise the display LEDs
  analogWrite(ammoPin, ((int) ammo));
  analogWrite(lifePin, ((int) life));
  lifeDisplay();
  ammoDisplay();

  Serial.println("Ready....");
}


// Main loop most of the code is in the sub routines
void loop(){
  receiveIR();
  if(FIRE != 0){
    shoot();
    ammoDisplay();
  }
  triggers();
}


// SUB ROUTINES


void ammoDisplay() { // Updates Ammo LED output
  float ammoF;
  ammoF = (260/maxAmmo) * ammo;
  if(ammoF <= 0){ammoF = 0;}
  if(ammoF > 255){ammoF = 255;}
  analogWrite(ammoPin, ((int) ammoF));
}


void lifeDisplay() { // Updates Ammo LED output
  float lifeF;
  lifeF = (260/maxLife) * life;
  if(lifeF <= 0){lifeF = 0;}
  if(lifeF > 255){lifeF = 255;}
  analogWrite(lifePin, ((int) lifeF));



void receiveIR() { // Void checks for an incoming signal and decodes it if it sees one.
  int error = 0;

  if(digitalRead(IRreceivePin) == LOW){    // If the receive pin is low a signal is being received.
    digitalWrite(hitPin,HIGH);
    if(digitalRead(IRreceive2Pin) == LOW){ // Is the incoming signal being received by the head sensors?
      received[0] = 1;
    }
    else{
      received[0] = 0;
    }

    while(digitalRead(IRreceivePin) == LOW){
    }
    for(int i = 1; i <= 17; i++) {                        // Repeats several times to make sure the whole signal has been received
      received[i] = pulseIn(IRreceivePin, LOW, timeOut);  // pulseIn command waits for a pulse and then records its duration in microseconds.
    }

    Serial.print("sensor: ");                            // Prints if it was a head shot or not.
    Serial.print(received[0]); 
    Serial.print("...");

    for(int i = 1; i <= 17; i++) {  // Looks at each one of the received pulses
      int receivedTemp[18];
      receivedTemp[i] = 2;
      if(received[i] > (IRpulse - 200) &&  received[i] < (IRpulse + 200)) {receivedTemp[i] = 0;}                      // Works out from the pulse length if it was a data 1 or 0 that was received writes result to receivedTemp string
      if(received[i] > (IRpulse + IRpulse - 200) &&  received[i] < (IRpulse + IRpulse + 200)) {receivedTemp[i] = 1;}  // Works out from the pulse length if it was a data 1 or 0 that was received  
      received[i] = 3;                   // Wipes raw received data
      received[i] = receivedTemp[i];     // Inputs interpreted data

      Serial.print(" ");
      Serial.print(received[i]);         // Print interpreted data results
    }
    Serial.println("");                  // New line to tidy up printed results

    // Parity Check. Was the data received a valid signal?
    check = 0;
    for(int i = 1; i <= 16; i++) {
      if(received[i] == 1){check = check + 1;}
      if(received[i] == 2){error = 1;}
    }
    // Serial.println(check);
    check = check >> 0 & B1;
    // Serial.println(check);
    if(check != received[17]){error = 1;}
    if(error == 0){Serial.println("Valid Signal");}
    else{Serial.println("ERROR");}
    if(error == 0){interpritReceived();}
    digitalWrite(hitPin,LOW);
  }
}


void interpritReceived(){  // After a message has been received by the ReceiveIR subroutine this subroutine decidedes how it should react to the data
  if(hitNo == memory){hitNo = 0;} // hitNo sorts out where the data should be stored if statement means old data gets overwritten if too much is received
  team[hitNo] = 0;
  player[hitNo] = 0;
  weapon[hitNo] = 0;
  hp[hitNo] = 0;
  // Next few lines Effectivly converts the binary data into decimal
  // Im sure there must be a much more efficient way of doing this
  if(received[1] == 1){team[hitNo] = team[hitNo] + 4;}
  if(received[2] == 1){team[hitNo] = team[hitNo] + 2;}
  if(received[3] == 1){team[hitNo] = team[hitNo] + 1;} 

  if(received[4] == 1){player[hitNo] = player[hitNo] + 16;}
  if(received[5] == 1){player[hitNo] = player[hitNo] + 8;}
  if(received[6] == 1){player[hitNo] = player[hitNo] + 4;}
  if(received[7] == 1){player[hitNo] = player[hitNo] + 2;}
  if(received[8] == 1){player[hitNo] = player[hitNo] + 1;}

  if(received[9] == 1){weapon[hitNo] = weapon[hitNo] + 4;}
  if(received[10] == 1){weapon[hitNo] = weapon[hitNo] + 2;}
  if(received[11] == 1){weapon[hitNo] = weapon[hitNo] + 1;} 

  if(received[12] == 1){hp[hitNo] = hp[hitNo] + 16;}
  if(received[13] == 1){hp[hitNo] = hp[hitNo] + 8;}
  if(received[14] == 1){hp[hitNo] = hp[hitNo] + 4;}
  if(received[15] == 1){hp[hitNo] = hp[hitNo] + 2;}
  if(received[16] == 1){hp[hitNo] = hp[hitNo] + 1;}

  parity[hitNo] = received[17];

  Serial.print("Hit No: ");
  Serial.print(hitNo);
  Serial.print("  Player: ");
  Serial.print(player[hitNo]);
  Serial.print("  Team: ");
  Serial.print(team[hitNo]);
  Serial.print("  Weapon: ");
  Serial.print(weapon[hitNo]);
  Serial.print("  HP: ");
  Serial.print(hp[hitNo]);
  Serial.print("  Parity: ");
  Serial.println(parity[hitNo]);


  //This is probably where more code should be added to expand the game capabilities at the moment the code just checks that the received data was not a system message and deducts a life if it wasn't.
  if (player[hitNo] != 0){hit();}
  hitNo++ ;
}


void shoot() {
  if(FIRE == 1){ // Has the trigger been pressed?
    Serial.println("FIRE 1");
    sendPulse(IRtransmitPin, 4); // Transmit Header pulse, send pulse subroutine deals with the details
    delayMicroseconds(IRpulse);

    for(int i = 0; i < 8; i++) { // Transmit Byte1
      if(byte1[i] == 1){
        sendPulse(IRtransmitPin, 1);
        //Serial.print("1 ");
      }
      //else{Serial.print("0 ");}
      sendPulse(IRtransmitPin, 1);
      delayMicroseconds(IRpulse);
    }

    for(int i = 0; i < 8; i++) { // Transmit Byte2
      if(byte2[i] == 1){
        sendPulse(IRtransmitPin, 1);
       // Serial.print("1 ");
      }
      //else{Serial.print("0 ");}
      sendPulse(IRtransmitPin, 1);
      delayMicroseconds(IRpulse);
    }

    if(myParity == 1){ // Parity
      sendPulse(IRtransmitPin, 1);
    }
    sendPulse(IRtransmitPin, 1);
    delayMicroseconds(IRpulse);
    Serial.println("");
    Serial.println("DONE 1");
  }


  if(FIRE == 2){ // Where a secondary fire mode would be added
    Serial.println("FIRE 2");
    sendPulse(IRtransmitPin, 4); // Header
    Serial.println("DONE 2");
  }
FIRE = 0;
ammo = ammo - 1;
}


void sendPulse(int pin, int length){ // importing variables like this allows for secondary fire modes etc.
// This void genertates the carrier frequency for the information to be transmitted
  int i = 0;
  int o = 0;
  while( i < length ){
    i++;
    while( o < IRpulses ){
      o++;
      digitalWrite(pin, HIGH);
      delayMicroseconds(IRt);
      digitalWrite(pin, LOW);
      delayMicroseconds(IRt);
    }
  }
}


void triggers() { // Checks to see if the triggers have been presses
  LTR = TR;       // Records previous state. Primary fire
  LT2R = T2R;     // Records previous state. Secondary fire
  TR = digitalRead(triggerPin);      // Looks up current trigger button state
  T2R = digitalRead(trigger2Pin);    // Looks up current trigger button state
  // Code looks for changes in trigger state to give it a semi automatic shooting behaviour
  if(TR != LTR && TR == LOW){
    FIRE = 1;
  }
  if(T2R != LT2R && T2R == LOW){
    FIRE = 2;
  }
  if(TR == LOW && automatic == 1){
    FIRE = 1;
  }
  if(T2R == LOW && automatic2 == 1){
    FIRE = 2;
  }
  if(FIRE == 1 || FIRE == 2){
    if(ammo < 1){FIRE = 0; noAmmo();}
    if(life < 1){FIRE = 0; dead();}
    // Fire rate code to be added here  
  }

}


void configureGame() { // Where the game characteristics are stored, allows several game types to be recorded and you only have to change one variable (myGameID) to pick the game.
  if(myGameID == 0){
    myWeaponID = 1;
    maxAmmo = 30;
    ammo = 30;
    maxLife = 3;
    life = 3;
    myWeaponHP = 1;
  }
  if(myGameID == 1){
    myWeaponID = 1;
    maxAmmo = 100;
    ammo = 100;
    maxLife = 10;
    life = 10;
    myWeaponHP = 2;
  }
}


void frequencyCalculations() { // Works out all the timings needed to give the correct carrier frequency for the IR signal
  IRt = (int) (500/IRfrequency);  
  IRpulses = (int) (IRpulse / (2*IRt));
  IRt = IRt - 4;
  // Why -4 I hear you cry. It allows for the time taken for commands to be executed.
  // More info: http://j44industries.blogspot.com/2009/09/arduino-frequency-generation.html#more

  Serial.print("Oscilation time period /2: ");
  Serial.println(IRt);
  Serial.print("Pulses: ");
  Serial.println(IRpulses);
  timeOut = IRpulse + 50; // Adding 50 to the expected pulse time gives a little margin for error on the pulse read time out value
}


void tagCode() { // Works out what the players tagger code (the code that is transmitted when they shoot) is
  byte1[0] = myTeamID >> 2 & B1;
  byte1[1] = myTeamID >> 1 & B1;
  byte1[2] = myTeamID >> 0 & B1;

  byte1[3] = myPlayerID >> 4 & B1;
  byte1[4] = myPlayerID >> 3 & B1;
  byte1[5] = myPlayerID >> 2 & B1;
  byte1[6] = myPlayerID >> 1 & B1;
  byte1[7] = myPlayerID >> 0 & B1;


  byte2[0] = myWeaponID >> 2 & B1;
  byte2[1] = myWeaponID >> 1 & B1;
  byte2[2] = myWeaponID >> 0 & B1;

  byte2[3] = myWeaponHP >> 4 & B1;
  byte2[4] = myWeaponHP >> 3 & B1;
  byte2[5] = myWeaponHP >> 2 & B1;
  byte2[6] = myWeaponHP >> 1 & B1;
  byte2[7] = myWeaponHP >> 0 & B1;

  myParity = 0;
  for (int i=0; i<8; i++) {
   if(byte1[i] == 1){myParity = myParity + 1;}
   if(byte2[i] == 1){myParity = myParity + 1;}
   myParity = myParity >> 0 & B1;
  }

  // Next few lines print the full tagger code.
  Serial.print("Byte1: ");
  Serial.print(byte1[0]);
  Serial.print(byte1[1]);
  Serial.print(byte1[2]);
  Serial.print(byte1[3]);
  Serial.print(byte1[4]);
  Serial.print(byte1[5]);
  Serial.print(byte1[6]);
  Serial.print(byte1[7]);
  Serial.println();

  Serial.print("Byte2: ");
  Serial.print(byte2[0]);
  Serial.print(byte2[1]);
  Serial.print(byte2[2]);
  Serial.print(byte2[3]);
  Serial.print(byte2[4]);
  Serial.print(byte2[5]);
  Serial.print(byte2[6]);
  Serial.print(byte2[7]);
  Serial.println();

  Serial.print("Parity: ");
  Serial.print(myParity);
  Serial.println();
}


void playTone(int tone, int duration) { // A sub routine for playing tones like the standard arduino melody example
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }
}


void dead() { // void determines what the tagger does when it is out of lives
  // Makes a few noises and flashes some lights
  for (int i = 1;i < 254;i++) {
    analogWrite(ammoPin, i);
    playTone((1000+9*i), 2);
  } 
  analogWrite(ammoPin, ((int) ammo));
  analogWrite(lifePin, ((int) life));
  Serial.println("DEAD");

  for (int i=0; i<10; i++) {
   analogWrite(ammoPin, 255);
   digitalWrite(hitPin,HIGH);
   delay (500);
   analogWrite(ammoPin, 0);
   digitalWrite(hitPin,LOW);
   delay (500);
  }
}


void noAmmo() { // Make some noise and flash some lights when out of ammo
  digitalWrite(hitPin,HIGH);
  playTone(500, 100);
  playTone(1000, 100);
  digitalWrite(hitPin,LOW);
}


void hit() { // Make some noise and flash some lights when you get shot
  digitalWrite(hitPin,HIGH);
  life = life - hp[hitNo];
  Serial.print("Life: ");
  Serial.println(life);
  playTone(500, 500);
  if(life <= 0){dead();}
  digitalWrite(hitPin,LOW);
  lifeDisplay();
}


Step 9: Optional Extras

Other features

There is a lot of potential for the game to be improved the code I have provided transmits quite a lot of data when shooting another tagger. This leaves the potential for player statistics at the end of the game, different weapon types etc...

Force feedback

Many light guns come with an eccentrically weighted motor to give force feedback, on one of the duino taggers I made I wired this up to allow for force feedback, but it caused a lot of problems as the pp3 9v battery I was using could not really cope with the power demands. So only attempt to add force feedback if you have a decent battery pack.

Secondary fire:
I have started to write some provision for this into the code.

Compatibility:
I have tried to make the code fairly compatible with the MilesTag Protocol, the chances are it will be able to transmit and receive the data, although the code does not yet have much provision for acting on the information.





Step 10: Design Ideas

I have been thinking about building a duino tagger from scratch. Here are a few of the concept sketches I drew of gun design ideas. Hopefully they might offer some ideas and inspiration. 

Step 11: Links

Arduino Contest

Runner Up in the
Arduino Contest