【项目背景】
2019年5月10日晚,北京冬奥会倒计时1000天活动在奥林匹克公园玲珑塔南侧广场举行。活动中,北京2022年冬奥会倒计时装置正式启动。
2021年2月4日,位于北京奥林匹克公园核心区中心位置的玲珑塔,北京冬奥会倒计时钟显示365天。玲珑塔南起500余米的国家体育场鸟巢,在2008年见证过国人的奥运梦想与荣光后,一年之后的夜晚,将迎来一场世界奥林匹克冰雪盛宴的开幕。
北京冬季奥运会设7个大项,15个分项,109个小项。北京将主办冰上项目,张家口将主办雪上项目,延庆协办张家口举办雪上项目。
我在河北张家口,我为冬奥会加油。
【项目设计】
使用LED灯带和掌控板,制作一个仿玲珑塔北京冬奥会倒计时钟。
【项目材料】
炫彩 WS2812 LED灯带 3米(2条)https://www.dfrobot.com.cn/goods-852.html
掌控板2.0(掌控宝)https://www.dfrobot.com.cn/goods-852.html
【项目制作】
【Mind+编程】
一、编号
因使用完整灯带,没有剪开,所以所有的灯都要重新对应编号。在编程时,也要使用列表将原灯号及新编号进行记录。才能对应控制每个灯的亮灭,完成显示效果。
二、程序初始化
1、每个灯带120个灯,共240个。数据线接掌控宝13口上。灯带单独供电,与掌控板要共地。
2、连接Wifi,获取时间。
3、倒计时的天数等时间,因图型中没有找到时间差处理方法,所以这几个值,后面用代码模式处理。
4、“灯列表”中,放的是灯的实际编号。因弯曲部分有灯,所显示出来的灯号并不都连续。(见制作图)
- [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,19,20,21,22,23,24,25,26,27,28,29,30,31,36,37,38,39,40,41,42,43,44,45,46,53,54,55,56,57,58,59,60,61,70,71,72,73,74,87,88,89,102,103,104,115,128,129,130,144,145,146,160,161,162,163,164,174,175,176,177,178,179,180,181,182,191,192,193,194,195,196,197,198,199,200,201,206,207,208,209,210,211,212,213,214215,216,217,218,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239]
复制代码
5、灯落下,为了形成沙堆的效果,需要对“灯堆”进行分层记录灯号。
- [239,206,201,174,130,225,218,191,182,128,115],[238,207,200,175,164,144,226,217,192,181,160,146,129],[237,208,199,176,163,227,216,193,180,161,145],[236,209,198,177,228,215,194,179,162],[235,210,197,229,214,195,178], [234,211,230,213,196], [233,231,212]
复制代码
并进行记录新编号(落灯堆序号)。
- [118,91,90,71,62,104,103,80,79,60,59],[117,92,89,72,70,63,105,102,81,78,66,65,61],[116,93,88,73,69,106,101,82,77,67,64],[115,94,87,74,107,100,83,76,68],[114,95,86,108,99,84,75], [113,96,109,98,85], [112,110,97]
复制代码
6、对中线灯进行记录编号(中线灯上下列表)
- [115,103,88,72,57,41,25,8]
复制代码
- [115,129,145,162,178,196,212,232]
复制代码
三、主程序
四、代码处理日期差,计算倒计时
- firstDay = (time.localtime()[0], time.localtime()[1], time.localtime()[2],time.localtime()[3],time.localtime()[4],time.localtime()[5],time.localtime()[6],time.localtime()[7],-1)
- endDay=(2022,2,4,20,0,0,1,35,-1)
- Time1=(time.mktime(endDay)-time.mktime(firstDay))
- Tian=(int(Time1/(24*3600)))
- Time2=Time1-Tian*24*3600
- Shi = int((Time2)/3600)
- Time3=Time2 - Shi*3600
- FenZhong = int((Time3)/60)
-
- #Miao = int(Time3-FenZhong*60)
- Miao =59
复制代码
五、MicroPython完成代码
-
- # MindPlus
- # mpython
- import font.digiface_44
- import font.digiface_21
- from mpython import *
- import neopixel
- import framebuf
- import network
- import ntptime
- import time
- import math
-
- def neopixel_range(rgb, start, end, color):
- trend = -1 if end < start else 1
- for i in range(start, end-1 if trend < 0 else end+1, trend):
- rgb[i] = (color)
- rgb.write()
-
- def display_font(_font, _str, _x, _y, _wrap, _z=0):
- _start = _x
- for _c in _str:
- _d = _font.get_ch(_c)
- if _wrap and _x > 128 - _d[2]: _x = _start; _y += _d[1]
- if _c == '1' and _z > 0: oled.fill_rect(_x, _y, _d[2], _d[1], 0)
- oled.blit(framebuf.FrameBuffer(bytearray(_d[0]), _d[2], _d[1],
- framebuf.MONO_HLSB), (_x+int(_d[2]/_z)) if _c=='1' and _z>0 else _x, _y)
- _x += _d[2]
-
-
- my_wifi = wifi()
- ws2812_P13 = neopixel.NeoPixel(Pin(Pin.P13), n=240, bpp=3, timing=1)
- ws2812_P13.fill((0, 0, 0))
- ws2812_P13.write()
- my_wifi.connectWiFi("sxs","smj080823")
- while not (my_wifi.sta.isconnected()):
- pass
- ntptime.settime(8, "ntp.ntsc.ac.cn")
-
- firstDay = (time.localtime()[0], time.localtime()[1], time.localtime()[2],time.localtime()[3],time.localtime()[4],time.localtime()[5],time.localtime()[6],time.localtime()[7],-1)
- endDay=(2022,2,4,20,0,0,1,35,-1)
- Time1=(time.mktime(endDay)-time.mktime(firstDay))
- Tian=(int(Time1/(24*3600)))
- Time2=Time1-Tian*24*3600
- Shi = int((Time2)/3600)
- Time3=Time2 - Shi*3600
- FenZhong = int((Time3)/60)
-
- #Miao = int(Time3-FenZhong*60)
- Miao =59
- DengLieBiao = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,19,20,21,22,23,24,25,26,27,28,29,30,31,36,37,38,39,40,41,42,43,44,45,46,53,54,55,56,57,58,59,60,61,70,71,72,73,74,87,88,89,102,103,104,115,128,129,130,144,145,146,160,161,162,163,164,174,175,176,177,178,179,180,181,182,191,192,193,194,195,196,197,198,199,200,201,206,207,208,209,210,211,212,213,214215,216,217,218,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239]
- LuoDengDui = [[239,206,201,174,130,225,218,191,182,128,115],[238,207,200,175,164,144,226,217,192,181,160,146,129],[237,208,199,176,163,227,216,193,180,161,145],[236,209,198,177,228,215,194,179,162],[235,210,197,229,214,195,178], [234,211,230,213,196], [233,231,212]]
- LuoDengDuiXuHao = [[118,91,90,71,62,104,103,80,79,60,59],[117,92,89,72,70,63,105,102,81,78,66,65,61],[116,93,88,73,69,106,101,82,77,67,64],[115,94,87,74,107,100,83,76,68],[114,95,86,108,99,84,75], [113,96,109,98,85], [112,110,97]]
- ZhongXianDengShangLieBiao = [115,103,88,72,57,41,25,8]
- ZhongXianDengXiaLieBiao = [115,129,145,162,178,196,212,232]
- while True:
- ws2812_P13.fill((0, 0, 0))
- ws2812_P13.write()
- LiangMieLieBiao = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
- i = 0
- FangXiang = 0
- for index in range(60):
- neopixel_range(ws2812_P13, (DengLieBiao[i]), (DengLieBiao[i]), (0, 0, 255))
- i = (i + 1)
- i = 0
- if (FangXiang == 0):
- LuoDengLieBiao = ZhongXianDengXiaLieBiao
- LuoDengXuHao = [59,61,64,68,75,85,97,111]
- else:
- LuoDengLieBiao = ZhongXianDengShangLieBiao
- LuoDengXuHao = [59,57,54,50,43,33,21,7]
- for index in range(60):
- YunXingShiJian = time.ticks_ms()
- YunXingShiJian = time.ticks_ms()
- j = 0
- ZhongXianMieDengGeShu = 0
- for index in range(len(LuoDengXuHao)):
- if ((LiangMieLieBiao[(LuoDengXuHao[j])]) == 0):
- ZhongXianMieDengGeShu = (ZhongXianMieDengGeShu + 1)
- j = (j + 1)
- if (ZhongXianMieDengGeShu > 0):
- LuoDengJianGeShiJian = (0.5 / ZhongXianMieDengGeShu)
- else:
- LuoDengJianGeShiJian = 0.5
- time.sleep(LuoDengJianGeShiJian)
- neopixel_range(ws2812_P13, (DengLieBiao[i]), (DengLieBiao[i]), (0, 0, 0))
- j = 0
- while not (((j == len(LuoDengLieBiao)) or ((LiangMieLieBiao[(LuoDengXuHao[j])]) == 1))):
- neopixel_range(ws2812_P13, (LuoDengLieBiao[j]), (LuoDengLieBiao[j]), (255, 0, 0))
- time.sleep(LuoDengJianGeShiJian)
- neopixel_range(ws2812_P13, (LuoDengLieBiao[j]), (LuoDengLieBiao[j]), (0, 0, 0))
- j = (j + 1)
- if (j == 8):
- LiangMieLieBiao[(LuoDengXuHao[7])]=1
- neopixel_range(ws2812_P13, 232, 232, (255, 0, 0))
- else:
- LuoDengDuiXing = (LuoDengDui[(j - 1)])
- LuoDengDuiXingDengGeShu = len(LuoDengDuiXing)
- LuoDengDuiXuHaoXing = (LuoDengDuiXuHao[(j - 1)])
- LuoDengDuiXuHaoXingDengGeShu = len(LuoDengDuiXuHaoXing)
- k = (LuoDengDuiXingDengGeShu - 1)
- for index in range(LuoDengDuiXingDengGeShu):
- if ((LiangMieLieBiao[(LuoDengDuiXuHaoXing[k])]) == 0):
- if (k == 0):
- neopixel_range(ws2812_P13, (LuoDengDuiXing[k]), (LuoDengDuiXing[k]), (255, 0, 0))
- LiangMieLieBiao[(LuoDengDuiXuHaoXing[k])]=1
- break
- if ((k > ((math.floor((LuoDengDuiXingDengGeShu / 2))) - 1)) and ((LiangMieLieBiao[(LuoDengDuiXuHaoXing[((math.floor((LuoDengDuiXingDengGeShu / 2))) - 1)])]) == 0)):
- pass
- else:
- neopixel_range(ws2812_P13, (LuoDengDuiXing[k]), (LuoDengDuiXing[k]), (255, 0, 0))
- time.sleep(0.01)
- neopixel_range(ws2812_P13, (LuoDengDuiXing[k]), (LuoDengDuiXing[k]), (0, 0, 0))
- else:
- neopixel_range(ws2812_P13, (LuoDengDuiXing[(k + 1)]), (LuoDengDuiXing[(k + 1)]), (255, 0, 0))
- LiangMieLieBiao[(LuoDengDuiXuHaoXing[(k + 1)])]=1
- break
- k = (k - 1)
- i = (i + 1)
- oled.fill(0)
- display_font(font.digiface_44, (str(Tian)), 0, 0, True, 2)
- display_font(font.digiface_21, (str(Shi)), 0, 45, True, 2)
- display_font(font.digiface_21, (str(FenZhong)), 55, 45, True, 2)
- display_font(font.digiface_21, (str(Miao)), 103, 45, True, 2)
- oled.show()
- if (Miao == 0):
- if (FenZhong == 0):
- if (Shi == 0):
- if (Tian == 0):
- oled.DispChar("今天开幕", 42, 22, 1)
- oled.show()
- break
- else:
- Tian = (Tian - 1)
- Shi = 23
- else:
- Shi = (Shi - 1)
- FenZhong = 59
- else:
- FenZhong = (FenZhong - 1)
- Miao = 59
- else:
- Miao = (Miao - 1)
- while not (((time.ticks_ms() - YunXingShiJian) > 1000)):
- pass
-
复制代码
【演示视频】
|