Warm-Up no.05, (Funky) Functions and (shaky) shaking
Introduction
For this warm-up we'll talk about functions. Functions are basically a way to encapsulate and re-use your existing code. You've already encountered functions.
# these are some functions we've already encountered:
# fill() here is a function
cp.pixels.fill((255,255,255))
# sleep() below is a function
time.sleep(0.2)
That is to say, when you see text followed by parenthesis, like fill(), this is a function. These were, however, functions written by other people. What we're doing is called "calling" this function. What we'll learn now is how to create one of our own.
How to define a function
So previously to make all the neopixels light up in sequence, we'd create a for loop like this:
from adafruit_circuitplayground import cp
import time
while True:
for i in range(10):
cp.pixels[i] = (255,255,255)
time.sleep(0.1)
cp.pixels[i] = (0,0,0)
However, we've done this enough that we might want to be able to re-use it without having to write out the whole for loop. To do that we could make a function.
from adafruit_circuitplayground import cp
import time
# here we create our DEFINE our function
# (def is for "define" here)
# this doesn't do anything, it just
# makes a function that we can CALL later.
def neopixel_circle():
# this is the same code from before.
for i in range(10):
cp.pixels[i] = (255,255,255)
time.sleep(0.1)
cp.pixels[i] = (0,0,0)
#OUR MAIN LOOO O O O P!
while True:
neopixel_circle()
The above code allows us to create, or define a function called "neopixel_circle()" that we can then call later, in our main loop.
Why does this matter?
For this snippet of code, it might not seem super useful, however, what we can start to do is create, what are called parameters. This is like when call cp.pixels.fill() you have to give it a color (like (255,255,255)). Here we can add parameters for color and time, and then dynamically call our function later.
from adafruit_circuitplayground import cp
import time
# defining our function but with
# parameters for color and time
def neopixel_circle(color, time):
# this is the same code from before.
for i in range(10):
cp.pixels[i] = color
time.sleep(time)
cp.pixels[i] = (0,0,0)
#OUR MAIN LOOO O O O P!
while True:
# calling it with white, and .1 seconds
neopixel_circle((255,255,255), .1)
# now magenta, with .2
neopixel_circle((255,0,255), .2)
# now yellow and .4
neopixel_circle((255,255,0), .4)
compare the above code to what you would do without any functions:
from adafruit_circuitplayground import cp
import time
# the same code as the previous example but without
# any functions
while True:
for i in range(10):
cp.pixels[i] = (255,255,255)
time.sleep(.1)
cp.pixels[i] = (0,0,0)
for i in range(10):
cp.pixels[i] = (255,0,255)
time.sleep(.2)
cp.pixels[i] = (0,0,0)
for i in range(10):
cp.pixels[i] = (255,255,0)
time.sleep(.4)
cp.pixels[i] = (0,0,0)
Now consider, what would happen if you had to have 10 different colors, or 100, and how functions make your code easier to both parse, write, and modify. You could for example call the function with random numbers. See the supplementary code page for another example using randomness.
"""
This is an example showing how to call a
function with a random color for our function
"""
# IMPORTZZZ Z Z
from adafruit_circuitplayground import cp
import time
import random #<-- import for randomness
# same function as before
def neopixel_circle(color, time):
# this is the same code from before.
for i in range(10):
cp.pixels[i] = color
time.sleep(time)
cp.pixels[i] = (0,0,0)
# MAIN LOOP
#---------------------------------
while True:
#every time we go through the loop,
#this will give us a random color
#this color will remain the same
#until we set it again.
random_color = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
#call our function
neopixel_circle(random_color, 0.1)
#---------------------------------
Shakin' and Tappin'
The Circuit Playground has what is called an accelerometer. This allows us to detect tilting but can also be configured for things like tapping and shaking. They are pretty easy to set up
""" SHAKING EXAMPLE """
# imports
# -----------------------------------------
from adafruit_circuitplayground import cp
import time
import random
# -----------------------------------------
# MAIN LOOP
# -----------------------------------------
while True:
# this is all we need to detect shaking
if cp.shake():
# pick a random neopixel
rando_num = random.randint(0,9)
# "glow" the pixel
for b in range(255,4):
cp.pixels[rando_num] = (b,0,b)
time.sleep(0.01)
for b in range(255, -1, -4):
cp.pixels[rando_num] = (b,0,b)
time.sleep(0.01)
cp.pixels[rando_num] = (0,0,0)
# -----------------------------------------
# imports
# -----------------------------------------
from adafruit_circuitplayground import cp
import time
import random
# -----------------------------------------
current_pixel = -1
# MAIN LOOP
# -----------------------------------------
while True:
# this is all we need to detect tapping
if cp.tapped:
#our example here "counts" how many
#taps we've done, via the neopixels
#here we add one to the current neopixel
#if it is less than the total
#otherwise, set it back to 0
#and reset all the other neopixels
if current_pixel < 9:
# we haven't see exactly this before
# what this does is the same as
# saying "current_pixel = current_pixel + 1"
# this is such a common thing to do
# we have "+="
current_pixel += 1
else:
current_pixel = 0
cp.pixels.fill((0,0,0))
#light up the current neopixel
cp.pixels[current_pixel] = (255,255,0)
# -----------------------------------------