Program it in MakeCode

Time to program your cardboard robot with Microsoft MakeCode for Adafruit!

MakeCode is a web-based code editor designed for beginners that is amazingly simple to learn. You can use it online -- there's no need to download anything. You can build programs quickly with drag-and-drop blocks, similar to Scratch. And best of all, it displays a simulated Circuit Playground Express so you can get an idea of how your program is working before you download your code to the real CPX board.

If you're new to MakeCode and CPX, you can learn more by checking out the Adafruit MakeCode Guide.

How the Inchworm Robot MakeCode Works

The MakeCode program for the Inchworm Robot gives you two ways to try it out:

  • When you press Button A, the CPX will show a light animation, signal the servo to swing back and forth five times, and then stop and clear (turn off) the lights.
  • A second stack of blocks programs the CPX's built-in microphone to listen for a loud sound. When that input is received, the code runs a different light animation, followed by the same servo sequence, and then clearing the lights.

Use Button A when you want to test the robot's movement, make adjustments, and then test it again at the touch of a button.

Then when you've got the Inchworm moving the way you want it to, use the sound trigger to turn the cardboard creature into a real robot that can sense, think, and act!

Download MakeCode to the CPX

To get started, connect the CPX to your computer with the micro USB cable. Your computer will treat the CPX the same way it does a flash drive, as an extra piece of hardware for storing files. It will show up as a drive called CLAYBOOT. Close any pop-ups that arise; you don't need to do anything with them.

 

To tell the CPX to get ready to receive code, you need to put it into bootloader mode. Press the tiny Reset button in the middle of the board until all the lights turn red for a second and then switch to green. You may have to press the button a few times the first time you use the CPX, or if it doesn't switch into bootloader mode the first time.

Now go to makecode.com/_9PfVyeYWMYz1 or click on the button below to see the MakeCode program. Click on the "Edit" link to open it up in MakeCode. (You can also click on the small "Download" link at the bottom of the MakeCode screen and load it immediately onto the CPX.)

When you open the program up, you will see the CPX simulator on the left, and the various categories of blocks you can code with listed in the middle. On the right is the code itself. In addition to the stacks of blocks for Button A and the sound sensor, there's a piece of code that runs as On Start, as soon as you turn on the CPX. It moves the servo arm to the 3 o'clock position, so the Inchworm is spread apart and ready to crawl.

When you are ready to download the program to your CPX, click the pink Download button on the MakeCode page. It will save a file named circuitplayground-Inchworm Robot.uf2 to the CLAYBOOT drive. If your computer automatically saves it to the Downloads folder or some other location, just find the file and drag and drop it (or copy and paste it) into the CLAYBOOT drive.

Running the Inchworm MakeCode Program

You can test your code while your CPX is still connected to the computer by pressing Button A or making a loud sound. Clapping or tapping the table near the Inchworm seems to work well.

To test how well it moves, unplug the USB cable and put the Inchworm on a surface with a little texture, so the teeth have something to grab. Turn the switch on the battery holder to On, and then press Button A or trigger the sound sensor (a loud clap or two works well).

If all goes well, your Inchworm will crawl along on command!

Troubleshooting Tips

Cardboard robots are not finely-tuned instruments. But learning to identify and fix problems is an excellent skill for robot-builders, so try some of these suggestions:

  • If your Inchworm is flexing but it's not getting anywhere, make sure the cuts in the feet are bent down enough to reach the crawling surface. Also try different surfaces, such as a tablecloth, carpet, or piece of felt.
  • Increase the weight on the front foot by taping some coins or batteries onto it.
  • Make sure the paperclip isn't hitting wires or other parts as it moves. You can tuck wires around the back of the Inchworm.

Playing With the Code

Taking a piece of code that works and messing around with it is a good way to learn how it works. Here are some ways you can change the program and start to get a feel for how MakeCode works:

  • On the blue Animation blocks, click on the small image to open a menu of other options. Or click on the Lights menu in the center of the MakeCode screen and build your own light show by adding a stack of Show Ring blocks. You can change colors by clicking on the palette of hues in the center and then on the light you want to change.
  • Make the Inchworm go farther by increase the number on the green Repeat block.
  • Change how far the servo arm swings by clicking on the number of degrees on the green Set Servo To blocks. You will see a slider showing the position of the arm.
  • Experiment with other inputs and outputs. For example, you can grab a pink On Light block from the center menu to control the robot using flashlights. Or add sound by telling the CPX to play a few notes before it starts to crawl.

Have fun! Building quick and simple robots is addictive, so look around to see what other ideas you can build with the CPX, MakeCode, and a little scrap cardboard.

This guide was first published on Oct 25, 2018. It was last updated on Oct 25, 2018. This page (Program it in MakeCode) was last updated on Apr 22, 2019.