Coding the Confetti Box
Duration: ~25 minutes
What’s in the code?
Concepts
- Sensors (Sound)
- Pixels
- Pin functions (servo)
Blocks
music.powerUp.play()
light.showRing("red red red red red red red red red red")
pins.A2.servoWrite(180)
pause(0)
Let’s code it!
|Step 1|
- Testing the servo
- Open MakeCode in your web browser.
- From PINS, drag out a
||pins:servo write||
block, and place it inside a||loops:forever||
loop to change the position of the servo. Make sure you set the pin toA1
. The servo can only turn between 0° and 180°, so try playing around with values inside that range and watch the servo turn. To close the door, a value of about 180° should work. Also, to open the door, a value of about 90° should work well. - Connect the Adafruit Circuit Playground Express to your computer with the USB cable and click Download. Follow the instructions to move the code to your Adafruit Circuit Playground Express.
forever(function () {
pins.A1.servoWrite(90)
})
|Step 2|
- Sound sensing
In LOGIC, find the
||logic:if then else||
block. Drag the block and place it inside a||loops:forever||
loop. Also from LOGIC grab a comparison,||logic:0 < 0||
, block and set it to the greater than symbol. Finally, from INPUT take a||input:sound level||
block and place it inside the comparison block.From LIGHT drag two
||light:show ring||
blocks into the workspace. Place one block into the||logic:if||
part and the other into the||logic:else||
part of the||logic:if then else||
statement. Customize the neopixel lights however you like, but make sure the rings are different from each other.Input a value in the comparison block to compare the sound value to. Then move your code to the Adafruit Circuit Playground Express. Try making some noise and see if you observe the lights changing. Experiment by comparing different sound level values.
forever(function () {
if (input.soundLevel() > 18) {
light.showRing(
"red red red red red red red red red red"
)
} else {
light.showRing(
"green green green green green green green green green green"
)
}
})
|Step 3|
- Putting it together
- From PINS drag two
||pins:servo write||
blocks into the workspace. Place one||pins:servo write||
block into the||logic:if||
part and the other into the||logic:else||
part of the||logic:if then else||
statement. - The door should open when a loud sound is heard, so set one
||pins:servo write||
block to turn the servo to 180° (closing the door) and the other to 90° (opening the door). - Adding a
||loops:pause||
block will give the servo enough time to rotate when a sound is heard, so drag a||loops:pause||
block into the||logic:if||
part of the||logic:if then else||
statement as well. - Download the code to your Adafruit Circuit Playground Express and try clapping. You should see the lights changing colors and your door opening and closing.
forever(function () {
if (input.soundLevel() > 18) {
light.showRing(
"red red red red red red red red red red"
)
pins.A1.servoWrite(90)
pause(1000)
} else {
pins.A2.servoWrite(180)
light.showRing(
"green green green green green green green green green green"
)
}
})
|Step 4|
- Play a tone
For extra effect, you can play a tone when the loud sound is detected. In MUSIC the ||music:play sound||
block lets you to choose from a variety of premade tunes. Pick one and try it out! Place it in the ||logic:if||
part of the ||logic:if then else||
statement.
Download the code again to the Adafruit Circuit Playground Express to add some flare!
forever(function () {
if (input.soundLevel() > 18) {
light.showRing(
"red red red red red red red red red red"
)
pins.A1.servoWrite(90)
music.powerDown.playUntilDone()
pause(1000)
} else {
pins.A2.servoWrite(180)
light.showRing(
"green green green green green green green green green green"
)
}
})
Good work!
You’ve finished! Fill your box with confetti, tissue paper, or another light-weight material. Then, place it above a door or window and wait for your unsuspecting victim to enter. When they do, clap loudly and watch as confetti spills down onto them. PRANKED!