8. BitBotXL LEDs
8.1. NeoPixel module
import neopixel
.from microbit import *
import neopixel
8.2. Set up LEDs
- neopixel.NeoPixel(pin, n)
- Initialise a strip of RGB LEDs.
pin
is the pin that they are connected by.n
is the number of LEDs.
np = neopixel.NeoPixel(pin13, 12)
.from microbit import *
import neopixel
PIN_NUM = pin13
NUM_PIXELS = 12
np = neopixel.NeoPixel(PIN_NUM, NUM_PIXELS)
8.3. Set LED color and brightness
- np[n] = (red, green, blue)
Set the red, green and blue brightness from 0 to 255 for a RGB LED at position n.
np[0]
.(255, 255, 255)
, the color is white.from microbit import *
import neopixel
np = neopixel.NeoPixel(pin13, 12)
np[0] = (255, 255, 255)
np.show()
from microbit import *
import neopixel
np = neopixel.NeoPixel(pin13, 12)
np[0] = (255, 255, 255)
np[1] = (255, 0, 0)
np[2] = (0, 255, 0)
np[3] = (0, 0, 255)
np.show()
Tasks
Write code to set the LEDS at position 1, 2 and 3 to yellow, cyan and magenta.
Write code to set all the LEDS to yellow for 1 sec, then cyan for 1 sec, then magenta for 1 sec.
8.4. Show LEDs
show()
is used on the neopixel object that was set up. e.g. np.show()
- show()
Show the LEDs using their color settings. This must be called for any updates to the LEDs to become visible.
np.show()
from microbit import *
import neopixel
np = neopixel.NeoPixel(pin13, 12)
np[0] = (255, 255, 255)
np.show()
8.5. Clear LEDs
- clear()
Clear all the LEDs so that they have no colors set and turns off the LEDs.
buggy_lights
for the neopixel object.clear()
.from microbit import *
import neopixel
buggyLights = neopixel.NeoPixel(pin13, 12)
dull_blue = [20, 20, 25]
dull_red = [25, 0, 0]
for i in range(6):
buggyLights[i] = dull_blue
for i in range(6, 12):
buggyLights[i] = dull_red
buggyLights.show()
sleep(2000)
buggyLights.clear()
Tasks
Modify the code to turn on the left lights for 2 sec then turn on the right lights for 2 sec.
8.6. LED values
To read the color of a specific RGB LED use its index position.
- np[n]()
Return the red, green and blue value for the RGB LED at position n.
(255, 0, 0)
.for
loop displays each color value of the LED at position 0.from microbit import *
import neopixel
buggy_lights = neopixel.NeoPixel(pin13, 12)
buggy_lights[0] = (255, 0, 0)
for rgb_value in buggy_lights[0]:
display.scroll(rgb_value)
8.7. color lists
for color in color_list:
loops through the colors.for led_num in range(12):
loops through each LED to set its color.from microbit import *
import neopixel
buggy_lights = neopixel.NeoPixel(pin13, 12)
white = (255, 255, 255)
red = (255, 0, 0)
yellow = (255, 255, 0)
green = (0, 128, 0)
cyan = (0, 255, 255)
blue = (0, 0, 255)
magenta = (255, 0, 255)
color_list = [white, red, yellow, green, cyan, blue, magenta]
for color in color_list:
for led_num in range(12):
buggy_lights[led_num] = color
buggy_lights.show()
sleep(200)
For less bright lights use:
white = (20, 20, 20)
red = (20, 0, 0)
yellow = (20, 20, 0)
green = (0, 128, 0)
cyan = (0, 20, 20)
blue = (0, 0, 20)
magenta = (20, 0, 20)
8.8. Primary and secondary colors
Tasks
See https://www.indezine.com/products/powerpoint/learn/color/color-rgb.html
Modify the code to use a shorter list of colors, with just the primary colors.
Modify the code to use a shorter list of colors, with just the secondary colors.