robotbit扩展板4个rgb灯,r-红,g-绿,b-蓝,值为0~255,可模拟出256*256*256种颜色。
from microbit import * import neopixel
r = 0
g = 0
b = 0
np = neopixel.NeoPixel(pin16, 4)
for r in range(0,256,10):
for g in range(0,256,10):
for b in range(0,256,10):
np[0] = (r, g, b)
sleep(200)
np.show()
函数 neopixel.NeoPixel(PIN, NUM) 用来创建 neopixel 对象,它有两个参数,第一个是GPIO,第二个是彩灯的数量。
neopixel 对象是一个元组列表,每个列表项都是由 RGB 三种颜色组成的元组。RGB参数的范围是 0-255,三种颜色组合起来就有 256 x 256 x 256 = 1.67M种颜色。
颜色参数写入列表后并不能改变彩灯,还需要调用函数 show(),才会更新。如果要清除彩灯,可以调用函数 clear().
官方的例子,随机显示彩灯。
"""
neopixel_random.py Repeatedly displays random colours onto the LED strip.
This example requires a strip of 8 Neopixels (WS2812) connected to pin0. """
from microbit import *
import neopixel
from random import randint # Setup the Neopixel strip on pin0 with a length of 8 pixels
np = neopixel.NeoPixel(pin16, 4) while True:
#Iterate over each LED in the strip for pixel_id in range(0, len(np)):
red = randint(0, 60)
green = randint(0, 60)
blue = randint(0, 60) # Assign the current LED a random red, green and blue value between 0 and 60
np[pixel_id] = (red, green, blue) # Display the current pixel data on the Neopixel strip
np.show()
sleep(500)