︎Back to Interactive & Experience Design

Fall 2022 ︎︎︎ SUNY Purchase ︎︎︎ (DES3090) Interactive & Experience Design

CPX Warm up #4

(you may need to refresh to see the code)


So we’ve been doing a lot of stuff with lights and there are as you can see below, a lot of other sensors on the CPX. 

Sounding it out

We’ll first play  waith the speaker on the CPX. You can do two things, play “tones” and .wav files. You simply need either the function cp.play_tone(), or cp.play_file(). The tone function simply needs a frequency and a duration (similar to time.sleep()). The file function needs a .wav file on your CIRCUITPY drive which you need to name. Obviously, you have to be wary of how big a file you can play as there is limited space on the drive. 

You’ll notice that the CPX is quite soft. If you want to play things louder you could hook it up to an aux cord like so. 

I used this chart of frequencies to notes︎ to make the notes work. Note that you have to have some understanding of music theory to do the below, this is not important for the homework.

Use tones to play “Twinkle, Twinkle, Little Star”︎︎︎

Play file︎︎︎


The CPX also has what is called an “accelerometer” similar to what you might find in a Nintendo Wii controller. Basically this is a sensor that measures small changes in the sensor in relation to Earth’s gravity. You don’t really need to know anything about how this works except that you can take advantage of it to do several things. 

The easiest is to detect a “shake” or a “tap” with if statements like below. You can use the “detect taps” parameter as either 1 or 2 to detect double or single taps.


Tap ︎︎︎

The more complex thing you can do is take advantage of the full range of tilt sensing. We can test in the x, y and z dimensions. If you remember, previously, we’ve used the accelerometer’s x and y parameters to control the mouse. Below that is a simple example where we use the tilt to control the lights.

Tilt controlling the mouse︎︎︎

Note that there is a little math here. The accelerometer will give us values from around -10 to 10 in each dimension. This isn’t of value to us if we are working with the lights as they go from 0 - 255. “math.fabs()” allows us to get the absolute value. That is, the value will never be negative (ie -2 will simply become 2). “f” in this case is for “floating point” meaning we can put in a number with a decimal point. We multiply this by 25 so that we can get a fuller range closer to 255.


(make sure that you upload each code example to the respective area in the spreadsheet. Create a separate .py for each exercise. Please make the files are named clearly, they need at least, your name, the week/warmup #, and the exercise number so something like “SantiagoBenjamin__WU4__E01.py” would be something I might name my files, but you’re not obligated to use this exact syntax.)
  1. Use the frequency note list linked above to make an arbitrary “melody” of your own creation.
  2. Make the first example (with “Twinkle, Twinkle, Little Star”) play when you press either button A or B.
  3. Convert the “play file” example to use either button A or B to trigger sound playback.
  4. Convert the “play file” example to be triggered by either shaking or tapping the CPX.
  5. Convert the “tap” example to switch between two colors of your choice when you tap the CPX (ie the first tap will show blue pixels, the second tap will show pink pixels, the third tap will show blue pixels, and the fourth will show pink pixels, etc.)
  6. Edit the “tilt” example such that, rather than showing a multitude of colors it switches from 0 to full white when titling in the “x” dimension.

︎Back to Interactive & Experience Design