本帖最后由 是最帅的啊 于 2021-8-7 01:46 编辑
事情是这样的,前一段时间有个朋友说她要过生日,问我送什么礼物给她,我说咱俩关系这么好,我给你做一个小礼物吧,她说好呀,买的东西总是不好,自己动手做的才是心意。
对于送什么礼物我毫无头绪,有天在B站上看着小姐姐跳舞的时候,突然发现一个UP主用ESP32和WS2812做了一个灯球的时候。我的内心告诉我,就是它了。
先给大家看一下视频吧
首先要了解这个这个灯球的运行原理,这里面的小灯为ws2812,下面是官方的介绍和它的引脚图
具体资料的话可以在立创上下载。这个我就不过多的讲解啦,正好在microPython中有这个ws2812的驱动函数,真是天助我也。
话不多说,开干开干。首先要准备材料,基础材料如下
材料清单
名称 |
数量 |
备注 |
ws2812 |
220 |
灯珠 |
esp8266-12f |
1 |
主控芯片 |
hx4054 |
1 |
锂电池管理芯片 |
662K |
1 |
DC-DC |
铜丝 |
8米 |
0.8mm直径 |
模具 |
1 |
固定与焊接灯珠使用 |
买回来的铜丝和模具先要检查好,具体的焊接方法可以在B站上搜索 山烬是条咸鱼 这个小姐姐,她有详细的焊接步骤和说明(我的模具也在她的店里买的)
这个是我焊接的照片,觉得不是那么容易(这里超级考验焊接功底,特别容易虚焊和锡太多短路) 希望大家在每焊完半个球的时候要检查一下虚焊问题,当你的灯珠在某一个点突然爆亮的时候,要检查一下前面灯珠的GND是不是虚焊了。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~分界线~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
经过10多个小时的焊接过程,终于把灯球的外金属框架搭起来了(这里忘记拍图了),在这期间我抽空画了一下原理图和PCB(立创画的),拨动开关是用来控制整体的供电,并不影响充电问题(这个电路必须要焊接电池才能正常给8266供电,如果没有电池,HX4054就无法正常工作,这里也可以分别在电池到662K之间串联一个肖特基二极管和USB到662K之间串联二极管,我嫌麻烦就没有这样做)。注意我这里在PCB上画了两个“中联”的焊盘也是很重要得哦。
检查完毕灯球的时候就可以快乐的写程序啦,先给灯球来一段定义代码
from machine import Pin
from neopixel import NeoPixel
import time
p2 = Pin(2,Pin.OUT) ###定义引脚
np = NeoPixel(p2,220)###定义灯的个数
section = [0,8,22,40,60,84,110,136,160,180,198,212,220]#每一层的起始点
上图上图
在开始的这一段程序中,它旋转前面的灯为高亮的粉色,然后渐变为普通的粉色直到把整个灯球铺满
铺满粉色之后呢,就开始进行颜色渐变遵循的是红到绿,绿到蓝,蓝到白,最后颜色消失。
在代码段,我选择使用颜色列表,目的是减少8266的计算量进而提高运行速度。(Python大法好,算颜色的时候提供很大的帮助)
def jianbian1():
colour = [
(32, 4, 10), (32, 4, 9), (32, 4, 8), (32, 4, 7), (32, 4, 6), (32, 4, 5), (32, 4, 4), (32, 4, 3), (32, 4, 2), (32, 4, 1),(32, 4, 0),
(32, 3, 0),(32, 2, 0),(32, 1, 0),(32, 0, 0), (31, 1, 0), (30, 2, 0), (29, 3, 0), (28, 4, 0), (27, 5, 0), (26, 6, 0), (25, 7, 0), (24, 8, 0),
(23, 9, 0), (22, 10, 0), (21, 11, 0), (20, 12, 0), (19, 13, 0), (18, 14, 0), (17, 15, 0), (16, 16, 0), (15, 17, 0), (14, 18, 0), (13, 19, 0),
(12, 20, 0), (11, 21, 0),
(10, 22, 0), (9, 23, 0), (8, 24, 0), (7, 25, 0), (6, 26, 0), (5, 27, 0), (4, 28, 0), (3, 29, 0), (2, 30, 0), (1, 31, 0), (0, 32, 0),
(0, 31, 1), (0, 30, 2), (0, 29, 3), (0, 28, 4), (0, 27, 5), (0, 26, 6), (0, 25, 7), (0, 24, 8), (0, 23, 9), (0, 22, 10), (0, 21, 11), (0, 20, 12),
(0, 19, 13), (0, 18, 14), (0, 17, 15), (0, 16, 16), (0, 15, 17), (0, 14, 18), (0, 13, 19), (0, 12, 20), (0, 11, 21), (0, 10, 22), (0, 9, 23), (0, 8, 24),
(0, 7, 25), (0, 6, 26), (0, 5, 27), (0, 4, 28), (0, 3, 29), (0, 2, 30), (0, 1, 31), (0, 0, 32),
(1, 0, 32), (2, 0, 32), (3, 0, 32), (4, 0, 32), (5, 0, 32), (6, 0, 32), (7, 0, 32),
(8, 0, 32), (9, 0, 32), (10, 0, 32), (11, 0, 32), (12, 0, 32), (13, 0, 32), (14, 0, 32), (15, 0, 32), (16, 0, 32), (17, 0, 32), (18, 0, 32),
(19, 0, 32), (20, 0, 32), (21, 0, 32), (22, 0, 32), (23, 0, 32), (24, 0, 32), (25, 0, 32), (26, 0, 32), (27, 0, 32), (28, 0, 32), (29, 0, 32),
(30, 0, 32), (31, 0, 32), (32, 0, 32),(32, 0, 32), (32, 1, 32), (32, 2, 32), (32, 3, 32), (32, 4, 32), (32, 5, 32), (32, 6, 32), (32, 7, 32),
(32, 8, 32), (32, 9, 32), (32, 10, 32), (32, 11, 32), (32, 12, 32), (32, 13, 32), (32, 14, 32), (32, 15, 32), (32, 16, 32), (32, 17, 32), (32, 18, 32),
(32, 19, 32), (32, 20, 32), (32, 21, 32), (32, 22, 32), (32, 23, 32), (32, 24, 32), (32, 25, 32), (32, 26, 32), (32, 27, 32), (32, 28, 32), (32, 29, 32),
(32, 30, 32), (32, 31, 32), (32, 32, 32),(32, 32, 32), (31, 31, 31), (30, 30, 30), (29, 29, 29), (28, 28, 28), (27, 27, 27), (26, 26, 26), (25, 25, 25),
(24, 24, 24), (23, 23, 23), (22, 22, 22), (21, 21, 21), (20, 20, 20), (19, 19, 19), (18, 18, 18), (17, 17, 17), (16, 16, 16), (15, 15, 15), (14, 14, 14),
(13, 13, 13), (12, 12, 12), (11, 11, 11), (10, 10, 10), (9, 9, 9), (8, 8, 8), (7, 7, 7), (6, 6, 6), (5, 5, 5), (4, 4, 4), (3, 3, 3), (2, 2, 2), (1, 1, 1),
(0, 0, 0)
]
for i in range(177):
for x in range(220):
np[x] = colour
np.write()
第三段呢 就是一层一层的点亮灯,然后每一圈灯的颜色都是不一样的。最上层是蓝色,颜色渐变到紫色最后中间是粉色。
def guangquan1():
colour = [(5,5,25),(10,4,25),(15,4,20),(20,4,15),(25,4,10),(30,5,5),(30,5,5),(25,4,10),(20,4,15),(15,4,20),(10,4,25),(5,5,25)]
for i in range(12):
for x in range(section,section[i+1]):
np[x] = colour
np.write()
time.sleep_ms(750)
第四部分为旋转的灯,就是在每一圈基本颜色都不变的情况下使部分灯珠变亮然后旋转。
def yidong(a=0,b=0,c=0,d=()):
if a > b-c :
a = a%(b-c)
np[a+c] = d
def guangquan2():
section = [0,8,22,40,60,84,110,136,160,180,198,212,220]
colour=[
[(5, 5, 5), (5, 5, 10), (5, 5, 15), (5, 5, 20), (5, 5, 25)],
[(5, 5, 25), (5, 10, 25), (5, 15, 25), (5, 20, 25), (5, 25, 25)],
[(5, 25, 25), (5, 25, 20), (5, 25, 15), (5, 25, 10), (5, 25, 5)],
[(5, 25, 5), (10, 25, 5), (15, 25, 5), (20, 25, 5), (25, 25, 5)],
[(25, 25, 5), (25, 20, 5), (25, 15, 5), (25, 10, 5), (25, 5, 5)],
[(25, 5, 5), (25, 5, 10), (25, 5, 15), (25, 5, 20), (25, 5, 25)],
[(25, 5, 5), (25, 5, 10), (25, 5, 15), (25, 5, 20), (25, 5, 25)],
[(25, 25, 5), (25, 20, 5), (25, 15, 5), (25, 10, 5), (25, 5, 5)],
[(5, 25, 5), (10, 25, 5), (15, 25, 5), (20, 25, 5), (25, 25, 5)],
[(5, 25, 25), (5, 25, 20), (5, 25, 15), (5, 25, 10), (5, 25, 5)],
[(5, 5, 25), (5, 10, 25), (5, 15, 25), (5, 20, 25), (5, 25, 25)],
[(5, 5, 5), (5, 5, 10), (5, 5, 15), (5, 5, 20), (5, 5, 25)],
]
for i in range(5):
for x in range(12):
np[section[x]+i] = colour[x]
time.sleep_ms(50)
np.write()
for i in range(5,1000):
for x in range(12):
for y in range(5):
yidong(i-y,section[x+1],section[x],colour[x][y])
np.write()
第五部分是高亮部分从两端汇聚到中间,然后又扩散到两端的效果
具体代码如下
def zeng(i=0,a=0,b=0,colour = []):
for x in range(a,b):
np[x] = colour
def jian(i=0,a=0,b=0,colour=[]):
for x in range(a,b):
np[x] = colour[4-i]
def juhe():
section = [0,8,22,40,60,84,110,136,160,180,198,212,220]
colours = [
[(5, 5, 25), (10, 10, 50), (15, 15, 75), (20, 20, 100), (25, 25, 125)],
[(5, 25, 25), (10, 50, 50), (15, 75, 75), (20, 100, 100), (25, 125, 125)],
[(5, 25, 5), (10, 50, 10), (15, 75, 15), (20, 100, 20), (25, 125, 25)],
[(25, 25, 5), (50, 50, 10), (75, 75, 15), (100, 100, 20), (125, 125, 25)],
[(25, 5, 5), (50, 10, 10), (75, 15, 15), (100, 20, 20), (125, 25, 25)],
[(25, 5, 25), (50, 10, 50), (75, 15, 75), (100, 20, 100), (125, 25, 125)],
[(25, 5, 25), (50, 10, 50), (75, 15, 75), (100, 20, 100), (125, 25, 125)],
[(25, 5, 5), (50, 10, 10), (75, 15, 15), (100, 20, 20), (125, 25, 25)],
[(25, 25, 5), (50, 50, 10), (75, 75, 15), (100, 100, 20), (125, 125, 25)],
[(5, 25, 5), (10, 50, 10), (15, 75, 15), (20, 100, 20), (25, 125, 25)],
[(5, 25, 25), (10, 50, 50), (15, 75, 75), (20, 100, 100), (25, 125, 125)],
[(5, 5, 25), (10, 10, 50), (15, 15, 75), (20, 20, 100), (25, 25, 125)]
]
for y in range(2):
for i in range(6):
for x in range(5):
zeng(x,section,section[i+1],colours)
zeng(x,section[11-i],section[12-i],colours)
time.sleep_ms(50)
np.write()
for x in range(5):
jian(x,section,section[i+1],colours)
jian(x,section[11-i],section[12-i],colours)
time.sleep_ms(50)
np.write()
for i in range(6):
for x in range(5):
zeng(x,section[5-i],section[6-i],colours)
zeng(x,section[6+i],section[7+i],colours)
time.sleep_ms(50)
np.write()
for x in range(5):
jian(x,section[5-i],section[6-i],colours)
jian(x,section[6+i],section[7+i],colours)
time.sleep_ms(50)
np.write()
到这里这个灯也就算了基本完成,历时一个月,从想法到实践,一点点的完成这个项目,从中经历的艰辛和快乐真的回味无穷。
这个灯球也寄到友人手里,她很开心,感谢我的付出说这是收到最好的一份礼物了,当我看到她拍的视频的时候(md,灯又脱焊了)。注定是一个不完美的结局。