# https://wokwi.com/projects/415039906347896833
from machine import Pin
from machine import RTC
from time import sleep
from neopixel import NeoPixel
# 數字顯示的 8x8 點矩陣,每個數字對應一個 8x8 的二進位矩陣
digits = {
0: [0b11111111, 0b10000001, 0b10000001, 0b10000001, 0b10000001, 0b10000001, 0b10000001, 0b11111111],
1: [0b00111110, 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00101000, 0b00011000, 0b00001000],
2: [0b11111111, 0b10000000, 0b10000000, 0b11111111, 0b00000001, 0b00000001, 0b10000001, 0b11111111],
3: [0b11111111, 0b00000001, 0b00000001, 0b11111111, 0b00000001, 0b00000001, 0b00000001, 0b11111111],
4: [0b00000001, 0b00000001, 0b00000001, 0b11111111, 0b10000001, 0b10000001, 0b10000001, 0b10000001],
5: [0b11111111, 0b00000001, 0b00000001, 0b00000001, 0b11111111, 0b10000000, 0b10000000, 0b11111111],
6: [0b11111111, 0b10000001, 0b10000001, 0b10000001, 0b11111111, 0b10000000, 0b10000000, 0b11111111],
7: [0b00000001, 0b00000001, 0b00000001, 0b00000001, 0b00000001, 0b00000001, 0b10000001, 0b11111111],
8: [0b11111111, 0b10000001, 0b10000001, 0b10000001, 0b11111111, 0b10000001, 0b10000001, 0b11111111],
9: [0b11111111, 0b10000001, 0b00000001, 0b00000001, 0b11111111, 0b10000001, 0b10000001, 0b11111111]
}
#函數:將數字顯示在 LED 矩陣上(左右反轉)
def display_digit(digit,pos):
matrix = digits[digit]
for col in range(8):
for row in range(8):
# 根據對應的二進位值設定每個LED的顏色(例如藍色)
if matrix[row] & (1 << (7 - col)): # 列的順序反轉
pixels[col * 8 + row+pos*64] = (0, 0, 255) # 藍色
else:
pixels[col * 8 + row+pos*64] = (0, 0, 0) # 關閉LED
pixels.write()
# 函數:控制冒號
def display_colon(on):
if on:
colon[0] = (255, 0, 0) # 上點:紅色
colon[1] = (255, 0, 0) # 下點:紅色
else:
colon[0] = (0, 0, 0) # 關閉
colon[1] = (0, 0, 0) # 關閉
colon.write()
# 初始化矩陣用的 NeoPixel(64 顆LED)
pixels = NeoPixel(Pin(13), 64 * 4)
# 初始化冒號用的兩顆 NeoPixel
colon = NeoPixel(Pin(14), 2) # 使用 Pin 4 控制
# 計數器
counter = "12:34"
# 顯示數字與冒號
Src_pos = [0, 1, 3, 4] # 數字部分的索引
Dest_pos = [0, 1, 2,3] # 矩陣上的目標位置
for i in range(len(Src_pos)):
display_digit(int(counter[Src_pos[i]]), Dest_pos[i])
#-----------------------------------
# 初始化 RTC
rtc = RTC()
# 確保已設置 RTC 時間(如果尚未設置,可執行 rtc.datetime((年, 月, 日, 星期, 時, 分, 秒, 微秒)))
rtc.datetime((2024, 11, 20, 1, 15, 30, 45, 0))
# 冒號閃爍
while True:
# 獲取當前時間
current_time = rtc.datetime()
# 提取小時與分鐘
hh = current_time[4] # 第 4 個值是小時
mm = current_time[5] # 第 5 個值是分鐘
# 格式化為 hh:mm
time_str = "{:02}:{:02}".format(hh, mm)
print("Current time:", time_str)
for i in range(len(Src_pos)):
display_digit(int(time_str[Src_pos[i]]), Dest_pos[i])
display_colon(True)
sleep(0.5)
display_colon(False)
sleep(0.5)