# Sword & Wand Prop Effects with Circuit Playground

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/036/824/medium800/sensors_swandThumb.jpg?1477498055)

![](https://cdn-learn.adafruit.com/assets/assets/000/036/761/medium800thumb/sensors_sword_cuDemo_02_64.jpg?1477344819)

Want to upgrade your prop sword/battle axe/light saber/magic wand with motion reactive light animation and sound effects? You can do just that with this simple project! All you'll need is a **Circuit Playground** or **Circuit Playground Express** , a battery pack, and zip ties to attach it all to any prop or toy.&nbsp;

https://youtu.be/hqGEoQycUnM

![](https://cdn-learn.adafruit.com/assets/assets/000/036/768/medium800thumb/sensors_wand_wave.jpg?1477357085)

![](https://cdn-learn.adafruit.com/assets/assets/000/036/769/medium800/sensors_sword_PA240049.jpg?1477359135)

Or,&nbsp;you can go a bit further and fabricate your own wand with embedded Circuit Playground from scratch using a 3D printer. This more advanced build will require&nbsp;a LiPoly battery and JST switch cable, plus a LiPoly charger, and the use of a 3D printer.

![](https://cdn-learn.adafruit.com/assets/assets/000/036/822/medium800/sensors_wand_PA250008.jpg?1477452892)

To program the lights and sounds, there are a couple of options. You'll&nbsp;learn to&nbsp;program the&nbsp;Circuit Playground Express using Microsoft **Make:Code**'s drag-and-drop web interface. Then, you'll program either the Circuit Playground Express or Circuit Playground with the Arduino IDE.

# Sword & Wand Prop Effects with Circuit Playground

## Circuit Playground Express with MakeCode

![](https://cdn-learn.adafruit.com/assets/assets/000/041/615/medium800/sensors_makecode01.jpg?1494442667)

## Lots of Coding Options

There are many&nbsp;options for programming the&nbsp;Circuit Playground Express! You can hand-code it with&nbsp;the traditional, text-based Arduino IDE, you can try your hand at the exciting new CircuitPython methods, or you can harness&nbsp;the incredibly user-friendly, yet powerful block-based interface of Microsoft's [MakeCode web app](https://makecode.adafruit.com/)!! (You can even go full circle and hand type code inside the MakeCode interface with Javascript!)

By simply dragging and dropping elemental code blocks, you can create Circuit Playground Express software in no time, and even test it out with the virtual Circuit Playground Express simulator built into the page.

Be sure you've gone through the [tutorial here on the basics of MakeCode](../../../../makecode?view=all) for Circuit Playground Express.&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/041/757/medium800thumb/sensors_3333_giffy1.jpg?1494628070)

# Wand Lights and Sound

You're now ready to begin coding&nbsp;your own program with the MakeCode interface to use theCircuit Playground Express as a magic wand prop.&nbsp;

Click the&nbsp; **Projects&nbsp;** button so you can start a new project.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/644/medium800/sensors_cpe_makeCodeWand1.jpg?1494452762)

Click the&nbsp; **New Project...** button to create the new empty project.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/645/medium800/sensors_cpe_makeCodeWand2.jpg?1494452879)

The new project should have only a single block in the layout area, the **forever&nbsp;** block. Any block of code you place inside the&nbsp; **forever&nbsp;** block will run over and over and over and over and over and over and over again. FOREVER! (Unless you create other inputs in your code that can change that, later.)

![](https://cdn-learn.adafruit.com/assets/assets/000/041/646/medium800/sensors_cpe_makeCodeWand3.jpg?1494453318)

## Rainbow Test
Try it out now. Click the **LIGHT** &nbsp;category. A collection of new blocks appears, all related to lighting up LEDs.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/647/medium800/sensors_cpe_makeCodeWand4.jpg?1494453559)

Click the block called&nbsp; **show frame of \_rainbow\_ animation&nbsp;** then drag and drop it inside the&nbsp; **forever&nbsp;** loop.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/648/medium800/sensors_cpe_makeCodeWand5.jpg?1494453701)

The animation block will snap inside the&nbsp; **forever&nbsp;** loop, and then after a moment, the Circuit Playground Express simulator will begin to play the rainbow animation.

In case the simulator doesn't play, click the play/stop button.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/649/medium800/sensors_cpe_makeCodeWand6.jpg?1494453883)

Notice what is happening here -- even though the&nbsp; **show frame of \_rainbow\_ animation&nbsp;** block only tells the&nbsp;Circuit Playground Express to play one frame, since it is inside the&nbsp; **forever&nbsp;** loop, it&nbsp;plays back a full animation over and over again.

Give your file a name in the name field, such as **cpe\_Wand**.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/651/medium800/sensors_cpe_makeCodeWand9.jpg?1494454812)

![](https://cdn-learn.adafruit.com/assets/assets/000/041/652/medium800/sensors_cpe_makeCodeWand10.jpg?1494457381)

As you did before, you'll Download the UF2 file, put Circuit Playground Express into bootloader mode by clicking the reset button, and then drag the UF2 file onto the CPLAYBOOT drive.

Now, the rainbow animation plays on your&nbsp;Circuit Playground Express!

## User Input

What would happen if you required user input to initiate the&nbsp; **show frame of \_rainbow\_ animation&nbsp;** code? Let's find out! Click the **INPUT&nbsp;** category, then pick the&nbsp; **on \_button A\_ \_click\_&nbsp;** block.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/650/medium800/sensors_cpe_makeCodeWand7.jpg?1494454327)

This adds the new input block to your layout. Drag and drop the&nbsp; **show frame of \_rainbow\_ animation&nbsp;** block out of the&nbsp; **forever&nbsp;** block and into the&nbsp; **on \_button A\_ \_click\_&nbsp;** block.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/663/medium800/sensors_cpe_makeCodeWand11.jpg?1494512948)

Try this out on the simulator by clicking the left button a few times. Each time you do so, the rainbow animation progresses by one frame.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/664/medium800/sensors_cpe_makeCodeWand12.jpg?1494513034)

Download the UF2 file, put your&nbsp;Circuit Playground Express into booloader mode, and drag UF2 file to the CPLAYBOOT drive.&nbsp;

Then, click the left button on your real world&nbsp;Circuit Playground Express to see the rainbow animation play back frame-by-frame!

NeoPixel LEDs can go from very dim to super bonkers bright! You can control them with a range of 0-256 levels of brightness. Adjust the overall brightness of all of the NeoPixels with the&nbsp; **set brightness&nbsp;** block. Click on the&nbsp; **LIGHT&nbsp;** category, then pick the&nbsp; **set brightness&nbsp;** block to add it to your layout area.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/666/medium800/sensors_cpe_makeCodeWand13.jpg?1494521267)

![](https://cdn-learn.adafruit.com/assets/assets/000/041/667/medium800/sensors_cpe_makeCodeWand14.jpg?1494521748)

## Setup

Notice how the new block is greyed out. This means that it isn't currently enabled. It needs to be part of a process that can run during some part of the program. Right now, this can either be any code blocks in the&nbsp; **forever&nbsp;** loop or&nbsp;any code blocks in the&nbsp; **on \_button A\_ \_click\_&nbsp;** loop. Neither of these is what we want in the case of setting overall NeoPixel brightness. Sorry guys! For this, we'll use an&nbsp; **on start** &nbsp;loop. Any code blocks inside of&nbsp; **on start**** &nbsp;**will only be run once when the Circuit Playground Express first starts&nbsp;up. Think of it like an initialization phase.

Click on the&nbsp; **LOOPS&nbsp;** category, then chose the&nbsp; **on start&nbsp;** loop.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/668/medium800/sensors_cpe_makeCodeWand15.jpg?1494522374)

Rearrange the&nbsp; **on start&nbsp;** loop to the top of the layout graph, just to keep things logically and spatially organized. Then, drag the&nbsp; **set brightness** block into the&nbsp; **on start&nbsp;** loop.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/669/medium800/sensors_cpe_makeCodeWand16.jpg?1494522549)

Try adjusting the brightness value by typing 140 into the&nbsp; **set brightness&nbsp;** number value block. Then, upload the UF2 file to your Circuit Playground Express.&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/041/670/medium800/sensors_cpe_makeCodeWand17.jpg?1494523022)

When you press the left button the frame of animation will display as before, only this time it'll be much brighter!

## Alternate Input

You can make your Circuit Playground Express do something while it waits to get instructions to do something else. What if we want to have the rainbow animation running unless the button is pressed, and then it'll play a sparkle animation for a moment?

You can do this by moving the&nbsp; **show frame of \_rainbow\_ animation&nbsp;** block into the&nbsp; **forever&nbsp;** loop, and then putting some new blocks into the&nbsp; **on \_buttonA\_ \_click\_&nbsp;** loop.

First, move the&nbsp; **show frame of \_rainbow\_ animation&nbsp;** block into the&nbsp; **forever&nbsp;** loop.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/671/medium800/sensors_cpe_makeCodeWand18.jpg?1494524715)

Info: 

![](https://cdn-learn.adafruit.com/assets/assets/000/041/672/medium800/sensors_cpe_makeCodeWand19.jpg?1494525008)

Duplicate the&nbsp; **show frame of \_rainbow\_ animation&nbsp;** block by right-clicking on it and choosing the duplicate dropdown menu item. Then, drag the new block into the&nbsp; **on \_buttonA\_ \_click\_ l** oop.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/673/medium800/sensors_cpe_makeCodeWand20.jpg?1494525163)

Change the animation pattern by clicking on&nbsp; **\_rainbow\_&nbsp;** and selecting&nbsp; **sparkle** instead.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/674/medium800/sensors_cpe_makeCodeWand21.jpg?1494525213)

Try uploading this UF2 code to your Circuit Playground Express. Notice that the rainbow animation plays constantly, until you press the left button. Then, there is a momentary flash as a single frame of the sparkle animation plays. It then returns to playing the rainbow animation, since that's in the&nbsp; **forever&nbsp;** loop.

Well, that's not very fun! Let's use a different kind of loop to play a bit more of the sparkle animation upon each button press.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/676/medium800/sensors_cpe_makeCodeWand22.jpg?1494525672)

From the&nbsp; **LOOPS&nbsp;** category, create a&nbsp; **repeat \_4\_ times/do&nbsp;** loop. This is a really useful loop that will execute any contained code blocks x many times, with x being the integer number you fill in. In more traditional Arduino programming this might be represented as:

`for(int i = 0; i<4; i++){`

`   //code to be repeated goes here`

`}`

So you can see how convenient this loop block is!

Drag&nbsp;the&nbsp; **show frame of \_sparkle\_&nbsp;**** animation&nbsp; **block into the&nbsp;** repeat \_4\_ times/do&nbsp;**loop.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/677/medium800/sensors_cpe_makeCodeWand23.jpg?1494526008)

The repeat/do and animation blocks are greyed out, since they don't sit inside of any loops that will be executed by the Circuit Playground Express.&nbsp;

Move them as a unit into the&nbsp; **on \_buttonA\_ \_click\_&nbsp;** loop.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/678/medium800/sensors_cpe_makeCodeWand24.jpg?1494526061)

Increase the number of repetitions from 4 to 24, then upload the program to the Circuit Playground Express and test it out by pressing the left button.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/679/medium800/sensors_cpe_makeCodeWand25.jpg?1494526167)

That's more like it! Now the rainbow animation plays until you click the left button, and you then get a moment of sparkle.

## Shake It

To use our magic wand and sword props, it'll be much more fun to trigger the lights and sounds with motion instead of a button press. The&nbsp;Circuit Playground Express has a built in accelerometer that can be used to detect different types of motion. Lets swat out the button push input for a shake input.

Chose the **shake&nbsp;** loop from the&nbsp; **INPUT&nbsp;** category.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/680/medium800/sensors_cpe_makeCodeWand26.jpg?1494526689)

Drag the repeat/do loop (including it's contained show frame of animation block) into the&nbsp; **shake&nbsp;** loop.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/681/medium800/sensors_cpe_makeCodeWand27.jpg?1494526704)

Get rid of&nbsp;the empty&nbsp; **on \_buttonA\_ \_click\_&nbsp;** loop by clicking on it and pressing delete on you keyboard.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/682/medium800/sensors_cpe_makeCodeWand28.jpg?1494526714)

Test the code on your board. Now, when you give it a good shake, the Circuit Playground Express will play back a bit of the sparkle animation, and then return to the rainbow!

Let's make the sparkles even more magical by setting them to maximum brightness!!

Make two duplicates of&nbsp;the&nbsp; **set brightness&nbsp;** block from the **on start** loop. Place one above the&nbsp; **repeat \_24\_ times/do&nbsp;** block, and one below it. Then, change tie value of the first one to 255.&nbsp;

Test it on your board -- now, when you shake the Circuit Playground Express the sparkle animation will be super bright!

Warning: 

![](https://cdn-learn.adafruit.com/assets/assets/000/041/683/medium800/sensors_cpe_makeCodeWand29.jpg?1494527509)

# Picking Themes

For versatility, you can make your Circuit Playground Express work in different modes, depending on the prop with which you want to use it. Here's how you can use the selector switch on board to pick between two different color and animation themes, one for the wand and the second for the sword.

## Slide Switch Input

Add an&nbsp; **on switch moved \_left\_ &nbsp;** loop. This is an input block, just like a button press or shake input, but uses the two position slide switch that's built onto the Circuit Playground Express.&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/041/689/medium800/sensors_cpe_makeCodeWand30.jpg?1494529394)

Duplicate the block and change the dropdown to make the&nbsp; **on switch moved \_right\_&nbsp;** loop.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/690/medium800/sensors_cpe_makeCodeWand31.jpg?1494529403)

## Variables

Next, we'll create a variable to help us check the state of the switch in other parts of the code. The variable is a named object that can store a value, in this case a number. Pick the&nbsp; **set \_item\_ to&nbsp;** block from the **Make A Variable** selections.

This block not only declares the variable, but can be used to change the value of the variable.&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/041/691/medium800/sensors_cpe_makeCodeWand32.jpg?1494529415)

Duplicate the&nbsp; **set \_item\_ to&nbsp;** block, and then place one inside each of the&nbsp; **on switch moved&nbsp;** loops. Also, set the value of the one in the&nbsp; **on switch moved \_right\_&nbsp;** loop to&nbsp; **1&nbsp;** instead of&nbsp; **0.**

![](https://cdn-learn.adafruit.com/assets/assets/000/041/692/medium800/sensors_cpe_makeCodeWand33.jpg?1494529423)

A nice thing about variables is that we can give them descriptive names that help you out. Let's rename the&nbsp; **item&nbsp;** variable to&nbsp; **theme**. That way you'll know whenever you see it in you code that this variable is used to store the value of which theme we're using: a **0&nbsp;** means Wand, and a **1** means&nbsp;Sword.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/693/medium800/sensors_cpe_makeCodeWand34.jpg?1494529430)

![](https://cdn-learn.adafruit.com/assets/assets/000/041/694/medium800/sensors_cpe_makeCodeWand35.jpg?1494529438)

![](https://cdn-learn.adafruit.com/assets/assets/000/041/695/medium800/sensors_cpe_makeCodeWand36.jpg?1494529446)

## Conditionals

You'll want to have the program logic react differently depending on the state of the slide switch.

- _ **If** _ the switch is on the left, _ **then** _ it will play lights and sounds for the Wand when shaken
- _ **If** _ the switch is on the right,&nbsp;_ **then&nbsp;** _it will play lights and sounds for the Sword when shaken

To do this, you'll use a conditional operator called an&nbsp; **if/then&nbsp;** loop. Add an&nbsp; **if/then&nbsp;** loop from the&nbsp; **LOGIC&nbsp;** category.

&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/041/696/medium800/sensors_cpe_makeCodeWand37.jpg?1494529457)

![](https://cdn-learn.adafruit.com/assets/assets/000/041/697/medium800/sensors_cpe_makeCodeWand38.jpg?1494529469)

We now need to create a block that lets&nbsp;the&nbsp; **if/then&nbsp;** loop know what condition needs to be met for it to evaluate as true or false. In our case, one loop will be true if&nbsp; **theme** equals **&nbsp;0,&nbsp;** and the other will be true if&nbsp; **theme** &nbsp;equals&nbsp; **1.**

From the&nbsp; **LOGIC&nbsp;** category, click on the&nbsp; **0 = 0&nbsp;** block.

Also, from the&nbsp; **VARIABLES&nbsp;** category, click on the&nbsp; **theme** block. This will allow you to compare the variable to a number (0 or 1).

![](https://cdn-learn.adafruit.com/assets/assets/000/041/699/medium800/sensors_cpe_makeCodeWand40.jpg?1494529485)

![](https://cdn-learn.adafruit.com/assets/assets/000/041/700/medium800/sensors_cpe_makeCodeWand41.jpg?1494529522)

Drag the&nbsp; **theme&nbsp;** variable onto the first&nbsp; **0&nbsp;** in the&nbsp; **0 = 0&nbsp;** comparison block -- it'll replace the first&nbsp; **0**. Then, drag the&nbsp; **theme = 0&nbsp;** block on top of the&nbsp; **true&nbsp;** segment of the&nbsp; **if/then&nbsp;** loop.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/701/medium800/sensors_cpe_makeCodeWand42.jpg?1494529530)

This loop is now set so that whenever the&nbsp; **theme&nbsp;** is set to **0&nbsp;** (the left position of the slide switch) it will run whatever code blocks are inside of the&nbsp; **then&nbsp;** section of the loop.&nbsp;

Move the set of blocks out of the&nbsp; **shake&nbsp;** loop and into the&nbsp; **if/then&nbsp;** loop.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/702/medium800/sensors_cpe_makeCodeWand43.jpg?1494529536)

Then, put the whole thing back into the&nbsp; **shake&nbsp;** loop.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/703/medium800/sensors_cpe_makeCodeWand44.jpg?1494529541)

So, here's what this section of your code now does:

1. Checks the slide switch
2. Sets the variable called&nbsp; **theme&nbsp;** to a&nbsp; **0&nbsp;** or&nbsp; **1&nbsp;** depending on switch position
3. Checks the accelerometer to feel for a&nbsp; **shake**
4. Compares the value of **theme&nbsp;** to the number&nbsp; **0**
5. If the&nbsp; **theme&nbsp;** does equal&nbsp; **0,&nbsp;** then it will bump up the brightness, play 24 frames of the&nbsp; **sparkle&nbsp;** animation, and then set the brightness back down

![](https://cdn-learn.adafruit.com/assets/assets/000/041/704/medium800/sensors_cpe_makeCodeWand45.jpg?1494529547)

Try this out on your board now. You'll see that with the selector switch in the left position, it behaves the same as before, running the rainbow animation until shaken, at which point it plays the sparkle animation for a moment. With the switch in the right position, however, the shake has no effect.

Now that that's working, let's create a loop to play a different animation when the switch is in the right position.

Duplicate the entire&nbsp; **if \_theme\_ = 0&nbsp;** loop, and then move the copy inside of the&nbsp; **shake&nbsp;** loop. Then, change the if statement value to 1, this means it will execute all code blocks inside of the if statement when the switch is to the right.

In this right switch loop, change the repeats to **300** and the animation to&nbsp; **comet**.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/708/medium800/sensors_cpe_makeCodeWand46.jpg?1494530397)

![](https://cdn-learn.adafruit.com/assets/assets/000/041/727/medium800/sensors_cpe_makeCodeWand47.jpg?1494603402)

Test it out on the board -- you now get two different shake animations depending on the position of the switch!

Let's use the same method to change the animation inside of the&nbsp; **forever&nbsp;** loop. This way, if you're using the Circuit Playground Express for the magic wand, you can set the switch to the left for **rainbows** and **sparkles** , or, set the switch to the right for sword mode which includes **comets** and **color wipes**!

Match your block layout&nbsp;to the one shown here, then test it out on your board.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/710/medium800/sensors_cpe_makeCodeWand48.jpg?1494530419)

## Sound

Now that you've gotten lights and action happening, it's time to add sound!&nbsp;

To begin with, click the **MUSIC** category, then click on the **MORE** &nbsp;button, then chose the **set speaker volume&nbsp;** button.&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/041/711/medium800/sensors_cpe_makeCodeWand49.jpg?1494537322)

Add this block to the&nbsp; **on start&nbsp;** loop, so that the speaker volume is initialized on startup.&nbsp;To avoid bothering your neighbors, you can pick a low volume, such as 20 as seen here, or really blast it up to 255!

![](https://cdn-learn.adafruit.com/assets/assets/000/041/712/medium800/sensors_cpe_makeCodeWand50.jpg?1494537460)

Next, create a block that makes some sound. Let's chose the&nbsp; **play melody \_power up\_&nbsp;** block. Place this inside the "if left switch" loop of the&nbsp; **shake&nbsp;** loop. This means that if the condition "shake" is met, and the condition "switch is in the left position" is met, the sound will play.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/713/medium800/sensors_cpe_makeCodeWand51.jpg?1494537845)

![](https://cdn-learn.adafruit.com/assets/assets/000/041/728/medium800/sensors_cpe_makeCodeWand52.jpg?1494603482)

You can select from a list of pre-made melodys, and then try them out by clicking the "shake" button on the Circuit Playground Express simulator. If you don't hear anything, try flipping the virtual selector switch to the other side.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/715/medium800/sensors_cpe_makeCodeWand53.jpg?1494538004)

![](https://cdn-learn.adafruit.com/assets/assets/000/041/716/medium800/sensors_cpe_makeCodeWand54.jpg?1494538199)

Duplicate the&nbsp; **play melody&nbsp;** block, insert the copy into the right switch if loop, and then choose a different melody to play during sword swing maneuvers!

![](https://cdn-learn.adafruit.com/assets/assets/000/041/729/medium800/sensors_cpe_makeCodeWand55.jpg?1494603528)

## Code Cleanup

It works! You're almost ready to attach your Circuit Playground Express to a wand or sword prop! But before you do so, let's clean up the code a little bit.

Often, when you are first creating a program, you'll copy and paste whole chunks of code that are meant to do similar things, and then change them slightly. This works great, but you can then optimizing things by removing redundant code blocks.

Let's have a look at the&nbsp; **shake&nbsp;** loop.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/718/medium800/sensors_cpe_makeCodeWand56.jpg?1494539080)

Since we built the first&nbsp; **if/then&nbsp;** loop and then duplicated it, we've got more copies of the&nbsp; **set brightness 255&nbsp;** and&nbsp; **set brightness 40&nbsp;** blocks than we need. Since we want to up the brightness whenever we shake, regardless of the position of the switch, that block can live outside of the&nbsp; **if/then** loops. The same is true of the block lowering the brightness once the shake is finished.

Move one&nbsp; **set brightness 255&nbsp;** block above and outside of the&nbsp; **if/then** loops, and one **set brightness 40&nbsp;** below and outside, as seen here. Then, delete the extras.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/719/medium800/sensors_cpe_makeCodeWand57.jpg?1494539329)

That's looking cleaner already! Next, let's consider the two&nbsp; **if/then&nbsp;** loops. Since our switch only has two states -- left and right -- we know that if the switch isn't to the left, then it must be to the right.&nbsp;

This is a great place to switch from two&nbsp; **if/then&nbsp;** loops to a single&nbsp; **if/then else&nbsp;** loop.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/720/medium800/sensors_cpe_makeCodeWand58.jpg?1494539506)

![](https://cdn-learn.adafruit.com/assets/assets/000/041/723/medium800/sensors_cpe_makeCodeWand59.jpg?1494539958)

The&nbsp; **if/then else&nbsp;** loop will replace both&nbsp; **if/then&nbsp;** loops. Drag the first condition --&nbsp; **theme = 0&nbsp;** -- over to the condition block of the&nbsp; **if/then else&nbsp;** loop.

Then, move the&nbsp; **play melody,** &nbsp; **repeat \_ \_ times,**** &nbsp; **and&nbsp;** show frame of \_ \_ animation,&nbsp; **blocks from the first&nbsp;** if/then&nbsp; **loop to the&nbsp;** then **section of the** if/then else&nbsp;**loop.

Do the same with the blocks in the second&nbsp; **if/then&nbsp;** loop, moving them to the&nbsp; **else&nbsp;** section of the&nbsp; **if/then else&nbsp;** loop.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/724/medium800/sensors_cpe_makeCodeWand60.jpg?1494540173)

Replace the two empty&nbsp; **if/then&nbsp;** loops with the&nbsp; **if/then else&nbsp;** loop.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/725/medium800/sensors_cpe_makeCodeWand61.jpg?1494540266)

Your code is more streamlined now! You can also optimize the&nbsp; **forever&nbsp;** loop in the same fashion, to look like this.

![](https://cdn-learn.adafruit.com/assets/assets/000/041/726/medium800/sensors_cpe_makeCodeWand62.jpg?1494540500)

Upload this code to your Circuit Playground Express and test it out. It should work identically, but it's good to know how you can optimize your code, especially if you start to create longer, more complex programs.

Here's what the final program looks like. Feel free to experiment now by changing things around, or making a whole new program from scratch!!

![](https://cdn-learn.adafruit.com/assets/assets/000/041/832/medium800/leds_cpe_makeCodeWand64.jpg?1495058084)

https://makecode.com/_PyCiyt810My4

# Sword & Wand Prop Effects with Circuit Playground

## Prep the Circuit Playground

Using the Circuit Playground to make lights and sounds when shaken is pretty straightforward. If this is your first Circuit Playground project, you'll first need to make sure you've installed the Arduino IDE, added the Circuit Playground library to the Arduino software, and and been through the basic tutorials on its use.&nbsp;This excellent guide, [Introducing Circuit Playground](../../../../introducing-circuit-playground/overview) and&nbsp;[Circuit Playground lesson #0](../../../../circuit-playground-lesson-number-0/add-circuit-playground-to-arduino?view=all) will show you how to do all of that and get you going.

![](https://cdn-learn.adafruit.com/assets/assets/000/036/739/medium800/sensors_projects_CircuitPlayground_illo_1600.png?1477279986)

Plug your **Circuit Playground** into your computer now, and launch the **Arduino IDE**. Double check that you've selected the board and port as in the Lesson #0 tutorial and are ready to upload code.

The **Arduino** sketch **Circuit Playground Make Believe** &nbsp;sketch does&nbsp;the following:

- Plays one&nbsp;light pattern and sound file while the Circuit Playground (and the prop weapon to which it has been affixed) is idle
- Plays a second light pattern and sounds file when swung
- Plays a third light pattern and sound file when tapped

&nbsp;

Download the file below, then unzip it and place it in your Arduino sketch directory.

[Make Believe code](https://github.com/adafruit/Adafruit_Learning_System_Guides/tree/master/CircuitPlaygroundMakeBelieve)
Open the CircuitPlaygroundMakeBelieve sketch in Arduino, you'll notice that there are also three other tabs that open -- knight.h, laser.h, and wand.h. These are three different theme sets, which are combinations of light animations and sound files that are triggered at different motion thresholds on the Circuit Playground's accelerometer.

Upload the sketch to your Circuit Playground and then try it out. The NeoPixels&nbsp;will play their idle animation and you'll hear the idle sound coming from the piezo speaker.

Swing the Circuit Playground around a bit to trigger the swing animation and sounds. This works because the code is checking the built-in accelerometer for sudden changes above a certain threshold. When the change is great enough, the swing is triggered.&nbsp;&nbsp;

The code also checks for even&nbsp;larger changes in acceleration, which indicates that there has been a tap on the Circuit Playground, or that your prop foam sword has hit its target! In this case, it will play the tap animation and sound.

The sketch&nbsp;uses pre-made theme sets&nbsp;of effects for use with different props. You can try them out to see which one works best for you.&nbsp;

If you look at the top of the Arduino sketch you'll see the theme choices. By uncommenting one (and commenting out the other -- there can only be one uncommented at a time) that theme set of lights and sounds will be used the next time you upload the sketch to your Circuit Playground.&nbsp;

For example:

```auto
// Enable ONE of these lines to select a theme:
//#include "knight.h" // Swoosh & clank metal sword
#include "laser.h"  // MWAAAW! "laser" sword
//#include "wand.h"   // Magic wand
```

will use the laser.h theme set. To switch to the wand.h theme set, do this:

```auto
// Enable ONE of these lines to select a theme:
//#include "knight.h" // Swoosh & clank metal sword
//#include "laser.h"  // MWAAAW! "laser" sword
#include "wand.h"   // Magic wand
```

# Sword & Wand Prop Effects with Circuit Playground

## Upgrade a Prop Sword

![](https://cdn-learn.adafruit.com/assets/assets/000/036/749/medium800/sensors_sword_PA240046.jpg?1477333251)

Here we have a very nice, off the shelf foam sword. It sure looks like it could use some light and sound effects!

![](https://cdn-learn.adafruit.com/assets/assets/000/036/750/medium800/sensors_sword_PA240049.jpg?1477333271)

There's a flat part on the sword guard that looks perfect for attaching the Circuit Playground.

Once you've uploaded your code to the Circuit Playground, it's time to connect it to the AAA battery pack and attach it all to your prop weapon.

![](https://cdn-learn.adafruit.com/assets/assets/000/036/744/medium800/sensors_sword_PA240053.jpg?1477333059)

Depending on your prop, you can choose different methods to attach it. Small zip ties are great because the are sturdy, fit through the holes on the Circuit Playground, and can be customized to different lengths by adding one zip tie to the end of another.

![](https://cdn-learn.adafruit.com/assets/assets/000/036/745/medium800/sensors_sword_PA240055.jpg?1477333087)

![](https://cdn-learn.adafruit.com/assets/assets/000/036/746/medium800/sensors_sword_PA240057.jpg?1477333127)

Here, you can see the Circuit Playground zip tied to a foam sword.

![](https://cdn-learn.adafruit.com/assets/assets/000/036/747/medium800/sensors_sword_PA240061.jpg?1477333160)

![](https://cdn-learn.adafruit.com/assets/assets/000/036/748/medium800/sensors_sword_PA240060.jpg?1477333224)

After pulling the zip ties tight, use diagonal cutters or scissors to clip the exess length from the ends.

![](https://cdn-learn.adafruit.com/assets/assets/000/036/751/medium800/sensors_sword_PA240063.jpg?1477333405)

![](https://cdn-learn.adafruit.com/assets/assets/000/036/752/medium800/sensors_sword_PA240064.jpg?1477333444)

Info: 

Next, feed the battery cable under&nbsp;the backside of the Circuit Playground, under the zip ties, then plug it in.

![](https://cdn-learn.adafruit.com/assets/assets/000/036/753/medium800/sensors_sword_PA240066.jpg?1477333546)

![](https://cdn-learn.adafruit.com/assets/assets/000/036/754/medium800/sensors_sword_PA240068.jpg?1477333571)

The battery pack fits nicely on the backside of the sword guard, also using zip ties. Create a zip tie harness for the battery pack by threading two through the back of the existing ones, double them with two more zip ties, then secure the battery pack. You can create an additional point of security with a set of three zip ties running around the battery pack horizontally.

![](https://cdn-learn.adafruit.com/assets/assets/000/036/755/medium800/sensors_sword_PA240070.jpg?1477333694)

![](https://cdn-learn.adafruit.com/assets/assets/000/036/756/medium800/sensors_sword_PA240071.jpg?1477333719)

![](https://cdn-learn.adafruit.com/assets/assets/000/036/757/medium800/sensors_sword_PA240072.jpg?1477333739)

![](https://cdn-learn.adafruit.com/assets/assets/000/036/758/medium800/sensors_sword_PA240074.jpg?1477333760)

Tighten the zip ties and clip the excess ends.

![](https://cdn-learn.adafruit.com/assets/assets/000/036/759/medium800/sensors_sword_PA240075.jpg?1477333821)

![](https://cdn-learn.adafruit.com/assets/assets/000/036/760/medium800/sensors_sword_PA240076.jpg?1477333843)

Swing it to try it out!

Next, we'll look at a more customized approch to building a prop magic wand from scratch to fit the Circuit Playground.

# Sword & Wand Prop Effects with Circuit Playground

## 3D Printed Magic Wand

![](https://cdn-learn.adafruit.com/assets/assets/000/036/791/medium800/sensors_wand_PA250011.jpg?1477429439)

This magic wand was designed specially to use the Circuit Playground as its centerpiece effect. It is 3D printed with a mix of translucent and opaque materials, has a mounting plate onto which you can secure the Circuit Playground, as well as an integrated LiPoly battery and on/off switch.

![](https://cdn-learn.adafruit.com/assets/assets/000/036/793/medium800/sensors_PA250005.jpg?1477429536)

In addition to the Circuit Playground, LiPoly battery, and switch, you'll also need four M3 x 6mm screws and nuts, two M3 x 16mm screws and nuts, and four M2 x 6mm screws and nuts.

![](https://cdn-learn.adafruit.com/assets/assets/000/036/788/medium800/sensors_wandCAD01b.png?1477411335)

![](https://cdn-learn.adafruit.com/assets/assets/000/036/786/medium800/sensors_wandCAD02.png?1477411221)

Here are the files you'll use to print it.

[starWand_stl.zip](https://cdn-learn.adafruit.com/assets/assets/000/036/784/original/starWand_stl.zip?1477410732)
Print the star parts in a translucent material, such as a natural PLA. The rest of the parts can be printed in any material you choose, and even painted for a more finished look.

![](https://cdn-learn.adafruit.com/assets/assets/000/036/792/medium800/sensors_wand_PA250007.jpg?1477429475)

Info: 

Next you'll&nbsp;assemble the wand. Make sure you've uploaded the Make Believe sketch to the Circuit Playground, then mount it to the mount plate using 2.5mm screws and nuts.

![](https://cdn-learn.adafruit.com/assets/assets/000/036/794/medium800/sensors_PA250008.jpg?1477429580)

![](https://cdn-learn.adafruit.com/assets/assets/000/036/795/medium800/sensors_PA250009.jpg?1477429603)

Next, place the switch into the handle cutout, feeding the plug ends through their respective holes for the battery and the Circuit Playground board. Plug the battery into the switch and the switch into the Circuit Playground's JST connector. Test the switch to make sure the Circuit Playground turns on.

![](https://cdn-learn.adafruit.com/assets/assets/000/036/796/medium800/sensors_PA250010.jpg?1477429629)

![](https://cdn-learn.adafruit.com/assets/assets/000/036/797/medium800/sensors_PA250011.jpg?1477429648)

![](https://cdn-learn.adafruit.com/assets/assets/000/036/798/medium800/sensors_PA250013.jpg?1477429670)

Place the battery in its place inside the lower handle bottom, then fasten the bottom with an M3 x 16mm screw and nut.

![](https://cdn-learn.adafruit.com/assets/assets/000/036/799/medium800/sensors_PA250015.jpg?1477429694)

![](https://cdn-learn.adafruit.com/assets/assets/000/036/800/medium800/sensors_PA250016.jpg?1477429712)

![](https://cdn-learn.adafruit.com/assets/assets/000/036/801/medium800/sensors_PA250017.jpg?1477429785)

Note how the lower switch wire and battery wire are tucked into the handle with the connectors accessiblye through the handle opening. This is so you can pull them out and dissconnect them for battery charging without unscrewing the wand.

![](https://cdn-learn.adafruit.com/assets/assets/000/036/802/medium800/sensors_PA250018.jpg?1477429803)

![](https://cdn-learn.adafruit.com/assets/assets/000/036/803/medium800/sensors_PA250025.jpg?1477429923)

Sandwich&nbsp;the Circuit Playground and mount between the two star halves, feeding the switch wire through the wire channel at the base of the star top half.

![](https://cdn-learn.adafruit.com/assets/assets/000/036/804/medium800/sensors_PA250022.jpg?1477430173)

![](https://cdn-learn.adafruit.com/assets/assets/000/036/785/medium800/sensors_wandCAD03.png?1477411169)

Begin the assembly&nbsp;in this order:

- Top half of handle
- Trim piece
- Star top
- Circuit Playground and mount
- Star bottom
- Bottom half of handle

![](https://cdn-learn.adafruit.com/assets/assets/000/036/805/medium800/sensors_wand_PA250008.jpg?1477430276)

Screw the handle top&nbsp;parts together with the other&nbsp;M3 x 16mm screw, placing the nut on the opposite side.

Add the remaining trim pieces on the top and bottom of the stars and screw them together with M2 x 8mm screws and nuts.

![](https://cdn-learn.adafruit.com/assets/assets/000/036/806/medium800/sensors_PA250024.jpg?1477430461)

Now, you can turn on the wand&nbsp;and wave and flick it to maximum magical effect. Should the battery ever require charging, you can pull the charging plug through the port in the handle and plug it into a LiPoly USB charger.

![](https://cdn-learn.adafruit.com/assets/assets/000/036/807/medium800/sensors_wand_PA250013_levels.png?1477430804)

Remember, this guide is just a leaping off point for using the Circuit Playground to add motion triggered effects to any prop you like!


## Featured Products

### Circuit Playground Classic

[Circuit Playground Classic](https://www.adafruit.com/product/3000)
Would you like to learn electronics, with an all-in-one board that has sensors and LEDs built in? **Circuit Playground** is here - and it's the best way to practice programming on real hardware with no soldering or sewing required!

This is the **Classic**...

In Stock
[Buy Now](https://www.adafruit.com/product/3000)
[Related Guides to the Product](https://learn.adafruit.com/products/3000/guides)
### 3 x AAA Battery Holder with On/Off Switch and 2-Pin JST

[3 x AAA Battery Holder with On/Off Switch and 2-Pin JST](https://www.adafruit.com/product/727)
This battery holder connects 3 AAA batteries together in series for powering all kinds of projects. We spec'd these out because the box is slim, and 3 AAA's add up to about 3.3-4.5V, a very similar range to Lithium Ion/polymer (Li-Ion) batteries and have an on-off switch. That makes...

In Stock
[Buy Now](https://www.adafruit.com/product/727)
[Related Guides to the Product](https://learn.adafruit.com/products/727/guides)
### JST 2-pin Extension Cable with On/Off Switch - JST PH2

[JST 2-pin Extension Cable with On/Off Switch - JST PH2](https://www.adafruit.com/product/3064)
By popular request - we now have a way you can turn on-and-off Lithium Polymer batteries without unplugging them.

This **PH2 Female/Male&nbsp;JST 2-pin Extension Cable&nbsp;** comes with an in-line **Switch** - click once to turn on, click again to turn off....

Out of Stock
[Buy Now](https://www.adafruit.com/product/3064)
[Related Guides to the Product](https://learn.adafruit.com/products/3064/guides)
### Lithium Ion Polymer Battery - 3.7v 150mAh

[Lithium Ion Polymer Battery - 3.7v 150mAh](https://www.adafruit.com/product/1317)
Lithium-ion polymer (also known as 'lipo' or 'lipoly') batteries are thin, light, and powerful. The output ranges from 4.2V when completely charged to 3.7V. This battery has a capacity of 150mAh for a total of about 0.6 Wh. If you need a larger battery, <a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/1317)
[Related Guides to the Product](https://learn.adafruit.com/products/1317/guides)
### Adafruit Micro Lipo - USB LiIon/LiPoly charger

[Adafruit Micro Lipo - USB LiIon/LiPoly charger](https://www.adafruit.com/product/1304)
Oh so adorable, this is the tiniest little lipo charger, so handy you can keep it any project box! Its also easy to use. Simply plug in the gold plated contacts into any USB port and a 3.7V/4.2V lithium polymer or lithium ion rechargeable battery into the JST plug on the other end. There are...

Out of Stock
[Buy Now](https://www.adafruit.com/product/1304)
[Related Guides to the Product](https://learn.adafruit.com/products/1304/guides)
### Circuit Playground Express Advanced Pack

[Circuit Playground Express Advanced Pack](https://www.adafruit.com/product/2769)
 **Circuit Playground Express** &nbsp;is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and made it even better! Not only did we pack even more sensors in, we also made it even easier to program....

Out of Stock
[Buy Now](https://www.adafruit.com/product/2769)
[Related Guides to the Product](https://learn.adafruit.com/products/2769/guides)

## Related Guides

- [Introducing Circuit Playground](https://learn.adafruit.com/introducing-circuit-playground.md)
- [Adafruit Circuit Playground Express](https://learn.adafruit.com/adafruit-circuit-playground-express.md)
- [3D Printed Flora Band](https://learn.adafruit.com/3d-printed-flora-band.md)
- [Circuit Playground Digital Input](https://learn.adafruit.com/circuit-playground-digital-input.md)
- [Digital Fidget Spinner](https://learn.adafruit.com/digital-fidget-spinner.md)
- [Anatomical 3D Printed Beating Heart with MakeCode](https://learn.adafruit.com/anatomical-3d-printed-beating-heart-with-makecode.md)
- [Cartoon Network and Make Code - Rose Quartz Shield Umbrella](https://learn.adafruit.com/cartoon-network-and-make-code-rose-quartz-shield-umbrella.md)
- [Circuit Playground Bike Glove](https://learn.adafruit.com/circuit-playground-bike-glove.md)
- [Neopixel Jewel 10 Minute Necklace](https://learn.adafruit.com/10-minute-neopixel-necklace.md)
- [Fidget Spinner Tachometer](https://learn.adafruit.com/fidget-spinner-tachometer.md)
- [Storage humidity and temperature monitor](https://learn.adafruit.com/storage-humidity-and-temperature-monitor.md)
- [Circuit Playground Bike Light](https://learn.adafruit.com/circuit-playground-bike-light.md)
- [Circuit Playground Express Compass](https://learn.adafruit.com/circuit-playground-express-compass.md)
- [Light-Activated Pixel Heart](https://learn.adafruit.com/light-activated-pixel-heart.md)
- [How Tall Is It?](https://learn.adafruit.com/how-tall-is-it.md)
- [Celebration Spectacles](https://learn.adafruit.com/celebration-spectacles.md)
