Fall 2023 ︎︎︎ 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”︎︎︎
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.
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.
ALL CODE SHOULD HAVE COMMENTS
UPLOAD BY FRIDAY the 13th)
- Use the frequency note list linked above to make an arbitrary “melody” of your own creation. It should be at least 10 notes.
- Make the first example (with “Twinkle, Twinkle, Little Star”) play when you press either button A or B.
- Convert the “play file” example to be triggered by either shaking or tapping the CPX.
︎Back to Interactive & Experience Design