Microbit with NeoPixels
The BitBot comes with a set of 12 programmable NeoPixel LEDs built along the arms of the robot - 6 each side. Each one is individually addressable (0 .. 11) and programmable using standard RGB values.
NeoPixels
Importing the neopixel library is a required step because it doesn't come in with the traditional blank microbit star import. Then we can set colour using an rgb tuple.
from microbit import *
import neopixel
pixels = neopixel.NeoPixel(pin13, 12)
pixels[0] = (255, 255, 255)
pixels.show()
We can set all pixels to the same colour easily too.
colour = (125, 10, 22)
for i in range(0, len(pixels)):
pixels[i] = colour
pixels.show()
All changes to the pixel values have to be followed up with a call to show(). Pixel values also persist between calls to show() so that turning off all LEDs is a good practice before changing which LEDs are shown.
# turn off all LEDs
for i in range(0, len(pixels)):
pixels[i] = (0, 0, 0)
pixels.show()
Microbit LEDs
We can add this NeoPixel functionality to the BitBot vehicle so that we can have headlights, reverse lights, brake lights...
# import neopixel
class MicrobitLEDs:
def __init__(self, led_pin):
self.neopixels = neopixel.NeoPixel(led_pin, 12)
def all(self, pixels, colour):
for i in pixels:
self.neopixels[i] = colour
self.neopixels.show()
def clear(self):
self.all(range(0, 12), (0, 0, 0))
def headlights(self, colour = (255, 255, 255)):
self.clear()
self.all([5, 11], colour)
def reversing_lights(self, colour = (127, 127, 127)):
self.clear()
self.all([0, 6], colour)
def brake_lights(self, colour = (127, 0, 0)):
self.clear()
self.all([0, 6], colour)
def left_lights(self, colour):
self.clear()
self.all(range(0, 6), colour)
def right_lights(self, colour):
self.clear()
self.all(range(6, 12), colour)
def sweep_forward(self, colour, delay = 25):
for i in range(0, 6):
self.clear()
self.neopixels[i] = colour
self.neopixels[i + 6] = colour
self.neopixels.show()
sleep(delay)
def sweep_backward(self, colour, delay = 25):
for i in range(11, 5, -1):
self.clear()
self.neopixels[i] = colour
self.neopixels[i - 6] = colour
self.neopixels.show()
sleep(delay)
I also added functions to light the left and right arms of the vehicle independently and two cool effects - swiping a colour from the back LEDs all the way to the front one (and the reverse effect) just to show off.
BitBot
We can add this code into the [BitBot class]({ FIX % link _posts/2019-05-16-microbit-motoring.md %}) like this:
class BitBot:
def __init__(self):
# ...other config here...
self.leds = MicrobitLEDs(pin13)
def forward(self, percent = 25):
self.leds.headlights()
# ...motor control...
def reverse (self, percent = 25):
self.leds.reversing_lights()
# ...motor control...
def stop(self):
self.leds.brake_lights()
# ...motor control...
Rainbows
For reference, since the LEDs are all RGB programmable, we can set each one to a different colour of the rainbow.
Colour | RGB |
---|---|
Red | 255, 0, 0 |
Orange | 255, 127, 0 |
Yellow | 255, 255, 0 |
Green | 0, 255, 0 |
Blue | 0, 0, 255 |
Indigo | 75, 0, 130 |
Violet | 148, 90, 211 |