5. MOVEMotor LEDs
5.1. NeoPixel module
import neopixel.from microbit import *
import neopixel
5.2. Set up LEDs
- neopixel.NeoPixel(pin, n)
- Initialise a strip of RGB LEDs.
pinis the pin that they are connected by.nis the number of LEDs.
np = neopixel.NeoPixel(pin8, 4).from microbit import *
import neopixel
np = neopixel.NeoPixel(pin8, 4)
5.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(pin8, 4)
np[0] = (255, 255, 255)
np.show()
from microbit import *
import neopixel
np = neopixel.NeoPixel(pin8, 4)
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 last LEDS at position 1, 2 and 3 to yellow, cyan and magenta.
5.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(pin8, 4)
np[0] = (255, 255, 255)
np.show()
5.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(pin8, 4)
dull_blue = [20, 20, 25]
dull_red = [25, 0, 0]
buggyLights[0] = dull_blue
buggyLights[1] = dull_blue
buggyLights[2] = dull_red
buggyLights[3] = dull_red
buggyLights.show()
sleep(2000)
buggyLights.clear()
Tasks
Modify the code to turn on the front lights for 2 sec then turn on the rear lights for 2 sec.
5.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(pin8, 4)
buggy_lights[0] = (255, 0, 0)
for rgb_value in buggy_lights[0]:
display.scroll(rgb_value)
5.7. color lists
for color in color_list: loops through the colors.for led_num in range(4): loops through each LED to set its color.from microbit import *
import neopixel
buggy_lights = neopixel.NeoPixel(pin8, 4)
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(4):
buggy_lights[led_num] = color
buggy_lights.show()
sleep(200)
5.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.