Program It in MakeCode

You're ready to program your dreidel with Microsoft MakeCode! If you're new to using MakeCode with the CPX, you can download the sample program below to get started. Look it over as you read through the description below to see how it was put together.

Then, when you get the hang of it, you can personalize the code to make a unique Mystery Dreidel.

To find out more, check out the Adafruit MakeCode Guide.

Breaking Down the Code

It may look like there's a lot of stuff on the screen, but don't let it scare you. There are only a few different things happening:

  • A stack of code runs as soon as you start the CPX up that sets the volume and the brightness of the lights, runs a light animation, and plays the Dreidel Song (the part that goes "Dreidel, dreidel, dreidel/I made it out of clay/And when it's dry and ready/Then dreidel I will play").
  • For each of the four directions the CPX can tilt -- up, down, left, or right -- there is a stack of code that tells the lights on that side to flash in a different color, and play a different pre-made sound.
  • A separate stack of code lets you play the Dreidel Song and a different light animation by turning the dreidel upside down (so the CPX is face down).
  • Two pieces of code let you turn the sound off (so just the light animations run) when the slide switch on the CPX is pushed to the left and turn it back on by pushing the switch to the right.

Here's a little more detail and a few helpful tips:

Adjust the Settings

To adjust the default settings on the CPX, grab a set volume block from the MUSIC menu and a set brightness block from the LIGHT Menu.

Mouse over the white ovals to open up a slider that lets you set them where you like.

Since the CPX is hidden inside the dreidel, you'll probably want to raise them both.

Create Your Own Light Animations

Designing your own lighting effects is easy. Just grab a show ring block from the LIGHT Menu, click on the color you want, and then click on the pixel you want to turn on (or grey to turn the light off). Stack the blocks to make the lights change. 

 

For the dreidel program, to flash the lights closest to the side facing up, turn on only the lights on that half of the CPX.

In other words, for the on tilt down block, the lights on the bottom of the CPX will flash blue, then white, then blue again. The clear block turns all the lights off so the CPX is ready for the next spin.

Add Comments

With so much going on, it's hard to see which stack of code goes with each section of the dreidel. Keep track by sticking a comment on your code.

 

Just right click on any stack of code and click "Add Comment" on the list that pulls down. Then type your notes in the pop-up box so you know what's what.

Write Strings of Musical Notes

You may have created your own short tunes by stacking up a few play tone at blocks. They are handy because mousing over the white oval opens up a keyboard with three octaves of notes (Low, Middle, and High).

However, there's a shortcut called a string that lets you write out notes by name. It's a lot quicker and takes up less space.

To create a string of notes, start with a regular play sound [  ] until done block from the MUSIC menu. You will replace the oval that has the pull-down menu of pre-made melodies with a blank oval you can type in.

To do that, first you need to click on the Advanced label and several additional categories of menus will appear. Then click on TEXT and grab an empty text oval. Drag it over the existing oval in the play sound [  ] until done block and it should click in. 

Then type in your notes. They are written in the format [name of note] : [number of beats]. The notes are in the Middle Octave unless you add "Low" or "High" in between. So a half-note in Middle C (two beats) is written C:2. And a High D whole note (four beats) is D:High:4.

Play Lights and Music at the Same Time

When you spin the dreidel and it lands on one side, you want the dreidel to light up and play music at the same time. If you just stack up the light and music blocks, the CPX will do one before it starts the next.

 

To tell the CPX to do both simultaneously, you can use a run in parallel block from the CONTROL menu. (Also found by clicking Advanced in the list of menus.)

 

In these examples, I've put the light blocks inside the "mouth" of the run in parallel block. Then it will flash the lights while also playing the music blocks that come after it.

Have Fun!

There are some advanced concepts used in this program, but you don't need to get fancy with MakeCode to create awesome effects. And you don't need to limit your ideas to the Mystery Dreidel. See what other spinning CPX ideas you can come up with!

This guide was first published on Dec 03, 2018. It was last updated on Dec 03, 2018. This page (Program It in MakeCode) was last updated on Nov 16, 2019.