zero玩树莓派3 流水灯
铁打的营盘流水的灯,今天学习了LED流水灯,分享一下过程。一、教学目标
利用gpiozero库的 LED类和TrafficLights类制作流水灯效果
二、实验设备
1.树莓派 *1 https://www.dfrobot.com.cn/goods-1976.html
2.树莓派扩展板*1 https://www.dfrobot.com.cn/goods-2041.html
3.LED模块*3 https://www.dfrobot.com.cn/goods-72.html
LED模块连接扩展板的16-17-18号端口
三、代码实现
1.LED类(16-17-18端口,顺序执行)
from gpiozero import LED
from time import sleep
led1 = LED(16)
led2 = LED(17)
led3 = LED(18)
led1.off()
led2.off()
led3.off()
while True:
sleep(1)
led1.on()
sleep(1)
led1.off()
sleep(1)
led2.on()
sleep(1)
led2.off()
sleep(1)
led3.on()
sleep(1)
led3.off()
2.LED类(16-17-18端口,for循环执行,精简代码)
from gpiozero import LED
from time import sleep
ledn=
while True:
for i in ledn:
led=LED(i)
led.on()
sleep(1)
led.off()
sleep(1)
3.gpiozero库的TrafficLights类
from gpiozero import TrafficLights
from time import sleep
lights = TrafficLights(16, 17, 18)
lights.red.off()
lights.amber.off()
lights.green.off()
while True:
sleep(1)
lights.amber.off()
lights.red.on()
sleep(1)
lights.red.off()
lights.amber.on()
sleep(1)
lights.amber.off()
lights.green.on()
sleep(1)
lights.green.off()
四、运行视频
https://v.youku.com/v_show/id_XNDYyMzI2MjY3Ng==.html
很好的教程,感谢分享
页:
[1]