Introduction
'Neopixels' is a term used by the company Adafruit to describe
chainable RGB LEDs that come with a constant current driver. You can
control a decent number of neopixels using a single GPIO pin and they
tend to be wonderfully bright. Since we are running these from battery
power, rather than power from the micro:bit, we can afford to run them a
little brighter than we might normally do.
There are 12 neopixels on the Bit:Bot, 6 of them on each side. They
are helpfully numbered on the PCB from 0 to 11, no excuses for not
lighting the correct ones.
Programming
The following program demonstrates the basics of working with neopixels on the micro:bit.
from microbit import *
import neopixel
# Initialise neopixels
npix = neopixel.NeoPixel(pin13, 12)
# Define some colours
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
# Make pixel 0 red
npix[0] = red
# Make pixel 6 green
npix[6] = green
# Make last pixel blue
npix[-1] = blue
# Cause the colour changes to be shown
npix.show()
sleep(5000)
# Clear the neopixels
npix.clear()
The line that initialises the strip of pixels states the pin and the
number of neopixels in the chain. For the Bit:Bot, this is pin 13 and 12
neopixels. Once this has been done, the variable we used becomes a list
of tuples that can be accessed in the normal way. When we define
colours, we need to remember to call the show() method in order to see
them on the pixels. We can also clear all of the pixels with a single
statement.
If you want to turn off a single pixel, rather than all of them, set its colour to (0,0,0).
Challenges
There's a fair bit of code on this page and it only scratches the
surface of the lovely and varied effects you can make with Neopixels. It
is well worth experimenting and exploring this at length.
- An obvious starting point is in synchronising lights and movement.
Using the functions on this page, you can get different lights to
indicate the direction in which you are driving the motors. You could
light up a different number of lights on each fin depending on how
quicky you are driving the motor on that side. You could use colour to
indicate the direction of the motor too.
- You can fade in and out of colours by using a loop to change the
amount of red, green or blue in small steps. Work out how to write a
function that fades from no colour to full brightness red. Work out how
to fade back to nothing again. Then develop a function that fades from
no colour to any colour you choose. This is harder since you have to go
up/down in different amounts. Finally make a function that fades from
one colour to another. Now you have a robot car that can perform some
basic mood lamp functions.
- Set all of the pixels to a single colour, not one that gives full
brightness on any channel. Let's say you start with a light red. Now
make one pixel at a time in the strip show full brightness red. Muck
around with the intervals you use so that it looks like the colour is
running around the strip of lights.
- Work out how to make a rainbow pattern across all of the lights,
starting at one end of the colour spectrum and ending at the other. It's
quite tricky to work out a general rule for doing this. You might find
it easier to work out the 12 colours you need first and store them in a
list of tuples.
- Similar to the last challenge. Make a single pixel twinkle. Quickly
brighten one pixel in a brighter version of the same colour used on the
other pixels. Work out how to vary which pixel you twinkle and create a
reusable function.
- Using different shades of red, you can make a heartbest effect. This
looks just as good if you use a different colour. What you are looking
for is a range of shades of the same colour with the edges of the strip
being dimmer than the centre. Make the pixels glow brighter by differing
amounts depending on their location in the strip.
- When you have written a decent number of functions and code snippets
for the neopixels, it's worth collating them all into a single example
program or into a reference you make for yourself. As you explore more
of the functionality of the Bit:Bot, you will be able to pick and choose
from your list more easily when writing new programs.