ESP32(MicroPython) WS2812 RGB流水灯对称版

ESP32(MicroPython)  RGB流水灯对称版

本程序相比上一个程序,改为上下两部分(实际上为WS2812序号的前半部分和后半部分)的灯的颜色对称。在代码上,需要使用两个变量分别记录前半部分和后半部分的起始位置,初始值分别为灯的序号值的最小值和最大值,并在变量值到达灯的序号值的中间部分时把变量调回初始值。在后半部分,每更新一个灯的数据后减小灯的序号值,即从后往前刷新以实现对称。

代码如下

#导入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=51
delay=40
mode=5
def key_get(): #获取键值并改变变量的值
    global brightness
    global delay
    global mode
    key=key_scan()
    if key==1 and brightness<255 :
        brightness+=17
    elif key==2 and brightness>17 :
        brightness-=17
    elif key==3 and delay<90 :
        delay+=10
    elif key==4 and delay>10 :
        delay-=10
    elif key==5 and mode<6 :
        mode+=1
    elif key==6 and mode>0 :
        mode-=1     

count1=0
count2=23
#程序入口
while True:
    key_get()
    if count1==12 :
        count1=0
    if count2==11 :
        count2=23    
    if mode==0 : #关灯
        for i in range(rgb_num):
            rgb_led[i]=(0, 0, 0)
            rgb_led.write()
    if mode==1 : #三原色对称
        temp=0
        i=count1
        count1+=1
        while temp<4 : #前半部分
            if i>11 :
                i-=12
            rgb_led[i]=(brightness, 0, 0)
            i+=1
            temp+=1
        temp=0    
        while temp<4 :
            if i>11 :
                i-=12
            rgb_led[i]=(0, brightness, 0)
            i+=1
            temp+=1
        temp=0    
        while temp<4 :
            if i>11 :
                i-=12
            rgb_led[i]=(0, 0, brightness)
            i+=1
            temp+=1
        temp=0
        i=count2
        count2-=1
        while temp<4 : #后半部分
            if i<12 :
                i+=12    
            rgb_led[i]=(brightness, 0, 0)
            i-=1
            temp+=1
        temp=0    
        while temp<4 :
            if i<12 :
                i+=12
            rgb_led[i]=(0, brightness, 0)
            i-=1
            temp+=1
        temp=0    
        while temp<4 :
            if i<12 :
                i+=12
            rgb_led[i]=(0, 0, brightness)
            i-=1
            temp+=1   
        rgb_led.write()    
        time.sleep_ms(delay)
    if mode==2 : #三原色对称重复
        temp=0
        i=count1
        count1+=1
        repeat=0
        while repeat<2 :
            repeat+=1
            while temp<2 : #前半部分
                if i>11 :
                    i-=12
                rgb_led[i]=(brightness, 0, 0)
                i+=1
                temp+=1
            temp=0    
            while temp<2 :
                if i>11 :
                    i-=12
                rgb_led[i]=(0, brightness, 0)
                i+=1
                temp+=1
            temp=0    
            while temp<2 :
                if i>11 :
                    i-=12
                rgb_led[i]=(0, 0, brightness)
                i+=1
                temp+=1
            temp=0
        i=count2
        count2-=1
        repeat=0
        while repeat<2 :
            repeat+=1    
            while temp<2 : #后半部分
                if i<12 :
                    i+=12    
                rgb_led[i]=(brightness, 0, 0)
                i-=1
                temp+=1
            temp=0    
            while temp<2 :
                if i<12 :
                    i+=12
                rgb_led[i]=(0, brightness, 0)
                i-=1
                temp+=1
            temp=0    
            while temp<2 :
                if i<12 :
                    i+=12
                rgb_led[i]=(0, 0, brightness)
                i-=1
                temp+=1   
            rgb_led.write()    
            time.sleep_ms(delay)        
    if mode==3 : #三间色对称
        temp=0
        i=count1
        count1+=1
        while temp<4 : #前半部分
            if i>11 :
                i-=12
            rgb_led[i]=(brightness, brightness, 0)
            i+=1
            temp+=1
        temp=0    
        while temp<4 :
            if i>11 :
                i-=12
            rgb_led[i]=(0, brightness, brightness)
            i+=1
            temp+=1
        temp=0    
        while temp<4 :
            if i>11 :
                i-=12
            rgb_led[i]=(brightness, 0, brightness)
            i+=1
            temp+=1
        temp=0
        i=count2
        count2-=1
        while temp<4 : #后半部分
            if i<12 :
                i+=12    
            rgb_led[i]=(brightness, brightness, 0)
            i-=1
            temp+=1
        temp=0    
        while temp<4 :
            if i<12 :
                i+=12
            rgb_led[i]=(0, brightness, brightness)
            i-=1
            temp+=1
        temp=0    
        while temp<4 :
            if i<12 :
                i+=12
            rgb_led[i]=(brightness, 0, brightness)
            i-=1
            temp+=1   
        rgb_led.write()    
        time.sleep_ms(delay)
    if mode==4 : #三间色对称重复
        temp=0
        i=count1
        count1+=1
        repeat=0
        while repeat<2 :
            repeat+=1
            while temp<2 : #前半部分
                if i>11 :
                    i-=12
                rgb_led[i]=(brightness, brightness, 0)
                i+=1
                temp+=1
            temp=0    
            while temp<2 :
                if i>11 :
                    i-=12
                rgb_led[i]=(0, brightness, brightness)
                i+=1
                temp+=1
            temp=0    
            while temp<2 :
                if i>11 :
                    i-=12
                rgb_led[i]=(brightness, 0, brightness)
                i+=1
                temp+=1
            temp=0
        i=count2
        count2-=1
        repeat=0
        while repeat<2 :
            repeat+=1    
            while temp<2 : #后半部分
                if i<12 :
                    i+=12    
                rgb_led[i]=(brightness, brightness, 0)
                i-=1
                temp+=1
            temp=0    
            while temp<2 :
                if i<12 :
                    i+=12
                rgb_led[i]=(0, brightness, brightness)
                i-=1
                temp+=1
            temp=0    
            while temp<2 :
                if i<12 :
                    i+=12
                rgb_led[i]=(brightness, 0, brightness)
                i-=1
                temp+=1   
            rgb_led.write()    
            time.sleep_ms(delay)   
    if mode==5 : #三原色+三间色对称
        temp=0
        i=count1
        count1+=1
        while temp<2 : #前半部分
            if i>11 :
                i-=12
            rgb_led[i]=(brightness, 0, 0)
            i+=1
            temp+=1
        temp=0    
        while temp<2 :
            if i>11 :
                i-=12
            rgb_led[i]=(brightness, brightness, 0)
            i+=1
            temp+=1
        temp=0    
        while temp<2 :
            if i>11 :
                i-=12
            rgb_led[i]=(0, brightness, 0)
            i+=1
            temp+=1
        temp=0    
        while temp<2 : 
            if i>11 :
                i-=12
            rgb_led[i]=(0, brightness, brightness)
            i+=1
            temp+=1
        temp=0    
        while temp<2 :
            if i>11 :
                i-=12
            rgb_led[i]=(0, 0, brightness)
            i+=1
            temp+=1
        temp=0    
        while temp<2 :
            if i>11 :
                i-=12
            rgb_led[i]=(brightness, 0, brightness)
            i+=1
            temp+=1    
        temp=0
        i=count2
        count2-=1
        while temp<2 : #后半部分
            if i<12 :
                i+=12    
            rgb_led[i]=(brightness, 0, 0)
            i-=1
            temp+=1
        temp=0    
        while temp<2 :
            if i<12 :
                i+=12
            rgb_led[i]=(brightness, brightness, 0)
            i-=1
            temp+=1
        temp=0    
        while temp<2 :
            if i<12 :
                i+=12
            rgb_led[i]=(0, brightness, 0)
            i-=1
            temp+=1
        temp=0    
        while temp<2 : 
            if i<12 :
                i+=12    
            rgb_led[i]=(0, brightness, brightness)
            i-=1
            temp+=1
        temp=0    
        while temp<2 :
            if i<12 :
                i+=12
            rgb_led[i]=(0, 0, brightness)
            i-=1
            temp+=1
        temp=0    
        while temp<2 :
            if i<12 :
                i+=12
            rgb_led[i]=(brightness, 0, brightness)
            i-=1
            temp+=1      
        rgb_led.write()    
        time.sleep_ms(delay)
    if mode==6 : #三原色+三间色对称重复
        i=count1
        count1+=1
        repeat=0 
        while repeat<2 : #前半部分
            repeat+=1
            if i>11 :
                i-=12
            rgb_led[i]=(brightness, 0, 0)
            i+=1
            if i>11 :
                i-=12
            rgb_led[i]=(brightness, brightness, 0)
            i+=1
            if i>11 :
                i-=12
            rgb_led[i]=(0, brightness, 0)
            i+=1
            if i>11 :
                i-=12
            rgb_led[i]=(0, brightness, brightness)
            i+=1
            if i>11 :
                i-=12
            rgb_led[i]=(0, 0, brightness)
            i+=1
            if i>11 :
                i-=12
            rgb_led[i]=(brightness, 0, brightness)
            i+=1
        i=count2
        count2-=1
        repeat=0
        while repeat<2 : #后半部分
            repeat+=1
            if i<12 :
                i+=12    
            rgb_led[i]=(brightness, 0, 0)
            i-=1
            if i<12 :
                i+=12
            rgb_led[i]=(brightness, brightness, 0)
            i-=1
            if i<12 :
                i+=12
            rgb_led[i]=(0, brightness, 0)
            i-=1
            if i<12 :
                i+=12    
            rgb_led[i]=(0, brightness, brightness)
            i-=1
            if i<12 :
                i+=12
            rgb_led[i]=(0, 0, brightness)
            i-=1
            if i<12 :
                i+=12
            rgb_led[i]=(brightness, 0, brightness)
            i-=1
        rgb_led.write()    
        time.sleep_ms(delay)
        

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

路易斯720

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

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

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

打赏作者

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

抵扣说明:

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

余额充值