Circuit Playground Express Laser Tag
2017-10-25 | By Adafruit Industries
License: See Original Project Circuit Playground
Courtesy of Adafruit
Guide by John Park
The Circuit Playground Express has a built-in infrared (IR) transmitter and receiver. With a pair of Circuit Playground Express boards, you can send and receive messages that will cause the boards to run prepared operations. In this case, being "hit" with an IR blast causes the NeoPixels to count up from one to ten shots, and then score the final hit with an all red light fill and sound effect!
Play laser tag with a friend, or use it for target shooting. Too easy? You can also make it more of a challenge by adding a straw or empty pen barrel to cover the transmitter (TX) IR LED, thus narrowing its cone of light.
Here's all you'll need:
Code the Laser Tag Game
MakeCode
Before you start, be sure you're familiar with the basics of coding the Circuit Playground Express using MakeCode by going through this tutorial.
let hits = 0
let health = 0
input.buttonA.onEvent(ButtonEvent.Down, function () {
if (health == 1) {
pins.D13.digitalWrite(true)
network.infraredSendNumber(Colors.Indigo)
pins.D13.digitalWrite(false)
}
})
input.buttonB.onEvent(ButtonEvent.Down, function () {
if (health == 1) {
pins.D13.digitalWrite(true)
network.infraredSendNumber(Colors.Orange)
pins.D13.digitalWrite(false)
}
})
network.onInfraredPacketReceived(function ({ receivedNumber }) {
pins.D13.digitalWrite(true)
if (hits == 9) {
health = 0
}
if (health == 1) {
light.pixels.setPixelColor(hits, receivedNumber)
hits += 1
} else {
light.pixels.setAll(Colors.Red)
music.playSoundUntilDone(music.sounds(Sounds.PowerDown))
}
pins.D13.digitalWrite(false)
})
light.pixels.setAll(Colors.White)
health = 1
hits = 0
You're now ready to begin coding your own program with the MakeCode interface with the Circuit Playground Express
Click the Projects button so you can start a new project, then click the New Project... button to create a fresh, clean project.
When the program begins, turn the entire ring of NeoPixels white. To do so, you'll initialize your code with an on start loop from the Loops category.
Then, drag into the start loop a set all pixels to red block from the Light...More category. Then change the red dropdown to white.
To use the button as a trigger, add the on button A click loop from the Input category.
So that the input registers the moment you push the button, instead of a down/up motion of the click method, change it to down.
Since IR light isn't visible, you may want an indicator that your button has been pressed. Add in the digital write pin A0 to false block from the Pins category found in the Advanced section. Then, change the pin to D13 and the dropdown to true. This will set the onboard red LED at pin 13 to high, which is lit.
Transmitting IR Messages
IR message transmission is very easy to set up in MakeCode. There are only two blocks to use, infrared send number and on infrared received.
Drag one of the infrared send number blocks into the on button A down loop.
The message we'll send when triggered is the color choice we want to use on the LEDs when the target is hit. From the Light... More category, drag in the color red, onto the current 0 send message, then change it to indigo.
Now, add the on infrared received block.
So we can tell that the message has been received easily, duplicate the pin 13 LED block and place it here.
Variables
We'll make some variables that can be used as counters for the number of hits, and the health of a target. Hits will start at 0, and health will start at 1. So, create the variables by clicking Make a Variable in the variables category, and add their declarations and value assignments to the on start loop.
Conditions
The first condition we need to check is on the health of a target. When it is at 1, things are healthy and the target can perform as its own blaster as normal. When it is unhealthy, or 0, it can't do anything until being reset.
Add an if...then block from the Logic category to the button A block, and place the previous contents inside the loop.
Also get a 0 = 0 block from the same place, and us it as the conditional test, instead of the word true that's there by default.
Change the first 0 to test the health variable, by dragging one into that position, and set the second number to 1 as seen here.
Also, it's good to turn off the red led, so duplicate the digital write block as seen above, and change the true to false.
Next, it's time to make the receiver do something interesting upon each hit. We want it to do the following:
- record how many times it has been hit
- light up in a color an incrementing number of pixels
- set the health to 0 upon the final hit
Build this collection of blocks to do those things.
Use the else section of the conditional to light up all the LEDs to red when the final blast has been received.
It's fun to also play a sound effect when the target has been obliterated, so add in a play sound power down until done to the else section.
Also, you can make use of both buttons by duplicating the on button input and changing one to button B and send a different color message.
Upload the code to both of your Circuit Playground Express boards, and try shooting at them!
Build Blasters and Targets
To build your blaster and target, place the AAA batteries in each pack, then plug them in, and turn on the packs.
Use zip ties or double stick foam tape to connect the boards to the battery packs.
Modification
You can add a barrel to your blaster to narrow the IR beam spread. This will actually make aiming at your target much more challenging -- and fun! You can use a straw, a pen barrel, even a rolled up tube of paper.
Simply place the barrel over the TX LED on the Circuit Playground Express. You can hold it in place while playing, or stick it on with some poster putty.
Have fun, and remember, when a target is full you can just reset it and start all over again! You'll be an IR sharpshooter in no time!
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum