友情提示:380元/半年,儿童学编程,就上码丁实验室。
使用硬件:micro:bit环形RGB灯扩展板
DFRobot :
Micro:bit Circular RGB Expansion board


使用编程软件:uPyCraft V1.0

01 导入库,完成初始化
from microbit import *
import neopixel
np = neopixel.NeoPixel(pin2, 24)
对应的MakeCode图形化编程:

02 下载更新一下固件(Firmware)先设计一个blink的效果,看看程序下载是否成功
from microbit import *
import neopixel
np = neopixel.NeoPixel(pin2, 24)
while True:
np[0] = (25,0,0)
np.show()
sleep(500)
np[0] = (0,0,0)
np.show()
sleep(500)
03 定义clear()函数
def clear():
for i in range(0,24):
np[i] = (0,0,0)
np.show()
对应的MakeCode图标:

04 定义全局变量br,及设置亮度函数setBrightness()函数
br=1.0
def setBrightness(n):
global br
br=n/255.0
对应的MakeCode图标:

05 定义函数All(r,g,b),全部24个LED同步点亮同一种颜色
def All(r,g,b):
r=int(br*r)
g=int(br*g)
b=int(br*b)
for i in range(0,24):
np[i] = (r,g,b)
np.show()
对应的MakeCode图标:

06 逐个点亮LED的函数Wipe(r,g,b,t)
def Wipe(r,g,b,t):
r=int(br*r)
g=int(br*g)
b=int(br*b)
for i in range(0,24):
np[i] = (r,g,b)
np.show()
sleep(t)
对应的MakeCode图形化编程:

主程序:
while True:
Wipe(100,0,0,50)
Wipe(0,100,0,50)
Wipe(0,0,100,50)
运行效果:
https://www.zhihu.com/video/1009521129869889536
07 “移动”着的LED灯的函数Dot(r,g,b,t)
def Dot(r,g,b,t):
r=int(br*r)
g=int(br*g)
b=int(br*b)
for i in range(0,24):
clear()
np[i] = (r,g,b)
np.show()
sleep(t)
对应的MakeCode图形化编程:

主程序:
while True:
Dot(100,0,0,50)
Dot(0,100,0,50)
Dot(0,0,100,50)
运行效果:
https://www.zhihu.com/video/1009521210295558144
08 彩虹色渐变函数rainbow(t)及其调用
from microbit import *
import neopixel
np = neopixel.NeoPixel(pin2, 24)
def rainbow(t):
r=100
g=0
b=0
for c in range(0,50):
g=g+2
for i in range(0,24):
np[i] = (r,g,b)
np.show()
sleep(t)
for c in range(0,50):
r=r-2
for i in range(0,24):
np[i] = (r,g,b)
np.show()
sleep(t)
for c in range(0,50):
b=b+2
for i in range(0,24):
np[i] = (r,g,b)
np.show()
sleep(t)
for c in range(0,50):
g=g-2
for i in range(0,24):
np[i] = (r,g,b)
np.show()
sleep(t)
for c in range(0,50):
r=r+2
for i in range(0,24):
np[i] = (r,g,b)
np.show()
sleep(t)
for c in range(0,50):
b=b-2
for i in range(0,24):
np[i] = (r,g,b)
np.show()
sleep(t)
while True:
rainbow(50)
对应的MakeCode图形化编程:

运行效果:
https://www.zhihu.com/video/1009521295171686400
始发于知乎专栏:牧之