Drama Piñata
2020-03-10 | By Adafruit Industries
License: See Original Project Programmers Circuit Playground
Courtesy of Adafruit
Guide by: Dano Wall
Overview
Piñatas are tragic, beautiful creatures, used once in a carnival of destruction, then tossed out and forgotten. This guide demonstrates how to modify a piñata so that it can be filled and refilled with candy over and over again.
This piñata also has an attitude, and can be programmed to respond, when hit, with sound effects or pre-recorded taunts.
A servo motor is hidden inside the body of the piñata, allowing a trap door in the bottom to be mechanically opened. A Circuit Playground Express sits on its back, allowing the use of either MakeCode or CircuitPython to program and customize the behavior.
The Parts
- Circuit Playground Express
- Micro servo
- Small Alligator Clip to Male Jumper Wire Bundle - 6 Pieces
- Lithium Ion Polymer Battery Ideal For Feathers - 3.7V 400mAh
Want to give your electronics a layer of protection? This enclosure case is a great way to protect your Circuit Playground Express from a swinging bat.
The Piñata
Most any piñata can be modified to work with the electronics listed above, however the exact piñata used for this project can be found on Amazon.
Other materials you will need:
- A paperclip
- Double sided foam tape
- Clear masking tape
Circuit Diagram
Connect your servo motor to the Circuit Playground Express by connecting the servo's red (or orange) wire to Vout, the yellow wire to A1, and the brown (or black) wire to GND.
Power Considerations
If a AAA battery pack is too bulky for your piñata, this small 400mAh lipo battery is an excellent power source for small projects like this.
Modify Piñata
This piñata is a modified version of the Fortnite Llama Loot Piñata.
Start by carefully opening the base of the piñata, gently removing the feet as you go (if you or someone you know likes Fortnite, save what's inside for them).
Once the piñata has been emptied, fold the flaps over and tape them down. This will help stiffen the cardboard and help it function as a trap door.
On the servo, use a small phillips screwdriver to attach a single arm servo horn (included) to the servo. Be sure to use the short screw as longer screws may damage the motor.
Next, find a paperclip and straighten it out.
Use pliers to bend a small hook and insert this through the last hole in the servo arm.
Gauge where you'd like the servo motor to be inside the piñata, rotating the arm to its start point to make sure it will be able to fully close the door.
Use a square of double sided foam tape to stick the servo motor inside piñata.
Poke the other end of the paper clip through the cardboard flap, bending it at a 90° angle to hold it in place.
Use small pieces of double sided tape to stick the Circuit Playground Express and battery to the back of the piñata.
Gather the cables together with a twist tie to keep them tidy.
Your modified piñata is now ready to rock!
MakeCode
Getting Familiar
Microsoft MakeCode for Adafruit is a web-based code editor that provides a block editor, similar to Scratch or Code.org, and also a JavaScript editor for more advanced users.
If you'd like to learn more about MakeCode, this guide is a good place to start.
How to Upload Code
To upload code, follow these instructions:
- Connect your Circuit Playground Express to your computer using a micro USB cable and press the small reset button in the center of the board. All the LEDs will flash red briefly, then turn green. Your computer should now show a removable drive called CPLAYBOOT.
- Click the Download button in the code window below to download the .UF2 file to your computer.
- Now drag and drop the .UF2 file onto the CPLAYBOOT drive in your computer's file explorer or finder.
The drive will automatically eject itself (your computer may give you a "failed to eject drive correctly" error, you can ignore this). The code is now on your Circuit Playground Express and ready to run!
input.onGesture(Gesture.ThreeG, function () {
hit_count += 1
music.playSound(music.sounds(Sounds.BaDing))
light.setAll(0xffffff)
pause(200)
light.clear()
})
input.buttonA.onEvent(ButtonEvent.Click, function () {
pins.A1.servoWrite(180)
})
input.buttonB.onEvent(ButtonEvent.Click, function () {
pins.A1.servoWrite(0)
})
let hit_count = 0
music.setVolume(255)
pins.A1.servoWrite(0)
forever(function () {
if (hit_count > Math.randomRange(3, 10)) {
pins.A1.servoWrite(180)
music.playSoundUntilDone(music.sounds(Sounds.MagicWand))
pause(2000)
pause(2000)
control.reset()
}
})
What is this code doing?
First, in the on_start loop, we tell Circuit Playground Express to move the servo motor connected to pin A1 to 0°, i.e. the start of its range. This guarantees that the trap door always starts in a closed position.
Whenever the piñata is hit, the hit_count increases by 1. This is accomplished using the on 3g function, which is triggered any time a force equal to three times earth gravity is detected by the accelerometer on-board the Circuit Playground Express.
In the forever loop, we tell the servo to wait until the hit_count rises above some randomly chosen number - between, in the default case, 3 to 10. Once triggered, the servo moves through its full 180° rotation, plays a sound effect, then waits two instances of pause 2000ms (aka 4 seconds) before resetting itself.
It's easy to change the range in the pick random bubble to make the game more or less certain.
Refilling the Piñata
Below the rest of the code, you will notice two blocks, one adding a function to button A, and one to button B.
These buttons will allow you to easily open and close the door using the servo motor, important for reloading the piñata with candy.
Troubleshooting
Problem: My motor doesn't move!
Solution: Make sure that your Circuit Playground Express is connected to a power source (anywhere between 3V-5V is safe). If you're using a AAAx3 battery pack, check that the switch on the battery pack is set to "ON". If you are using the LiPo battery, be sure it is fully charged using the charger in the Featured Products.
Problem: My Circuit Playground Express doesn't show up as CPLAYBOOT
Solutions:
- Be sure you have a Circuit Playground Express. The Circuit Playground Classic will not work with MakeCode.
- If you get a drive named CIRCUITPY instead, press the reset button twice in succession to get to CPLAYBOOT.
- Some USB cables are "charge-only" and won't pass data. Try using a different USB cable and try using a different USB port on your computer.
- With the Circuit Playground Express plugged into your computer with a micro USB cable, click the small reset button in the center of the board. The lights should all turn green. If they don't, try double-clicking the reset button.
If things still aren't working, head over to the Intro to Circuit Playground Express guide for more suggestions.
More MakeCode!
If you enjoy MakeCode and want to continue exploring you can check out lots more MakeCode projects on the Adafruit Learn System.
Want to Get Back to CircuitPython?
If you ever need to convert your Circuit Playground Express back to CircuitPython mode, you can do so by downloading the appropriate .UF2 file from CircuitPython.org and dragging it over onto your CPLAYBOOT drive. See this guide page for the step-by-step instructions.
CircuitPython Code
Getting to know CircuitPython
CircuitPython is a programming language based on Python, one of the fastest growing programming languages in the world. It is specifically designed to simplify experimenting and learning to code on low-cost microcontroller boards.
CircuitPython is easiest to use within the Mu Editor. If you haven't previously used Mu, this guide will get you started.
Why Use CircuitPython?
CircuitPython allows for a certain level of flexibility that cannot be achieved in MakeCode, so for more experienced users it can be a natural next step after MakeCode.
The example code here allows you to include custom audio files, one of which will play when the piñata is hit, and another that plays when the piñata dispenses candy.
Detailed instructions on how to create your own audio files can be found here.
Preparing your Board
To get your Circuit Playground Express set up to run this code, follow these steps:
- Install the latest CircuitPython for CPX from CircuitPython.org. For this guide, please use version 4.0.0 or greater. Version 3.1.2 is not as efficient on memory and may give an error.
- Get the latest 4.0 library pack, unzip it, and drag the library folder adafruit_motor over into the /lib folder on CIRCUITPY. If there is no lib directory, create one to put the file into. (More info on installing libraries, read information here.)
Find the adafruit_motor library in the library pack
Drag to the lib folder in the CIRCUITPY drive
Uploading
Make sure you've connected the Circuit Playground Express to your computer (mac/PC/Linux) via a known good USB A to micro-B cable. Your board should show up as a flash disk drive named CIRCUITPY (If you see a disk name CPLAYBOOT, try pressing the reset button again. If the only drive name you get is named CPLAYBOOT, CircuitPython may not be loaded on the board. You can load CircuitPython as per this guide).
Once your board is connected, copy code.py from the window below and paste it into Mu. Press the Save button and your code should automatically be saved to the CIRCUITPY disk drive (which appears when the Circuit Playground Express is plugged into your computer) as code.py.
Download: Project Zip or code.py | View on Github
# Circuit Playground Express Piñata by Dano Wall for Adafruit Industries
# CircuitPython code by Mike Barela for Adafruit Industries, MIT License
import time
import random
import board
import pulseio
from adafruit_motor import servo
from adafruit_circuitplayground.express import cpx
# create a PWMOut object on CPX Pin A1
pwm = pulseio.PWMOut(board.A1, frequency=50)
# Create a servo object cpx_servo
cpx_servo = servo.Servo(pwm)
hits = 0
max_hits = random.randint(3, 10)
cpx.detect_taps = 1 # Detect single taps
cpx_servo.angle = 0
cpx.pixels.fill((0, 0, 0)) # All NeoPixels off
while hits < max_hits:
if cpx.tapped:
print("Hit!")
hits += 1
cpx.pixels.fill((255, 255, 255)) # All White
cpx.play_file("hit.wav")
time.sleep(1.0) # Wait time in seconds
cpx.pixels.fill((0, 0, 0)) # All off
time.sleep(0.1)
# Hits Reached, Payout!
print("Release!")
cpx.pixels.fill((0, 255, 0)) # All green
cpx.play_file("candy.wav")
cpx_servo.angle = 180
print("Press Reset or power cycle to reset device")
while True:
pass # Infinite loop, press Reset button to reset
Make sure the file saved to CIRCUITPY is named "code.py", this will allow it to run automatically when your CPX is powered on.
Piñata Sounds
Click on the green buttons below to upload the two sound files used in this project:
The sound clips used in this project have been trimmed and edited from the following free audio files:
If you'd like to change the sounds your piñata makes, more audio clips can be downloaded from sites like http://soundbible.com/ or https://freesound.org/.
You will need to convert the files to the appropriate format for microcontroller use. See this guide on how to do that.
When you have a file converted, you can name it either hit.wav or candy.wav and drag it onto your CIRCUITPY drive to have it play when the piñata is hit or to announce the dispensing of candy.
Directory View
When you have all the files on your CIRCUITPY drive, you should have the files shown below. The lib folder is in the main directory, the adafruit_motor folder in the lib directory and the py/mpy files in the adafruit_motor directory.
Troubleshooting
Problem: I don't hear any audio!
Solution: If you're creating your own audio files, make sure they are all exported as 16-bit PCM WAV at 22,050 Hz and they are should be Mono.
Problem: My Circuit Playground Express isn't recognized by Mu!
Solution: Make sure your board is set up with CircuitPython, which has the Circuit Playground Express show up as a flash drive named CIRCUITPY when you connect the CPX to your computer. If it is showing up as CPLAYBOOT on your computer, you can follow the steps in this guide to ensure CircuitPython is loaded and you see the CIRCUITPY drive.
More CircuitPython Projects!
If you enjoy programming in CircuitPython and want to continue learning you can check out more CircuitPython projects on the Adafruit Learn System.
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum