2018-10-2 18:47:09 [显示全部楼层]
5563浏览
查看: 5563|回复: 4

【掌控】mpython-6…三颗小灯的大舞台

[复制链接]
本帖最后由 rzyzzxw 于 2018-11-8 08:14 编辑

掌控板上有三颗板载ws2812灯珠,还没有玩到哎。今天拿来玩,主要学习for循环。


【学习小目标】
1、了解mPython掌控板载3颗ws2812。
2、编程控制,尝试渐变、流水、呼吸灯效。



【一起玩起来】
mPython掌控板载3颗ws2812灯珠,WS2812是一种集成了电流控制芯片的低功耗的RGB三色灯。采用特殊的单线通讯方式控制rgb灯颜色,使用简单。
1、点亮那指路的明灯控制mPython掌控上的3颗RGB, 首先导入mpython模块:from mpython import *
注解导入mpython模块后,会为掌控创建一个NeoPixel对象rgb,控制板载的RGB只需对rgb对象操作。
设置每个像素点颜色:rgb[0] = (255, 0, 0)  # 设置为红色,全亮度rgb[1] = (0, 128, 0)  # 设定为绿色,半亮度rgb[2] = (0, 0, 64)   # 设置为蓝色,四分之一亮度然后使用 write() 将颜色输出到RGB灯: rgb.write()


试试效果,亮起来了,成就感来了;P
【掌控】mpython-6…三颗小灯的大舞台图1

2、来一组灯效
【渐亮】
[mw_shl_code=python,true]from mpython import *
import time

while True:
    for i in range(0,255):
    #设置每个像素点颜色
        rgb[0] = (i,0,0)    # 设置红色
        rgb[1] = (0,i,0)  # 设定为绿色
        rgb[2] = (0,0,i)   # 设置为蓝色
        rgb.write()           #将颜色输出到RGB灯
        time.sleep_ms(10)
       [/mw_shl_code]

【渐暗】
[mw_shl_code=python,true]from mpython import *
import time

while True:
    for i in range(255,0,-1):
    #设置每个像素点颜色
        rgb[0] = (i,0,0)    # 设置为红色
        rgb[1] = (0,i,0)  # 设定为绿色
        rgb[2] = (0,0,i)   # 设置为蓝色
        rgb.write()           #将颜色输出到RGB灯
        time.sleep_ms(10)
       [/mw_shl_code]

【渐亮再渐暗】
[mw_shl_code=python,true]from mpython import *
import time

while True:
    for i in range(255,0,-1):
        #设置每个像素点颜色
        rgb[0] = (i,0,0)    # 设置为红色
        rgb[1] = (0,i,0)  # 设定为绿色
        rgb[2] = (0,0,i)   # 设置为蓝色
        rgb.write()           #将颜色输出到RGB灯
        time.sleep_ms(1)
    for i in range(0,255):
        #设置每个像素点颜色
        rgb[0] = (i,0,0)    # 设置为红色
        rgb[1] = (0,i,0)  # 设定为绿色
        rgb[2] = (0,0,i)   # 设置为蓝色
        rgb.write()           #将颜色输出到RGB灯
        time.sleep_ms(1)      [/mw_shl_code]
【同色呼吸】
[mw_shl_code=python,true]from mpython import *
import time

while True:
    for i in range(0, 256):
        rgb.fill((i, 0, 0))   # 三灯全红
        rgb.write()
        time.sleep_ms(3)
    for i in range(0, 256):
        rgb.fill((0, i, 0))   # 三灯全绿
        rgb.write()
        time.sleep_ms(3)
    for i in range(0, 256):
        rgb.fill((0, 0, i))   # 三灯全蓝
        rgb.write()
        time.sleep_ms(3)        [/mw_shl_code]

来一个效果:




【流水渐亮】
[mw_shl_code=python,true]from mpython import *
import time

while True:
    for i in range(0,255,5):
        #设置每个像素点颜色
        rgb[0] = (i,0,0)    # 设置红色
        rgb[1] = (0,0,0)  # 设定为绿色
        rgb[2] = (0,0,0)   # 设置为蓝色
        rgb.write()           #将颜色输出到RGB灯
        time.sleep_ms(100)   
        rgb[0] = (0,0,0)   
        rgb[1] = (0,i,0)   
        rgb[2] = (0,0,0)   
        rgb.write()           
        time.sleep_ms(100)
        rgb[0] = (0,0,0)   
        rgb[1] = (0,0,0)   
        rgb[2] = (0,0,i)   
        rgb.write()           
        time.sleep_ms(100)      [/mw_shl_code]



【RGB彩色呼吸氛围灯】
程序效果:RGB氛围灯红、绿、蓝三色混合渐变显示出多种颜色,呈现出彩色呼吸效果。
[mw_shl_code=python,true]
from mpython import *  #导入掌控库
import time   #引入时间库

while True:
    red = 0
    green = 0
    blue = 255
    for i in range(255):   
        rgb.fill((i, 0, 255 - i))        
        rgb.write()
        time.sleep_ms(1)
    for i in range(255):   
        rgb.fill((255 - i, i, 0))   
        rgb.write()
        time.sleep_ms(1)
    for i in range(255):   
        rgb.fill((0, 255 - i, i))   
        rgb.write()
        time.sleep_ms(1)[/mw_shl_code]


【全亮全灭】
这里用到了目前帮助文档里还没写上的函数。

[mw_shl_code=python,true]from mpython import *
import time

while True:
    rgb.fill((50,0,0))   # 三灯全红
    rgb.write()
    time.sleep(3)
    rgb.fill((0,0,0))    # 三灯全灭
    rgb.write()
    time.sleep(3)[/mw_shl_code]



上面程序也不够简练,灯效也一般。建议自定义函数,也可以整合到别的项目中,例如上一帖子的个性徽章。
好吧,各种灯效自己来玩吧。
欢迎来拍砖。

赤星三春牛!  初级技神

发表于 2022-2-8 10:47:32

这个不错
回复

使用道具 举报

赤星三春牛!  初级技神

发表于 2022-2-8 10:48:33

但是代码看不懂
回复

使用道具 举报

赤星三春牛!  初级技神

发表于 2022-2-8 10:49:34

可以做mind+的程序吗?
回复

使用道具 举报

赤星三春牛!  初级技神

发表于 2022-2-8 10:50:35

灯挺漂亮的。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4 备案 沪公网安备31011502402448

© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail