Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。

Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台

Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。

Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。

Micropython ESP频道

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


ESP32(MicroPython) WS2812 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)

        

 

————————————————

版权声明:本文为CSDN博主「路易斯720」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/weixin_74155302/article/details/131426125



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 ESP56.com All Rights Reserved

执行时间: 0.010501146316528 seconds