What is MakeCode?

MakeCode is a web-based code editor for physical computing made by Microsoft.

What does that mean for you? It means you can program your Circuit Playground Express to do almost anything you can dream up right from a website! You can code with blocks similar to the language Scratch, or you can do more advanced coding with Javascript. We'll be sticking to the block-based programming for this project.

More on MakeCode for Adafruit boards here.

Create a New Project with MakeCode

Head over to https://makecode.adafruit.com/ and create a new project.

Click the button below to access the code for this project.

What's going on the code above?

  • On start, set the volume of the CPX speaker, set the variable play to 0, then set all the NeoPixels to red.
  • In a forever loop, we check to see if the variable play is set to 1. If it is, call the song function to play the tune "Auld Lang Syne" then set play to 0. As it's a forever loop, this code block loops forever regardless of other events that happen in the code!
  • An abbreviated version of the song The "Auld Lang Syne" can be separated into 2 parts. The tune starts with part 1, then goes to part 2, before repeating again. Creating functions for these song parts then "calling" the functions later allows us to have much cleaner looking and more organized code. It also allows us to reduce the amount of code needed for the project making the program more efficient (more on that in the next code block).
  • When the CPX is shaken, we call the function countingdown, set the variable play to 1, turn the NeoPixels rainbow, turn the servo clockwise for 3 seconds, pause the servo for 15 seconds, turn the servo counter-clockwise for 3 seconds then turn the NeoPixels back to all red.
  • In the countingdown function, we want to count the NeoPixels down one at a time, per second, from all red to all white. To do this with out making the code clunky, we can use something called a while loop. First, the NeoPixels are set to all red, then the variable countdown is set to 9. In the while loop, as long as the variable countdown is greater than or equal to 0, each concurrent NeoPixel will change from red to white, every second. In each iteration of the loop, countdown is decremented by 1 until it reaches -1 at which point the program will exit the function and continue with the rest of the code to play the song and turn the servo.
  • In the on shake code block, the piece of code that says "Servo write pin A1 to (value between 0 and 180)" determines how fast or slow and what direction the servo spins. Values between 0 and 90 spin the servo clock wire and 90 - 180 is counter clockwise. The closer the values are to 90, the slower the servo will spin in either direction.

Uploading the Code

Now that we have the code for the project, we need to upload it to the CPX.

Let's name our file and download it.

  • Choose a name at the bottom of the page.
  • Then click the pink Download button.

Follow the directions and connect your CPX to your computer via the usb cable. Click the CPX Reset button once to go into programming mode (all NeoPixels will turn green).

In some cases, you may need to press the reset button twice to get into programming mode.

Next, look for the file in your downloads folder and drag it onto your CPLAYBOOT drive that should have showed up in your file manager/finder when you plugged in your board and entered programming mode.

You should now see the CPLAYBOOT drive disappear.

The code should now be running and the NeoPixels should be all red. Before we test it out let's wire up the servo! 

This guide was first published on Dec 25, 2018. It was last updated on Dec 25, 2018.

This page (Program with MakeCode) was last updated on Dec 19, 2018.

Text editor powered by tinymce.