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) RGB流水灯 新版

Snipaste_2023-06-28_20-19-09.png

 本程序相比上一个程序,改用了24灯的环形WS2812模块,数据引脚改用13脚。增加了把相应颜色重复2次(即前半部分和后半部分的灯颜色排列相同)和4次的模式,模式增加到9种。


代码如下

#导入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=8
def key_get(): #获取键值并改变变量的值
    global brightness
    global delay
    global mode
    key=key_scan()
    if key==1 and brightness17 :
        brightness-=17
    elif key==3 and delay10 :
        delay-=10
    elif key==5 and mode0 :
        mode-=1     
 
count=0
#程序入口
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
        while temp23 :
                i-=24
            rgb_led[i]=(brightness, 0, 0)
            i+=1
            temp+=1
        temp=0    
        while temp23 :
                i-=24
            rgb_led[i]=(0, brightness, 0)
            i+=1
            temp+=1
        temp=0    
        while temp23 :
                i-=24
            rgb_led[i]=(0, 0, brightness)
            i+=1
            temp+=1
        rgb_led.write()    
        time.sleep_ms(delay)
    if mode==2 : #三原色重复2次
        temp=0
        i=count
        count+=1
        repeat=0
        while repeat<4 :
            temp=0
            repeat+=1
            while temp23 :
                    i-=24
                rgb_led[i]=(brightness, 0, 0)
                i+=1
                temp+=1
            temp=0    
            while temp23 :
                    i-=24
                rgb_led[i]=(0, brightness, 0)
                i+=1
                temp+=1
            temp=0    
            while temp23 :
                    i-=24
                rgb_led[i]=(0, 0, brightness)
                i+=1
                temp+=1
        rgb_led.write()    
        time.sleep_ms(delay)
    if mode==3 : #三原色重复4次
        temp=0
        i=count
        count+=1
        repeat=0
        while repeat<4 :
            temp=0
            repeat+=1
            while temp23 :
                    i-=24
                rgb_led[i]=(brightness, 0, 0)
                i+=1
                temp+=1
            temp=0    
            while temp23 :
                    i-=24
                rgb_led[i]=(0, brightness, 0)
                i+=1
                temp+=1
            temp=0    
            while temp23 :
                    i-=24
                rgb_led[i]=(0, 0, brightness)
                i+=1
                temp+=1
        rgb_led.write()    
        time.sleep_ms(delay)        
    if mode==4 : #三间色
        temp=0
        i=count
        count+=1
        while temp23 :
                i-=24
            rgb_led[i]=(brightness, brightness, 0)
            i+=1
            temp+=1
        temp=0    
        while temp23 :
                i-=24
            rgb_led[i]=(0, brightness, brightness)
            i+=1
            temp+=1
        temp=0    
        while temp23 :
                i-=24
            rgb_led[i]=(brightness, 0, brightness)
            i+=1
            temp+=1
        rgb_led.write()    
        time.sleep_ms(delay)
    if mode==5 : #三间色重复2次
        temp=0
        i=count
        count+=1
        repeat=0
        while repeat<2 :
            temp=0
            repeat+=1
            while temp23 :
                    i-=24
                rgb_led[i]=(brightness, brightness, 0)
                i+=1
                temp+=1
            temp=0    
            while temp23 :
                    i-=24
                rgb_led[i]=(0, brightness, brightness)
                i+=1
                temp+=1
            temp=0    
            while temp23 :
                    i-=24
                rgb_led[i]=(brightness, 0, brightness)
                i+=1
                temp+=1
        rgb_led.write()    
        time.sleep_ms(delay)
    if mode==6 : #三间色重复4次
        temp=0
        i=count
        count+=1
        repeat=0
        while repeat<4 :
            temp=0
            repeat+=1
            while temp23 :
                    i-=24
                rgb_led[i]=(brightness, brightness, 0)
                i+=1
                temp+=1
            temp=0    
            while temp23 :
                    i-=24
                rgb_led[i]=(0, brightness, brightness)
                i+=1
                temp+=1
            temp=0    
            while temp23 :
                    i-=24
                rgb_led[i]=(brightness, 0, brightness)
                i+=1
                temp+=1
        rgb_led.write()    
        time.sleep_ms(delay)     
    if mode==7 : #三原色+三间色
        temp=0
        i=count
        count+=1
        while temp23 :
                i-=24
            rgb_led[i]=(brightness, 0, 0)
            i+=1
            temp+=1
        temp=0    
        while temp23 :
                i-=24
            rgb_led[i]=(brightness, brightness, 0)
            i+=1
            temp+=1
        temp=0    
        while temp23 :
                i-=24
            rgb_led[i]=(0, brightness, 0)
            i+=1
            temp+=1
        temp=0    
        while temp23 :
                i-=24
            rgb_led[i]=(0, brightness, brightness)
            i+=1
            temp+=1
        temp=0    
        while temp23 :
                i-=24
            rgb_led[i]=(0, 0, brightness)
            i+=1
            temp+=1
        temp=0    
        while temp23 :
                i-=24
            rgb_led[i]=(brightness, 0, brightness)
            i+=1
            temp+=1
        rgb_led.write()    
        time.sleep_ms(delay)
    if mode==8 : #三原色+三间色重复2次
        temp=0
        i=count
        count+=1
        repeat=0
        while repeat<2 :
            temp=0
            repeat+=1
            while temp23 :
                    i-=24
                rgb_led[i]=(brightness, 0, 0)
                i+=1
                temp+=1
            temp=0    
            while temp23 :
                    i-=24
                rgb_led[i]=(brightness, brightness, 0)
                i+=1
                temp+=1
            temp=0    
            while temp23 :
                    i-=24
                rgb_led[i]=(0, brightness, 0)
                i+=1
                temp+=1
            temp=0    
            while temp23 :
                    i-=24
                rgb_led[i]=(0, brightness, brightness)
                i+=1
                temp+=1
            temp=0    
            while temp23 :
                    i-=24
                rgb_led[i]=(0, 0, brightness)
                i+=1
                temp+=1
            temp=0    
            while temp23 :
                    i-=24
                rgb_led[i]=(brightness, 0, brightness)
                i+=1
                temp+=1
        rgb_led.write()    
        time.sleep_ms(delay)           
    if mode==9 : #三原色+三间色重复4次
        temp=0
        i=count
        count+=1
        repeat=0
        while repeat23 :
                i-=24
            rgb_led[i]=(brightness, 0, 0)
            i+=1
            temp+=1
            if i>23 :
                i-=24
            rgb_led[i]=(brightness, brightness, 0)
            i+=1
            temp+=1
            if i>23 :
                i-=24
            rgb_led[i]=(0, brightness, 0)
            i+=1
            temp+=1
            if i>23 :
                i-=24
            rgb_led[i]=(0, brightness, brightness)
            i+=1
            temp+=1
            if i>23 :
                i-=24
            rgb_led[i]=(0, 0, brightness)
            i+=1
            temp+=1
            if i>23 :
                i-=24
            rgb_led[i]=(brightness, 0, brightness)
            i+=1
            temp+=1
        rgb_led.write()    
        time.sleep_ms(delay)


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

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

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



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

Copyright © 2014 ESP56.com All Rights Reserved

执行时间: 0.009890079498291 seconds