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.
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 block to use, infrared send number and on infrared received.
Drag one of the infrared send numbed 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!
Text editor powered by tinymce.