Understanding the Code

Before moving forward, let's take a moment to look at the code for this project.

The code was created using Microsoft MakeCode for Adafruit, a web-based code editor. MakeCode 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.

Getting into Bootloader Mode

To make your board work with MakeCode we need to put it into "bootloader mode". All that's required to do this is to connect the board to your computer with a micro USB cable and click the small reset button in the center of the board. 

Click this link or the button below to enter the portal to interact with the code for this project.

How to Upload Code

To upload code, connect you Circuit Playground Express to your computer using the micro USB cable, click the Download button to download the .uf2 file to your computer, and drag 'n drop that .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!

Connect Servo

Once your code is uploaded, plug the alligator clip to pin wires into the servo connector such that you have three alligator clips with the wires going to the servo harness.

Connect your servo motor to your Circuit Playground Express using the alligator clips.

  • Connect the red middle wire from the servo to Vout on the CPX
  • Connect the black or brown wire from the servo to GND
  • Connect the yellow or white wire from the servo to A1

You should see your motor start rotating 180 degrees, with a pause, before rotating back again.

What This Code Does

This simple sketch tells your animatronic hand to look for a cool, dark place to hide. It does this by telling the motor to run until two conditions are both met.

In the forever block there is a while statement, instructing the Circuit Playground Express to run the servo unless:

  • The temperature goes below the specified threshold (in this case, 20 degrees Celsius)
  • The light level goes below the specified value (in this case, 100). 

You can test this code by turning off the lights and cooling down the thermistor on the board (I used a quick blast from a can of compressed air).

If you'd like to play with this code, click "Edit" and a new window will open in which you can create your own version.

Troubleshooting

Problem: My servo motor isn't responding!

Solution: Make sure that you have the servo's wires connected to the correct pads. The brown wire should go to GND, the yellow wire to A1, and the red wire to VOUT.

 

Problem: My Circuit Playground Express doesn't show up as CPLAYBOOT!

Solution: Your Circuit Playground Express board comes ready to work with CircuitPython, and will show up as a flash drive named CIRCUITPY the first time it's connected to your computer. To switch over to work with MakeCode, connect the board to your computer with a micro USB cable and click the small reset button in the center of the board.  

This guide was first published on Oct 11, 2018. It was last updated on Oct 11, 2018. This page (Understanding the Code) was last updated on Oct 11, 2018.