Arduino + Wiimote + Lego Shooting Gallery Game

Arduino + Wiimote + Lego Shooting Gallery Game

I know, it’s quite a mouthful. I’ve only recently gotten into Arduino and tinkering with hardware, and this is my first project that I designed myself. I’m going to go into a fair amount of detail on how it was constructed, so if you have all the parts, you can make it yourself! I’ve even created a simple Unity game which works with the gun, which you can download for free by clicking here.

How Does it Work?

Essentially the gun is made up of an RF transmitter, which sends out signals detailing whether the switches for the trigger and the magazine holder are pressed or not. The second Arduino, connected to the computer, has an RF receiver which sends these signals to the computer.

The second main component is the WiiMote in the barrel of the gun. Using a driver (that I did not design) it can be used like a mouse on a computer. You simply point it at a couple of sources of infrared light, and it works (reasonably well)!

So the game is essentially just using the mouse cursor. You can even play the game with just a mouse. The switch states from the RF receiver are picked up through code in the game.

I’m going to be going step-by-step, and I designed this guide for people who are new to Arduino, as I am.

Here is the parts list:

  1. Two Arduinos (I used Arduino UNOs, but any kind should work)

  2. USB cables to connect your Arduino to a computer. Most Arduinos come with the proper type of cable

  3. Two breadboards (not necessary, but makes the project cleaner)

  4. RF transmitter and receiver

  5. A Bluetooth enabled Windows computer

  6. Jumper cables

  7. Two 10k Ohm resistors

  8. Two switches (also called buttons)

  9. A battery pack and appropriate battery (9v shown)

  10. A Wiimote

  11. Two candles (or any source of infrared light, IR LEDs will work as well)

  12. Lots of LEGOs! You can be fairly creative here, you don’t have to build the gun exactly as I did. I will still show the basics of how I put it together.

There is a shopping list at the end of this tutorial, in case you want to get the exact same components I have.

Where to Begin?

I originally started by building the LEGO frame, so let’s start there. I made mine fairly modularly, which makes it easier to modify, but more susceptible to breaking. In mine, the stock is red and black (completely cosmetic, feel free to skip this part), the trigger assembly is yellow, the magazine holder is blue, the clip itself is red, and the barrel (which holds the Wiimote) is white and red. The final part, which rests on top of the gun and houses the Arduino, is a melange of leftover colors.

As I said, feel free to take creative liberty in designing your own gun. These are just guidelines, so have fun with it!

The Stock

A simple cosmetic part. I designed the gun to look like an AK-47. Stocks on real guns are made to make them easier to hold, and absorb some of the recoil into the user’s torso.

The Trigger Assembly

This is a tricky part to build, and the first of two parts that will contain switches. Plan ahead when building this; you need room for wires to run, and you need to make sure the switch is firmly attached to the LEGO. I found these pieces to connect firmly to the switches I have.

If you attach two to either side, they do not connect perfectly to one connecting piece, as seen in the next picture. They are about three and a half modules apart (one module is one peg on a piece of LEGO. For example, the piece above is 2×1 modules).

Luckily, there are LEGO pieces that are two modules long, and have a single module directly in the center for essentially going one half module. You can connect the two sides with this piece, giving it much more stability, as shown;

Sometimes, getting LEGO to interface with another piece of hardware isn’t so simple, so the hard part is out of the way. Next, build up the solid part of the rest of the trigger. Remember to leave at least a little of it hollow, so that the wires can be run up into the Arduino. Try to find a nice balance between stability and thickness, as you will have to wrap your hand around it to pull the trigger. Mine ended up being four modules thick, with a 2×1 section left hollow in the middle for wires. Some of the sides are left out too, so you can get a better idea of how it is built. It becomes thicker the closer it gets to the core of the gun, as I found this prevents breakage.

 Connect the female to male jumper cables to the switch during this process, and feed them through your gap to the top of the trigger assembly. Connect the power and ground to one side of the switch. I use orange for power and white for ground, though the more common standard is red for power and black for ground. Connect the third and final cable opposite the ground, which I used a blue cable for.

The trigger itself is a simple LEGO Technic rod connected to a swivel piece that lets it move freely. You don’t even necessarily need this part, but it feels more like a gun if you’re not just pushing down on a switch.

The Magazine Holder and Magazine

This part will likely require some finesse. The tricky part here is getting a switch to recognize when the magazine is firmly entered into the gun. You may need to try different pieces and methods than I used. That is all part of the fun!

The first thing I did was build the magazine. Since this has no wires, it is essentially just a solid block of LEGO. Design it to fit your hand. I found 8×4 modules works well for me, and curved it at the end for comfort as well as cosmetic reasons. The very top is the part that will push in the switch, and I found a smooth piece gave me best results.

Next for the magazine holder itself. Design the base to fit the magazine perfectly, as expected, and leave a hole in the middle for the top of the magazine to fit in. This is where the switch will be placed.

Next, work on connecting the switch in a similar fashion to the trigger assembly. I only had two of the 2×1 connectors, so I used a slightly different connector this time (showed in bottom left of next picture). I also connected a piece of LEGO to the actual switch instead of the included piece of plastic. They fit on my switches perfectly, and effectively make the switch longer. They can rotate around a little bit, which is actually a benefit in our situation. Making the magazine press a switch when it is inserted is difficult, so this gives it a little bit of room to be pressed in lightly or a little firmer, and still press the switch down.

The Arduino case will be sitting very near this piece, so don’t worry too much about cable management.

The WiiMote Encasement

Okay, hard stuff is over for a little bit! Next we are making the barrel, which simply holds a WiiMote and connects to the magazine holder. I found the easiest way to do this was to build around a WiiMote. Some things to consider; this is going to be a fairly long piece, so build it up from underneath to make it less likely to fall off. Curve it into the magazine holder. Also, don’t forget about that dangling cord. I made a nice little holder for it to keep it out of the way.

Transmitter Arduino

“There’s still one LEGO part left!”, I hear you shouting. You’re right, but as with the WiiMote, it’s easier to build the Arduino case if we know what we’re building it around. Your Arduino might be a different size, as might your breadboard and battery encasement. Making the transmitter first will prevent some headaches.

Let’s start simple. Connect a jumper from the Arduino 5V connector to one row of pins, and the adjacent GND to another. This means that all pins in the same row are now connected to power and ground, respectively. Connect the RF transmitter on a separate section of the breadboard. Run a jumper from one of the power connected pins to the VCC pin of the transmitter, and run another from the ground to the GND pin. Lastly, run a jumper directly from the Arduino digital pin 12 to the DATA pin. When finished it should look something like this. Remember I use orange for power, white for ground, and blue for signal.

Next we connect the cables that will run to the two switches on the gun.

Start by running a jumper from the ground row to another empty row of pins. On that row, connect one end of a 10k Ohm resistor, and connect the other end to another empty row of pins. On this row, connect a long male to female jumper that will connect to the switch itself. Next, connect another male to female jumper to the row of power pins. This can connect directly to the switch, no need for a resistor. Connect a final male to female jumper to digital pin 2 on the Arduino. These three cables will connect to the three you connected earlier when building the trigger assembly.

Repeat this process for the other switch. Use unique pin rows for ground. The only other difference is to connect the signal cable to digital pin 4 on the Arduino. These jumpers will go to the switch that detects the magazine. Once finished, it should look something like this;

Transmitter Sketch

The sketch (aka the program) of the transmitter is straightforward. It sends out a signal with two numbers; both 0 or 1. 1 means a button is pressed, 0 means depressed. The first number sent is the state of the trigger switch, the second the magazine switch. The sent data is as simple as “0 1”, which would state that the trigger is not pressed, but the magazine is in. Here is a link to the code for simpler copying.

To upload the sketch to your Arduino, first download the Arduino software. Connect your Arduino to your PC with the included USB cable. Paste the sketch into the program, and click Sketch > Upload. Easy peasy!

#include <VirtualWire.h> //library for simple wireless communication

const int triggerPin = 2; //defining which input pins do what

const int clipPin = 4;

int triggerState = 0; //the variables holding the state of the switches

int clipState = 0;

int lastTriggerState=0; //these variables are used to determine if the

int lastClipState=0;    //switch state has changed since the last check

void setup()

{

  vw_setup(2000); // bits per sec, specific to transmitter

  vw_set_tx_pin(12); //transmitter pin

  pinMode(triggerPin,INPUT); //defining input pins

  pinMode(clipPin,INPUT);

  Serial.begin(9600); //for testing

}

void loop()

{

  char str[8]; //the string that will be sent wirelessly

  triggerState = digitalRead(triggerPin); //check the trigger pin

  if(triggerState == HIGH)

  {

   strcpy (str,"1"); //if it's pressed, we send a "1"

  }

  else

  {

    strcpy (str,"0"); //otherwise, send a "0"

  }

  strcat (str,"  "); //this adds a space between the two sent values

  clipState = digitalRead(clipPin); //check the magazine pin

  if(clipState == HIGH)

  {

   strcat (str,"1"); //if it's pressed, send "1", like before

  }

  else

  {

    strcat (str,"0");

  }

  if(lastTriggerState!=triggerState||lastClipState!=clipState)

  {             //this makes sure we only send data if it's not the exact

    send(str);  //same as it was before, that would be redundant.

  }             //send is a defined function below

  lastTriggerState=triggerState;

  lastClipState=clipState;

}

void send (char *message

{

  Serial.println(message); //print to the serial port, for testing

  vw_send((uint8_t *)message, strlen(message)); //send the string out

  vw_wait_tx(); // wait until the whole message is gone 

}

The Arduino Case

From a LEGO standpoint, this part is pretty straightforward. Build a case that will house your Arduino, breadboard, battery and all the cables. I also built a switch into the side that runs between the power coming from the battery, so I can just turn it off when not using it. This is totally optional, as simply disconnecting the battery serves the same purpose.

Finalizing the Gun

Connect your pin 2 jumper to the signal cable running from the trigger switch, and the power and ground cables appropriately. Connect the pin 4 jumper to the signal end of the magazine switch, and the power and ground opposite. Congratulations, you’re finished with the gun! Only one more piece of hardware to make, the receiver, and then we can move on to the software side!

Receiver Arduino

This part is pretty simple compared to the transmitter. It’s essentially the same thing, without the switches.

Connect a jumper from the 5V connector of the Arduino to a row of pins, and the GND to another row, same as before. Since we only have one component, you can even run them directly to the RF receiver. Connect the receiver to the breadboard, and connect power to VCC, and ground to GND, just like last time. Run a final jumper from digital pin 7 on the Arduino to the signal pin of the receiver. That’s it! Here’s what it will look like when you’re done;

Receiver Sketch

The receiver sketch is similarly simple. It accepts incoming messages and writes them to the serial port. This is for more than just testing this time around. The Unity game reads data off of the serial port directly. This means that once you’re done testing, make sure the serial monitor in the Arduino software is closed, as only one program can read from a serial port at one time. Here is the link for this sketch.

#include <VirtualWire.h> //the same library as before

byte message[VW_MAX_MESSAGE_LEN]; //a buffer to store the incoming messages 

byte messageLength = VW_MAX_MESSAGE_LEN; //the size of the message

void setup()

{

  Serial.begin(9600); //this time, this is important. unity reads from the serial port data

  vw_set_rx_pin(7);   //the data pin the receiver is connected to

  Serial.println("Device is ready");

  vw_setup(2000); //bits per sec

  vw_rx_start(); //start the receiver 

}

void loop() 

  if (vw_get_message(message, &messageLength)) //whenever we get a message or messages 

  

    for (int i = 0; i < messageLength; i++) //for each message

    

      Serial.write(message[i]); //write it to the serial port

    }

    Serial.println(); //and start a new line, ready for the next message

  

Using the WiiMote as a Mouse

Now we have the computer able to recognize which switches you have pressed on the gun, but that doesn’t explain how the computer recognizes where you’re aiming! Never fret, this is where the WiiMote comes in. WiiMotes work off of Bluetooth technology, and can connect to a computer the same way any other Bluetooth device does. However, Windows doesn’t know how to use it, so you must install a proper driver to get it to work. After trying many different ways to get the WiiMote properly recognized by Windows, I found this driver by Julian L?hr to work best. Follow the well detailed instructions on that page to get it to work.

Once the WiiMote is properly recognized, it requires two sources of infrared light to determine where it is facing. The Wii sensor bar works perfectly, as it is simply two infrared LED’s. If you have your own infrared LED’s, feel free to make your own. Alternatively, fire gives off infrared light, so candles work as well. This is a picture of the table I have in front of my screen which houses the candles and the receiver;

The Game

Now to the fun part. We have the gun sending it’s trigger state and working as a mouse. We’re ready to start the game! Simply download the game from this link, extract the .rar file, and run the .exe. Once in the game, you will need to specify which port your receiver Arduino is connected to. To find this out, open your control panel and go into the device manager. Here, expand the “Ports (COM and LPT)” section, and look for your Arduino. It is typically labeled as such, but if you’re not sure, unplug it and plug it back in to see which one it is. Take note of which COM port it is associated with. In the game, write this port in the input field in the bottom left, as COM9 or COM5, or whichever yours happens to be.

That’s it! You’re all set to play.

Source Code

Iif you’d like to take a look at the guts of the game, you can download the full source code and all resources as a Unity project by clicking here.

Video

You can watch a video of the game being played, and some footage of the gun in action by following this YouTube link.

Shopping List

I typically use aliexpress.com to purchase my components. They are cheap and of reasonable quality. They do take a while to ship to North America, and sometimes there is a lack of documentation.

Arduino Starter Kit. Contains one Arduino Uno, switches, resistors, jumper cables, a breadboard, and a battery holder, as well as plenty of other parts to get you started with making your own projects.

Second Arduino Uno

RF Transmitter and Receiver pair

As for LEGOs, I recommend sites like Kijiji or Craigslist to try and find it used locally. You can get a WiiMote at most any games or electronics store, online or off. 

Final Words

For me, building the gun and the game are just as much fun as playing it when it’s all finished. I was only introduced to the world of Arduino this year, but I foresee plenty of tinkering in my future.

Thanks for reading,

Brandon Dorey

Leave a Comment

Your email address will not be published. Required fields are marked *