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:

1) 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. 

2) Click the Download button in the code window below to download the .UF2 file to your computer.

3) 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!

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:

  1. Be sure you have a Circuit Playground Express.  The Circuit Playground Classic will not work with MakeCode.
  2. If you get a drive named CIRCUITPY instead, press the reset button twice in succession to get to CPLAYBOOT.
  3. 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.  
  4. 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.

This guide was first published on Apr 11, 2019. It was last updated on Mar 26, 2024.

This page (MakeCode) was last updated on Mar 08, 2024.

Text editor powered by tinymce.