ESP32(MicroPython) WS2812 RGB流水灯渐变版

ESP32(MicroPython) RGB流水灯渐变版

本程序实现了渐变颜色的RGB流水灯,速度和亮度可调,分对称和环形两种模式。

主要实现方法是先设定一个可调的基本亮度,渐变开始前第一种颜色亮度为基本亮度的8倍,第二种颜色亮度为0,之后每次渐变第一种颜色的亮度减去基本亮度,第二种颜色的亮度加上基本亮度,经过8个灯后完成渐变。在对称模式下每次加减的亮度为基本亮度的2倍,由于取值不同,对称模式需要增加2个变量记录起始位置而非复用环形模式的变量。

代码如下

#导入Pin模块
from machine import Pin
import time
from machine import PWM
from neopixel import NeoPixel
 
# 五向导航按键,COM引脚接3.3V
key1 = Pin(21, Pin.IN, Pin.PULL_DOWN)
key2 = Pin(19, Pin.IN, Pin.PULL_DOWN)
key3 = Pin(18, Pin.IN, Pin.PULL_DOWN)
key4 = Pin(5, Pin.IN, Pin.PULL_DOWN)
key5 = Pin(17, Pin.IN, Pin.PULL_DOWN)
key6 = Pin(16, Pin.IN, Pin.PULL_DOWN)
 
pin=13
rgb_num=24
rgb_led=NeoPixel(Pin(pin,Pin.OUT),rgb_num)

key_en=1
#按键扫描函数
def key_scan():
    global key_en
    if key_en==1 and (key1.value()==1 or key2.value()==1 or key3.value()==1 or key4.value()==1 or
                      key5.value()==1 or key6.value()==1  ):
        time.sleep_ms(10)
        key_en=0
        if key1.value()==1:
            return 1
        elif key2.value()==1:
            return 2
        elif key3.value()==1:
            return 3
        elif key4.value()==1:
            return 4
        elif key5.value()==1:
            return 5
        elif key6.value()==1:
            return 6
    elif (key1.value()==0 and key2.value()==0 and key3.value()==0 and key4.value()==0 and
          key5.value()==0 and key6.value()==0  ) :
        key_en=1
    return 0

brightness=6
delay=40
mode=1
def key_get(): #获取键值并改变变量的值
    global brightness
    global delay
    global mode
    key=key_scan()
    if key==1 and brightness<30 :
        brightness+=3
    elif key==2 and brightness>3 :
        brightness-=3
    elif key==3 and delay<90 :
        delay+=10
    elif key==4 and delay>10 :
        delay-=10
    elif key==5 and mode<2 :
        mode+=1
    elif key==6 and mode>0 :
        mode-=1     

count=0
count1=0
count2=23
#程序入口
while True:
    key_get()
    if count==23 :
        count=0
    if mode==0 : #关灯
        for i in range(rgb_num):
            rgb_led[i]=(0, 0, 0)
            rgb_led.write()
    if mode==1 : #非对称
        temp=0
        i=count
        count+=1
        r=8*brightness #红色渐变到绿色
        g=0
        while temp<8 :
            r-=brightness
            g+=brightness
            if i>23 :
                i-=24
            rgb_led[i]=(r, g, 0)
            i+=1
            temp+=1
        temp=0
        g=8*brightness #绿色渐变到蓝色
        b=0
        while temp<8 :
            g-=brightness
            b+=brightness
            if i>23 :
                i-=24
            rgb_led[i]=(0, g, b)
            i+=1
            temp+=1
        temp=0
        b=8*brightness #蓝色渐变到红色
        r=0
        while temp<8 :
            b-=brightness
            r+=brightness
            if i>23 :
                i-=24
            rgb_led[i]=(r, 0, b)
            i+=1
            temp+=1
        rgb_led.write()    
        time.sleep_ms(delay)
    if count1==12 :
        count1=0
    if count2==11 :
        count2=23     
    if mode==2 : #对称
        temp=0  #前半部分
        i=count1
        count1+=1
        r=8*brightness #红色渐变到绿色
        g=0
        while temp<4 :
            r-=2*brightness
            g+=2*brightness
            if i>11 :
                i-=12
            rgb_led[i]=(r, g, 0)
            i+=1
            temp+=1
        temp=0
        g=8*brightness #绿色渐变到蓝色
        b=0
        while temp<4 :
            g-=2*brightness
            b+=2*brightness
            if i>11 :
                i-=12
            rgb_led[i]=(0, g, b)
            i+=1
            temp+=1
        temp=0
        b=8*brightness #蓝色渐变到红色
        r=0
        while temp<4 :
            b-=2*brightness
            r+=2*brightness
            if i>11 :
                i-=12
            rgb_led[i]=(r, 0, b)
            i+=1
            temp+=1
        temp=0  #后半部分
        i=count2
        count2-=1
        r=8*brightness #红色渐变到绿色
        g=0
        while temp<4 :
            r-=2*brightness
            g+=2*brightness
            if i<12 :
                i+=12
            rgb_led[i]=(r, g, 0)
            i-=1
            temp+=1
        temp=0
        g=8*brightness #绿色渐变到蓝色
        b=0
        while temp<4 :
            g-=2*brightness
            b+=2*brightness
            if i<12 :
                i+=12
            rgb_led[i]=(0, g, b)
            i-=1
            temp+=1
        temp=0
        b=8*brightness #蓝色渐变到红色
        r=0
        while temp<4 :
            b-=2*brightness
            r+=2*brightness
            if i<12 :
                i+=12
            rgb_led[i]=(r, 0, b)
            i-=1
            temp+=1    
        rgb_led.write()    
        time.sleep_ms(delay)

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路易斯720

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值