︎Back to Interactive & Experience Design

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

CPX Warm up #5






(you may need to refresh to see the code)


Introduction


We're gonna be talking about functions in this warm-up!. You've already seen functions before time.sleep() cp.pixels.fill(), the word with parentheses is the function(technically these are also member of the "time" and "cp" objects, but these are not not functions)

What that function do?

Functions allow you to encapsulate functionality. That is to say, prevent you from having to re-type boring-ass code. This serves other functions like preventing you from having to deal with errors and creating code that is more "human-readable." As in a function named sleep() gives you some idea about what it does.

Hi is this function?

So below is some rather simple code we have seen a bunch, a for loop to blink our lights. It blinks the lights several different colors.︎︎︎

Let's imagine that ultimately we want to make it so we can have a function that says how many times we want our lights, and the color we want our lights to flash. To do that we need, as you can imagine, function. To create a function you need to use the word "def" this means you are "defining" the function. You put the function name (in the case below it is "light_flash"), parentheses, colon and then say what you want to do. After that to actually use it you have to call the function. To do this you just type the name of the function somewhere in your code. Let's take one of the blink loops and put it in our function definition.︎︎︎

Goin' ham...on params (idk, whatever)

So the thing you can do to functions to increase their...functionality, is to add what are called parameters. Parameters are things like the color for cp.pixels.fill(), or the amount of time in time.sleep(). To do tha we have to simply add them to our definition.︎︎︎

Our function above, assumes that we want to blink a light a certain color and then turn the light off. We'll also give the function something called blinks which says how many times we want to blink. This allows us to execute an example like that below︎︎︎

There are more things you can do with functions but we won't talk about them here for the sake of clarity, and basically they are just making functions more dynamic.

One more random thing

Last thing I want to introduce is randomness just so we can create a specific example. To use randomness we need to import random and then call a function called "random.randint" and give a range of two numbers.︎︎︎

Note in the above that we create a variable called "which" so that the random number doesn't change each time. We pick one random pixel, fade it out, and then move onto the next one. That's it!

Exercises

(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__WU5__E01.py” would be something I might name my files, but you’re not obligated to use this exact syntax.)
  1. Recreate the light_blink() function, and add a parameter that allows you to control the wait time in time.sleep()
  2. Create a function that will play a tone three times, and the frequency/pitch of the tone is defined by
    a parameter. Call the function for three different pitches.
  3. Create two different functions. One makes each pixel blink in sequence (any colors or direction), and one make all the pixels fade out. Call those two functions to show they work.
  4. Put the randomness example above into a function. Add the functions you created for the previous exercise (#3) and call each of them.
  5. Take the randomness example and convert it into a function such that the pixels are either "pink" (255,0,255), "yellow" (255,255,0) or "cyan" (0, 255, 255)

︎Back to Interactive & Experience Design