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

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

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

代码如下

#导入Pin模块from machine import Pinimport timefrom machine import PWMfrom neopixel import NeoPixel # 五向导航按键,COM引脚接GNDkey1=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=13rgb_num=64rgb_led=NeoPixel(Pin(pin,Pin.OUT),rgb_num)key_en=1#按键扫描函数def key_scan():global key_enif key_en==1 and (key1.value()==0 or key2.value()==0 or key3.value()==0 or key4.value()==0 orkey5.value()==0 or key6.value()==0):time.sleep_ms(10)key_en=0if key1.value()==0:return 1elif key2.value()==0:return 2elif key3.value()==0:return 3elif key4.value()==0:return 4elif key5.value()==0:return 5elif key6.value()==0:return 6elif (key1.value()==1 and key2.value()==1 and key3.value()==1 and key4.value()==1 andkey5.value()==1 and key6.value()==1) :key_en=1return 0brightness=2delay=40mode=1sleep=0def key_get(): #获取键值并改变变量的值global brightnessglobal delayglobal modekey=key_scan()if key==1 and brightness1 :brightness-=1elif key==3 and delay10 :delay-=10elif key==5 and mode0 :mode-=1 count=0#程序入口while True:key_get()full_brightness=brightness*8if count>=63 :count=0if mode==0 : #关灯for i in range(rgb_num):rgb_led[i]=(0, 0, 0)rgb_led.write()if mode==1 : #流水灯temp=0i=countcount+=1repeat=0while repeat<2 : #重复repeat+=1temp=0while temp63 :i-=63rgb_led[i]=(0, full_brightness, 0)i+=1temp+=1temp=0while temp63 :i-=63rgb_led[i]=(full_brightness, 0, 0)i+=1temp+=1temp=0while temp63 :i-=63rgb_led[i]=(0, full_brightness, 0)i+=1temp+=1temp=0while temp63 :i-=63rgb_led[i]=(0, 0, full_brightness)i+=1temp+=1rgb_led.write()time.sleep_ms(delay)if mode==2 : #渐变流水灯temp=0i=countcount+=1repeat=0while repeat<2 : #重复repeat+=1temp=0g=8*brightnessr=0while temp63 :i-=63rgb_led[i]=(r, g, 0)g-=brightnessr+=brightnessi+=1temp+=1temp=0r=8*brightnessg=0while temp63 :i-=63rgb_led[i]=(r, g, 0)r-=brightnessg+=brightnessi+=1temp+=1temp=0g=8*brightnessb=0while temp63 :i-=63rgb_led[i]=(0, g, b)g-=brightnessb+=brightnessi+=1temp+=1temp=0b=8*brightnessg=0while temp63 :i-=63rgb_led[i]=(0, g, b)b-=brightnessg+=brightnessi+=1temp+=1rgb_led.write()time.sleep_ms(delay)if mode==3 : #滚动显示sleep+=1if sleep==5:sleep=0if count%8!=0 : #确保从一列的顶端开始显示count=0 temp=0i=countcount+=8repeat=0while repeat<4 : #重复repeat+=1temp=0while temp63 :i-=63 rgb_led[i]=(0, full_brightness, 0)i+=1if i>63 :i-=63rgb_led[i]=(full_brightness, 0, 0)i+=1temp+=1temp=0while temp63 :i-=63rgb_led[i]=(0, 0, full_brightness)i+=1if i>63 :i-=63rgb_led[i]=(0, full_brightness, 0)i+=1temp+=1 rgb_led.write()time.sleep_ms(delay)if mode==4 : #同心矩形sleep+=1if sleep==5:sleep=0count+=1if count%4==0 :i=0while i<64:rgb_led[i]=(0, full_brightness, 0)i+=1i=9while i<55:rgb_led[i]=(full_brightness, 0, 0)i+=1if i%8==7:i+=2i=18while i<46:rgb_led[i]=(0, full_brightness, 0)i+=1if i%8==6:i+=4i=27while i<37:rgb_led[i]=(0, 0, full_brightness)i+=1if i%8==5:i+=6if count%4==1 :i=0while i<64:rgb_led[i]=(full_brightness, 0, 0)i+=1i=9while i<55:rgb_led[i]=(0, full_brightness, 0)i+=1if i%8==7:i+=2i=18while i<46:rgb_led[i]=(0, 0, full_brightness)i+=1if i%8==6:i+=4i=27while i<37:rgb_led[i]=(0, full_brightness, 0)i+=1if i%8==5:i+=6if count%4==2 :i=0while i<64:rgb_led[i]=(0, full_brightness, 0)i+=1i=9while i<55:rgb_led[i]=(0, 0, full_brightness)i+=1if i%8==7:i+=2i=18while i<46:rgb_led[i]=(full_brightness, 0, 0)i+=1if i%8==6:i+=4i=27while i<37:rgb_led[i]=(0, full_brightness, 0)i+=1if i%8==5:i+=6if count%4==3 :i=0while i<64:rgb_led[i]=(0, 0, full_brightness)i+=1i=9while i<55:rgb_led[i]=(0, full_brightness, 0)i+=1if i%8==7:i+=2i=18while i<46:rgb_led[i]=(full_brightness, 0, 0)i+=1if i%8==6:i+=4i=27while i<37:rgb_led[i]=(0, full_brightness, 0)i+=1if i%8==5:i+=6 rgb_led.write()time.sleep_ms(delay)