Supplementary Code for Projects

Introduction

On this page you'll find code examples that will be helpful for you that don't necessarily fit into examples from class

Shake

The CPX has what's called an accelerometer, to detect tilting, which can also be used to detect shaking and tapping

"""
SHAKE IT
by Benjamin Santiago

A little shake example. 
"""
from adafruit_circuitplayground import cp
import time

while True:
    # all you need to do is see if 
    # cp.shake() is True with if
    if cp.shake() is True:
        # this happens after a shake
        # note that you can't do stuff
        # while this loop happens
        cp.pixels.fill((255,0,255))
        time.sleep(0.3)

External Buttons (Digital in/out)

This example we use a button, it can be found on Adafruit here a clear LED arcade button from Adafruit

You should connect it to the board with alligator clips like the ones below: multicolored alligator clicps from Adafruit

We press the button and then can process that input in any way that we like, in this case, we make the lights turn on.

"""
In this example we'll use an external button to light up the 
neopixels on the CPX. This requires a little bit of code that
we haven't seen before, but once you set things up they 
function essentially the same. 
The button we're using is this one: 
https://www.adafruit.com/product/3491
It is "normally closed" which means it will read as False 
when it is pressed, and True when not pressed.
"""

# our imports
# ------------------------------------------------------
# note these first two, we need them for these examples
import board

# we need this for our button!
from digitalio import DigitalInOut, Direction, Pull
from adafruit_circuitplayground import cp
# ------------------------------------------------------

# ------------------------------------------------------
# variable for our button
# we select A5 for where we will connect it
arcade_button               = DigitalInOut(board.A5)
# input means we want it as a button 
# (as opposed to output from a light or speaker)
arcade_button.direction     = Direction.INPUT
# the button is "normally closed"
# as a result of this we just need to set Pull.UP
# rather than down.
# (this is an electronics things)
arcade_button.pull          = Pull.UP

# dim the neopixels a little
cp.pixels.brightness = 0.1
# ------------------------------------------------------

# MAIN LOOP
# ------------------------------------------------------
while True:
    # now simply test the button and turn the lights
    # on when pressed. 
    # note that we use "not" here because the button reads 
    # as "False" when pressed and "True" when released.
    if not arcade_button.value: 
        cp.pixels.fill((255,255,255))
    else:
        cp.pixels.fill((0,0,0))
# ------------------------------------------------------

Random

If you want to make a game where you simulate dice rollling or randomness in some way you could use the following code:

"""
RANDO EXAMPLE 
by Benjamin Santiago

light up random neopixels, to show
that random works. 
"""
# IMPORTS
import time
import random #<--- this one is important, for rando numbers
from adafruit_circuitplayground import cp

while True:
    # get a random number from 0 to 9
    # (for the neopixels)
    rando_num = random.randint(0, 9)

    # make that random neopixel glow
    # (assign the number to a single neopixel)
    for b in range(0,255, 8):
        cp.pixels[rando_num] = (b, b, b)
        time.sleep(0.002)
    for b in range(255, -1, -8):
        cp.pixels[rando_num] = (b, b, b)
        time.sleep(0.002)
    
    # reset the brightness in case we don't get 
    # exactly to 0 when descending
    cp.pixels[rando_num] = (0,0,0)

If you want to influence the randomness in some way, you might do something like the below code. This code works by making a big tuple (like the ones we use for colors), for the dice values (instead of 1, 2, 3, 4, 5, 6)

"""
NON-STANDARD DICE
(Benjamin Santiago)
use the cp.shake() function in order to 
"roll" a non-standard dice
(six possible random options that 
are not 1 - 6)
"""

# IMPORTZ
from adafruit_circuitplayground import cp
import random #<-- important!
import time
 
# construct the values of a non-standard dice 
# so that we don't get 1-6
non_STANDARD_dice = (2, 2, 4, 4, 6, 6)

# LOOP
# ---------------------------------------------------------
while True:
    # when we SHAKE, show the value by lighting up
    # a random amount of dice taken from the 
    # non-standard dice
    if cp.shake():
        # our ROLL --> random number 0,5
        # use this to select the value in the
        # tuple or list of values
        # (we will get 2, 4 or 6 in this case)
        rando = non_STANDARD_dice[random.randint(0,5)]
        
        # show the result of our roll
        # (for example 4, will make neopixels
        # 0 - 3 blue), and wait a little
        # so we see a sequence
        for p in range(rando):
            cp.pixels[p] = (0,255,255)
            time.sleep(0.1)
        
        # let the roller see the result
        time.sleep(.5)
        
        # let the glow go down 
        # (the neopixels turn white, 
        # I did that by accident and kinda
        # liked it so kept it)
        for p in range(rando-1, -1, -1):
            for b in range(255, -1, -4):
                cp.pixels[p] = (b,b,b)
            # make sure the neopixel is off
            # in case our math is off
            cp.pixels[p] = (0,0,0)
# ---------------------------------------------------------