Here’s a nice, easy mission to start this section with: a name badge. This is to stop any spies from dressing up as your Dad and trying to enter your home. No name badge, no entry.
We’ll use the micro:bit to scroll a name across the LEDs, and we’ll use the Circuit Playground Express’s colored lights to make a unique secret pattern.
Before we start coding, we’re going to create our algorithm. Remember, an algorithm is a sequence of steps or rules for solving a problem. It’s not code; it’s like a plan for what we’re going to do next. The algorithm will be similar for all our devices.
To attach the micro:bit as a badge, take out the USB cable, and plug in the battery. Pop the battery pack in a shirt pocket, and dangle the device out the front. The micro:bit sits quite nicely in a shirt pocket. If you don’t have a shirt pocket, you could pop the battery pack into a T-shirt collar.
Alternatively, put some string through the holes in the micro:bit, and tie it carefully around your neck as a necklace. Don’t wear the micro:bit necklace to bed, and don’t use metallic string.
In this code, we’re going to show the string “Lorraine” when the micro:bit starts. Replace Lorraine with your name, your family member’s name, or your pet’s name you want to identify.
1. Test your code in the simulator.
2. If it’s working and you’re happy, download it to your computer and then transfer it to your micro:bit.
The problem with this code is that it will only run once when you start the device. To run it again, you will have to press the Reset button or take the battery out and put it back in again. Let’s change it so that the name appears every time we press the A button.
Add a secret message to the other button. Here’s the algorithm. You create the code.
To attach the Circuit Playground Express as a badge, take out the USB cable, and plug in the battery. Put the battery pack in a shirt pocket, and dangle the device out the front, just like the micro:bit picture earlier (Figure 7.1). Or pop the battery pack in a T-shirt neck, and let it dangle out the front like this!
Figure 7.1 S for Sam!
Figure 7.2 Show “Lorraine.”
Figure 7.3 Debugged.
Figure 7.4 Favorite colors!
The Circuit Playground Express has a ring of lights around the edge. These lights can be lots of different colors. For our name badge, we will flash all the lights in three different colors as a secret code. Only the right colors in the right order will let you in the house. What colors will you choose?
Figure 7.5 What are your favorite colors?
Figure 7.6 Debugged.
There are two problems here! If you tested your code earlier in the simulator, you will have spotted the second problem. The first problem with this code is that it will only run once when you start the device. To run it again, you will have to press the Reset button or take the battery out and put it back in again. Let’s change it so that the name appears every time we press the A button.
There’s one final bug in this code. Did you spot it? Try pressing A in the simulator. What happened? Why? In my simulator, the Circuit Playground Express only displayed orange. Why do you think this is? Why didn’t it show green or white?
Computers are really fast. The Circuit Playground Express did show green, white, and then orange. It just did it too fast for you to see. In order for us to see these colors, we need a pause between each one.
Figure 7.7 shows the new code to display green for a second, white for a second, and then orange.
Figure 7.7 Final code.
Test it again in the simulator. If you’re happy, download the code to the Circuit Playground Express. Give it to your trusted family members, and check their IDs from now on.
Let’s add a tune to the other button. Maybe it’s their favorite song, or maybe you can make up a tune for them?
See my secret tune on the website (savetheworld.mcunderwood.org).
3.143.228.40