Warm-Up no.02, Lighting Things Up!

The skeleton

from adafruit_circuitplayground import cp
import time

while True:    

You might want to just save this in a separate file that you copy/paste from, or try to type it out from scratch such that you just memorize it. All it does is import the code we need specific to the CPX board, and the Python code we need to do stuff with time.

“while True:” simply gives us our loop so that the code continues executing (try the next example without this to see what happens). Everything indented after it is part of our loop.

Note that in Mu, if you don’t do anything with “import.time” you will receive a warning that you have no time related code. This is fine, Mu is just trying to help you optimize your code.

Light it up

http://youtube.com/watch?v=lDdX6meXcY4

Now we want to do stuff. The most common thing to do is light up lights. The lights used on this board are proprietary RGB LED’s called neopixels. All this means is that they are made by Lady Ada/Adafruit and they can light up (LED ︎︎︎ Light Emitting Diode) and there are three small LED’s within the structure to create the impression of being able to emit multiple colors.

Here's how to light up all the neopixels

from adafruit_circuitplayground import cp
import time

while True:
	cp.pixels.fill((255,255,255))

All we’re adding is one line: cp.pixels.fill((255,255,255)). You should remember seeing this before. If you’re wondering the terminology around this stuff, when we have a bunch of periods like this we are accessing an object. The subsequent periods are members of that object which may be other objects, functions or variables.

  • “cp” means we want to do something with the Circuit Playground that the import statement allows us to acess to.
  • “pixels” means we want to do something with the neopixels; in this case all of them.
  • “fill()” is a function that allows us assign a color to all the neopixels; ie “fill” them with a color.
  • “(255, 255, 255)” is the color that we want to fill the neopixels with, we need three numbers in a range from 0 - 255. (0, 0, 0) will be the same as turning them off, and the values go in order of R, G, B. When values have parentheses around them like this; it is called a tuple. This is not disssimlar to a list or an array you may have seen in other languages (which also exist in Python). It just allows us to have one variable with multiple values for R, G, and B separately.

separate pixels

from adafruit_circuitplayground import cp
import time

while True:
  cp.pixels[0] = (255, 255, 255)

For whatever reason, when you light up a neopixel individually you need to use this syntax. “pixels” is an array of 10 neopixels which we can access individually with the brackets. This is how arrays and tuples are accessed in many programming languages. Note that the first neopixel is 0 rather than 1. So we go from 0 through 9. Setting that equal to a tuple with a color, we are able to light up an individual neopixel. This is a concept called zero indexing

using time

from adafruit_circuitplayground import cp
import time

while True:
  cp.pixels.fill((255,255,255))
  time.sleep(0.2)
  cp.pixels.fill((0,0,0))
  time.sleep(0.2)