In addition to using MakeCode's on tilt and on shake blocks to trigger notes, you can map readings from the CPX's accelerometer to the frequency of the notes the board is playing. That way, your Musical Glove can play continuous notes that change as you move the board around.

The accelerometer measures the pull of gravity on the board in three directions: along the x axis (right to left), along the y axis (top to bottom), and along the z axis (up and down). Usually the measurements are given in terms of mgs (microgravity). But for this project, it's a little easier to measure rotation in degrees, like a circle.

To learn more about how an accelerometer works and how to program it in MakeCode, check out the Make It Shake, Rattle, and Roll Guide.

How to See What the Accelerometer is Reading

A quick way to see readings from the accelerometer (as well as many of the other sensors on the CPX) is to go to the LIGHT menu and grab a graph 0 block.

Then go the INPUT menu, find the oval for the reading you want to monitor, and drag it over the 0.

When you tilt the board back and forth, the pixels on the simulated board will light up in proportion to the value its reading. The more pixels, the higher the value.

You can also click on the Show Console Simulator button to open a graph and a list of numbers. The graph is marked to show the high and low points, and the numbers show you the readings in real time.

In this case, when you tilt the simulated board as far as it will go, you get high and low readings of around 90 and -90.

If you have the MakeCode app on your computer and plug in your CPX, you can also get live readings from your board!)

Code a Musical Glove That Measures Rotation

This code example plays a tone based on how many degrees the CPX is tilted to the right or left. When the board is flat and facing straight up, that reading is zero. Tilt it all the way to the right at the accelerometer will read -90 degrees. All the way to the left is 90. Depending on how far you want to twist your wrist beyond that, you can set the range from about 130 to 150 (and -130 to -150).

Write the Rotation Code

Open the INPUT menu and scroll down to the bottom to grab the rotation (°) pitch block. The "pitch" is the measurement side to side. "Roll" measures tilt from top to bottom.

Next, go to the MATH menu and grab the map 0 from low 0 high 1023 to low 0 high 4 block, all the way at the bottom.


Drag the rotation block over the first 0 until it snaps into place. Then type in numbers over the remaining spaces. This will "map" or translate the rotation range you want to read to the frequencies you want the CPX to play.

The first two numbers represent the low and high end of the rotation you want to measure, in terms of degrees. In this example, -130 was typed over the next 0, and 130 over the 1023.

The next two numbers are the low and high end of the notes you want to play, measured in hertz. Choose whatever notes sound good to you. For this example, 27 was typed over the third 0, and 1500 over the 4.

Next, put a play tone block inside a forever block and drag the map block you just created over the Middle C.

The final step is to add any additional blocks you like to adjust things like volume and tempo.

Add to Your Musical Glove Program

You can build on the rotation code by creating a phrase of two or more notes. If you start with the rotation value and multiply it by a whole number or a decimal (using blocks from the MATH menu), the intervals between the notes will stay the same as the string of notes get higher and lower. Musical intervals like a fifth (0.2) and a third (0.3) will sound good. Multiplying a note by one half (0.5) or doubling it will give you the same note, just an octave higher or lower.

To see how that works, click on the green button and listen to the simulation of a program using intervals.

You can try all kinds of combinations of input to create different sounds. (You can even use readings from other sensors, such as the light sensor.) Play around with them to make something that sounds good to you!

This guide was first published on Apr 01, 2019. It was last updated on Mar 08, 2024.

This page (More Musical Methods) was last updated on Mar 08, 2024.

Text editor powered by tinymce.