Arduino Avengers: Laser Tag

Published

Introduction: Arduino Avengers: Laser Tag

Have you ever wanted to become a superhero? This Arduino Laser tag project immerses you in game where you can act as a superhero firing at your opponents to victory.

Using a flex sensor, you can reenact the famous Spiderman web shooter and Iron Man repulsor beam. You are also able to keep track of your ammunition and life count on the LCD screen on your wrist. Every time you’re hit, you can feel the impact through a vibration motor. When you win, the speaker will play your theme song.


This project won best laser tag project (out of 12) at Binghamton University's Freshman Engineering Design Arduino Exposition.

Step 1: Parts

Everything but the tools were bought on Amazon and the total project was $140 (Arduinos were knockoffs).

Electronics

  • Arduino Uno (x2)
  • Protoboard (x2)
  • Flex Sensor (x2)
  • I2C LCD Display (x2)
  • IR Emitter (x2)
  • IR Receiver (x4) - 3 prong NOT 2 prong
  • Vibration Motor (x2)
  • Enclosed Piezo (x2)

Costume

  1. Spiderman Shirt
  2. Spiderman Mask
  3. Spiderman Gloves
  4. Iron Man Shirt
  5. Iron Man Mask
  6. Iron Man Gloves

Other

  • Arduino Housing (x2)
  • Latex Gloves
  • Lots of wire

Tools

  • Soldering iron and solder
  • Breadboard and Jumper Wires for prototyping
  • Wire Strippers

Step 2: Prototyping

I would suggest first testing each component (Piezo, Flex Sensor, LCD, Vibration IR Emitter and Receiver) separately before you begin. There's plenty of sample code all over the internet to test each.

Wire everything to the breadboard as you see in the diagram above.

Note:

  • The I2C LCD only has 4 pins, so wire accordingly
  • The vibration motor has a 3V max, so wire it to the 3V pin and ground it to pin 13 (this means the code to control it is reversed)
  • The flex sensor is denoted as a potentiometer in the schematic

Here's a link to the circuits.io schematic.

Step 3: Code

Codebender Link

#include <IRremote.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "pitches.h"

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

int RECV_PIN = 11;
int RECV_PIN2 = 10;
IRrecv irrecv(RECV_PIN);
IRrecv irrecv2(RECV_PIN2);
decode_results results;
IRsend irsend;

const int flexSensor = A0;
int lastFlexNum = 0;
int lives = 5;
int ammo = 50;

int buzzerPin = 13;

String superhero = "Spiderman";
//String superhero = "Iron Man";

void setup(){
    digitalWrite(buzzerPin, HIGH);
    pinMode(buzzerPin,OUTPUT);
    
	irrecv.enableIRIn();
	irrecv2.enableIRIn();
	
  	lcd.begin(16,2);
	lcd.backlight();
    lcd.clear();
    updateLCD();
    
    Serial.begin(9600);
}

void loop(){
	int flexVal = analogRead(flexSensor);
	if((lastFlexNum>350)&&((flexVal>100)&&(flexVal<350))){
		if(ammo>0){
			irsend.sendSony(0xa90, 12);
			
			irrecv.enableIRIn();
			irrecv2.enableIRIn();
			
			ammo = ammo - 1;
			updateLCD();
			delay(3000);
		}
	}
	lastFlexNum = flexVal;
	
    if (irrecv.decode(&results)) {
    	if ((results.value == 4045713590)||(results.value == 316007967)||(results.value == 2704)){
    		lives = lives - 1;
    		
    		lcd.setCursor(0,1);
  			lcd.print("                ");
  			lcd.setCursor(1,1);
  			lcd.print(">Front Shot<");
  		
    		digitalWrite(buzzerPin, LOW);
    		delay(2000);
    		digitalWrite(buzzerPin, HIGH);
    		updateLCD();
    	}
    	irrecv.resume(); // Receive the next value
    }
    if (irrecv2.decode(&results)) {
    	if ((results.value == 4045713590)||(results.value == 316007967)||(results.value == 2704)){
    		lives = lives - 1;
    		
    		lcd.setCursor(0,1);
  			lcd.print("                ");
  			lcd.setCursor(1,1);
  			lcd.print(">Back Shot<");
    		
    		digitalWrite(buzzerPin, LOW);
    		delay(2000);
    		digitalWrite(buzzerPin, HIGH);
    		updateLCD();
    	}
    	irrecv2.resume(); // Receive the next value
    }
    
    delay(100);
}

void updateLCD(){
    lcd.clear();
	if(superhero == "Spiderman"){
		lcd.setCursor(4,0);
		lcd.print("Spiderman");
	}
	else{
		lcd.setCursor(4,0);
		lcd.print("Iron Man");
	}
	
  	if (lives == 0){
  		lcd.setCursor(2,1);
  		lcd.print("You Died :(");
  		
  		playMusic();
  		
  		lcd.setCursor(0,1);
  		lcd.print("                ");
  		lives = 5;
  	}
	
  	lcd.setCursor(0,1);
  	lcd.print("Lives:");
  	lcd.setCursor(6,1);
  	lcd.print(lives);
    lcd.setCursor(9,1);
  	lcd.print("Ammo:");
  	lcd.setCursor(14,1);
  	lcd.print(ammo);
}

void playMusic(){
	if(superhero == "Spiderman"){
		int melody[] = {NOTE_D4, NOTE_F4, NOTE_A4, 0, NOTE_GS4, NOTE_F4, NOTE_D4, 0, NOTE_D4, NOTE_F4, NOTE_A4, NOTE_A4, NOTE_GS4, NOTE_F4, NOTE_D4,0, NOTE_G4, NOTE_AS4, NOTE_D5, 0, NOTE_C5, NOTE_AS4, NOTE_G4, 0, NOTE_D4, NOTE_F4, NOTE_A4, 0, NOTE_GS4, NOTE_F4, NOTE_D4, NOTE_AS4, NOTE_G4, NOTE_G4, NOTE_F4, NOTE_G4, NOTE_F4, NOTE_D4};
		int noteDurations[] = {4, 8, 4, 4, 4, 8, 4, 4, 4, 8, 4, 4, 4, 8, 4, 4, 4, 8, 4, 4, 4, 8, 4, 4, 4, 8, 4, 4, 4, 8, 4, 8, 2, 4, 6, 4, 6, 2};
		
		for (int thisNote = 0; thisNote < 38; thisNote++) {
			int noteDuration = 1200 / noteDurations[thisNote];
			tone(8, melody[thisNote], noteDuration);
			int pauseBetweenNotes = noteDuration * 1.30;
			delay(pauseBetweenNotes);
			noTone(8);
		}
	}
	else{
		int melody[] = {NOTE_B3, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_E4, NOTE_G4, NOTE_FS4, NOTE_G4, NOTE_FS4, NOTE_G4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_E4, NOTE_B5, NOTE_D5, NOTE_D5, NOTE_E5, NOTE_E5, NOTE_G5, NOTE_FS5, NOTE_G5, NOTE_FS5, NOTE_G5, NOTE_D5, NOTE_D5, NOTE_E5, NOTE_E5};
		int noteDurations[] = {4, 4, 8, 8, 4, 16, 16, 16, 16, 8, 8, 8, 8, 4, 4, 4, 8, 8, 4, 16, 16, 16, 16, 8, 8, 8, 8, 4};
		
		for (int thisNote = 0; thisNote < 29; thisNote++) {
			int noteDuration = 1200 / noteDurations[thisNote];
			tone(8, melody[thisNote], noteDuration);
			int pauseBetweenNotes = noteDuration * 1.30;
			delay(pauseBetweenNotes);
			noTone(8);
		}
	}
}

Step 4: Soldering

Solder wires and components to a protoboard the same way you did with a breadboard. This makes your project compact and ensures that all the wires stay connected.

Step 5: Build Housing and Attach to Costume

  • Enclose the Arduino in a box or some sort of casing (it can be as simple as a decorated envelope which is what was used in this project)
  • Attach a battery pack to the side which will also act as the Arduino power button.
  • Sew the LCD to Latex glove and wire it to the Arduino
  • Put the superhero glove over the latex glove and put on the rest of the costume
  • Wire the IR emitter to the bottom of the superhero glove (to aim and shoot IR beam)
  • Wire the IR receivers through the costume (one to front and one to back)

Step 6: Playing Instructions

You're all done!!

  • The goal of the game is to hit the other player’s sensor 5 times
  • When a person wants to fire a shot, they must bend the flex sensor which causes the IR emitter to send a signal
  • Players must aim the emitter at the receivers embedded within their opponent’s shirt
  • Each time a person is hit, their life count decreases by one
  • The game continues until one person has lost all of their lives (their super hero’s theme song will play indicating they lost)

Share

    Recommendations

    • Water Contest

      Water Contest
    • Creative Misuse Contest

      Creative Misuse Contest
    • Stick It! Contest

      Stick It! Contest

    25 Discussions

    We have been working on this project for quite some time now and still getting many error messages. My teacher, who usually makes us do all our troubleshooting and such only telling us that "it's broke and that is the problem," has noticed how much we have been struggling with this code. He told us that if the code compiles with the original code then the code is good. However, the code keeps on getting an error from the start with just trying to compile it. I liked the idea of this project but the code is making it almost impossible to actually do the project.

    2 replies

    I'm sorry you are having trouble with this. This is a complex project that involved a lot of tweaking, so I wouldn't be surprised if it doesn't work for other people. My guess is that any errors probably stem from the custom libraries. It's very hard to get the IR, LCD, and Tone libraries to work together, so as is explained in the project above, the libraries had to be custom edited. The edits are saved to codebender and you can try to upload to your board straight from the website, but you may have to make the changes yourself.

    Hello. My friend and I are trying to implement this project however, the code will not compile. We tried changing what you had said to do in previous comments but it didn't do much for us. Can you give us any other advice?

    5 replies

    Maybe check if you have the latest version of the I2C library

    We figured out that we are not using the same LCD's as you and found out how to fix that portion of the code. However, We are now having issues with the IRrecv portion

    We had taken out the I2C library since our LCD's are not I2C's like what he used. However, when you change that another error will pop up saying that the IRrecv isnt right and that is where we are having trouble now.

    Hi what board do you use when you upload the code? If you try to upload it says error about the board? Thanks

    Screenshot (7).png

    Hi,

    I used an Arduino Uno for my project. You may have to change the code to get the IR and speaker to work with a nano.

    my friend and i are trying to make this project however, the code refuses to compile despite adding the libraries necessary. help?

    3 replies

    Hi, I believe the tone library and the IR library use the same PWM pin, so you have to edit one of the libraries to change that. If you search the error, you might find the solution explained. I'm not sure where I changed the pin, but you check out my Codebender files (https://codebender.cc/sketch:398776#Laser%20Tag%20(Receive%20and%20Send).ino) and try to find which pin I chnaged

    the issue is in this line

    LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

    our arduinos dont like the POSITIVE at the end nor the i2c lcd

    Forgive me if this is a dumb question. But, would I be able to have for example, two spiderman and two iron man all play each other? Two players is fun. But four is better =)

    1 reply

    Yes. Very easy to do. In the code there's a variable at the beginning that you either set to "Superman" or "Iron Man." All you have to do is change it for each Arduino. This has no affect on the actual game. It just sets the name on the LCD and it dictates what music will play.