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

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

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

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

Micropython ESP频道

ESP32(MicroPython) RGB点阵流水灯+滚动显示+同心矩形


ESP32(MicroPython) RGB点阵流水灯+滚动显示+同心矩形

Snipaste_2023-06-28_20-13-53.png

ESP32 WS2812流水灯+滚动显示+同心矩形


 本程序实现了WS2812流水灯+滚动显示+同心矩形显示功能,延时和亮度可调。由于点阵的灯数不是3的倍数,在颜色选择上使用了类似于拜耳阵列排列,绿色像素数是红、蓝像素数之和。


在变量设定上,由于有渐变流水灯模式,本程序设定了较小的亮度用于确认渐变中每一级的亮度,又设定了另一个变量存储其它模式下的亮度以便使用。流水灯按灯的序号移动,分为渐变和非渐变两种模式。滚动显示在每次开始前要把起始的编号调到8的倍数以确保从一列的开头位置开始,每次起始位置序号加8以实现滚动。同心矩形显示先设定灯的颜色来绘制较大的矩形,在绘制较小的矩形覆盖大的矩形的中间部分,并对记录起始位置的变量取余以循环显示4种图案;绘制较小的矩形时每次到达一列中绘制位置的末端时把灯的编号加上相应数值以到达在下一列绘制的起始位置。


代码如下

#导入Pin模块
from machine import Pin
import time
from machine import PWM
from neopixel import NeoPixel
 
# 五向导航按键,COM引脚接GND
key1=Pin(12,Pin.IN,Pin.PULL_UP)
key2=Pin(14,Pin.IN,Pin.PULL_UP)
key3=Pin(26,Pin.IN,Pin.PULL_UP)
key4=Pin(25,Pin.IN,Pin.PULL_UP)
key5=Pin(33,Pin.IN,Pin.PULL_UP)
key6=Pin(32,Pin.IN,Pin.PULL_UP)
 
pin=13
rgb_num=64
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()==0 or key2.value()==0 or key3.value()==0 or key4.value()==0 or
                      key5.value()==0 or key6.value()==0  ):
        time.sleep_ms(10)
        key_en=0
        if key1.value()==0:
            return 1
        elif key2.value()==0:
            return 2
        elif key3.value()==0:
            return 3
        elif key4.value()==0:
            return 4
        elif key5.value()==0:
            return 5
        elif key6.value()==0:
            return 6
    elif (key1.value()==1 and key2.value()==1 and key3.value()==1 and key4.value()==1 and
          key5.value()==1 and key6.value()==1  ) :
        key_en=1
    return 0
 
brightness=2
delay=40
mode=1
sleep=0
def key_get(): #获取键值并改变变量的值
    global brightness
    global delay
    global mode
    key=key_scan()
    if key==1 and brightness<7 :
        brightness+=1
    elif key==2 and brightness>1 :
        brightness-=1
    elif key==3 and delay<90 :
        delay+=10
    elif key==4 and delay>10 :
        delay-=10
    elif key==5 and mode<4 :
        mode+=1
    elif key==6 and mode>0 :
        mode-=1     
 
count=0
#程序入口
while True:
    key_get()
    full_brightness=brightness*8
    if count>=63 :
        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
        repeat=0
        while repeat<2 : #重复
            repeat+=1
            temp=0
            while temp<8 :
                if i>63 :
                    i-=63
                rgb_led[i]=(0, full_brightness, 0)
                i+=1
                temp+=1
            temp=0    
            while temp<8 :
                if i>63 :
                    i-=63
                rgb_led[i]=(full_brightness, 0, 0)
                i+=1
                temp+=1
            temp=0    
            while temp<8 :
                if i>63 :
                    i-=63
                rgb_led[i]=(0, full_brightness, 0)
                i+=1
                temp+=1
            temp=0    
            while temp<8 :
                if i>63 :
                    i-=63
                rgb_led[i]=(0, 0, full_brightness)
                i+=1
                temp+=1    
        rgb_led.write()    
        time.sleep_ms(delay)
    if mode==2 : #渐变流水灯
        temp=0
        i=count
        count+=1
        repeat=0
        while repeat<2 : #重复
            repeat+=1
            temp=0
            g=8*brightness
            r=0
            while temp<8 :
                if i>63 :
                    i-=63
                rgb_led[i]=(r, g, 0)
                g-=brightness
                r+=brightness
                i+=1
                temp+=1
            temp=0
            r=8*brightness
            g=0
            while temp<8 :
                if i>63 :
                    i-=63
                rgb_led[i]=(r, g, 0)
                r-=brightness
                g+=brightness
                i+=1
                temp+=1
            temp=0
            g=8*brightness
            b=0
            while temp<8 :
                if i>63 :
                    i-=63
                rgb_led[i]=(0, g, b)
                g-=brightness
                b+=brightness
                i+=1
                temp+=1
            temp=0
            b=8*brightness
            g=0
            while temp<8 :
                if i>63 :
                    i-=63
                rgb_led[i]=(0, g, b)
                b-=brightness
                g+=brightness
                i+=1
                temp+=1    
        rgb_led.write()    
        time.sleep_ms(delay)
    if mode==3 : #滚动显示
        sleep+=1
        if sleep==5:
            sleep=0
            if count%8!=0 : #确保从一列的顶端开始显示
                count=0   
            temp=0
            i=count
            count+=8
            repeat=0
            while repeat<4 : #重复
                repeat+=1
                temp=0
                while temp<4 :
                    if i>63 :
                        i-=63   
                    rgb_led[i]=(0, full_brightness, 0)
                    i+=1
                    if i>63 :
                        i-=63
                    rgb_led[i]=(full_brightness, 0, 0)
                    i+=1
                    temp+=1
                temp=0    
                while temp<4 :
                    if i>63 :
                        i-=63
                    rgb_led[i]=(0, 0, full_brightness)
                    i+=1
                    if i>63 :
                        i-=63
                    rgb_led[i]=(0, full_brightness, 0)
                    i+=1
                    temp+=1   
            rgb_led.write()    
        time.sleep_ms(delay)
    if mode==4 : #同心矩形
        sleep+=1
        if sleep==5:
            sleep=0
            count+=1
            if count%4==0 :
                i=0
                while i<64:
                    rgb_led[i]=(0, full_brightness, 0)
                    i+=1
                i=9    
                while i<55:
                    rgb_led[i]=(full_brightness, 0, 0)
                    i+=1
                    if i%8==7:
                        i+=2
                i=18        
                while i<46:
                    rgb_led[i]=(0, full_brightness, 0)
                    i+=1
                    if i%8==6:
                        i+=4
                i=27        
                while i<37:
                    rgb_led[i]=(0, 0, full_brightness)
                    i+=1
                    if i%8==5:
                        i+=6
            if count%4==1 :
                i=0
                while i<64:
                    rgb_led[i]=(full_brightness, 0, 0)
                    i+=1
                i=9    
                while i<55:
                    rgb_led[i]=(0, full_brightness, 0)
                    i+=1
                    if i%8==7:
                        i+=2
                i=18        
                while i<46:
                    rgb_led[i]=(0, 0, full_brightness)
                    i+=1
                    if i%8==6:
                        i+=4
                i=27        
                while i<37:
                    rgb_led[i]=(0, full_brightness, 0)
                    i+=1
                    if i%8==5:
                        i+=6
            if count%4==2 :
                i=0
                while i<64:
                    rgb_led[i]=(0, full_brightness, 0)
                    i+=1
                i=9    
                while i<55:
                    rgb_led[i]=(0, 0, full_brightness)
                    i+=1
                    if i%8==7:
                        i+=2
                i=18        
                while i<46:
                    rgb_led[i]=(full_brightness, 0, 0)
                    i+=1
                    if i%8==6:
                        i+=4
                i=27        
                while i<37:
                    rgb_led[i]=(0, full_brightness, 0)
                    i+=1
                    if i%8==5:
                        i+=6
            if count%4==3 :
                i=0
                while i<64:
                    rgb_led[i]=(0, 0, full_brightness)
                    i+=1
                i=9    
                while i<55:
                    rgb_led[i]=(0, full_brightness, 0)
                    i+=1
                    if i%8==7:
                        i+=2
                i=18        
                while i<46:
                    rgb_led[i]=(full_brightness, 0, 0)
                    i+=1
                    if i%8==6:
                        i+=4
                i=27        
                while i<37:
                    rgb_led[i]=(0, full_brightness, 0)
                    i+=1
                    if i%8==5:
                        i+=6               
            rgb_led.write()    
        time.sleep_ms(delay)


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

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

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



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

Copyright © 2014 ESP56.com All Rights Reserved

执行时间: 0.010216951370239 seconds