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
Then go the INPUT menu, find the oval for the reading you want to monitor, and drag it over the
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!)
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
Next, go to the MATH menu and grab the
The first two numbers represent the low and high end of the rotation you want to measure, in terms of degrees. In this example,
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,
Next, put a
The final step is to add any additional blocks you like to adjust things like volume and tempo.
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!