PI-32Baguhan

Binary clock gamit ang Raspberry Pi Pico (6 LED + button)

Walang digit ang orasang ito! Gagawa tayo ng orasan gamit ang Raspberry Pi Pico na nagpapakita ng oras sa binary. Ang 6 LED ay ang mga puwestong 32-16-8-4-2-1; pagsamahin ang mga nakasindi at mababasa mo ang segundo, minuto o oras. Isang button lang ang naglilipat ng field at nagtatakda ng oras, at may dumaraang sinag ng ilaw sa mga LED bawat buong oras.

0.0(0)
Binary clock gamit ang Raspberry Pi Pico (6 LED + button)
Orasang nagsasabi ng oras gamit ang ilaw sa halip na digit: ipinapakita ng anim na LED ang segundo, minuto o oras sa binary. Mula kaliwa pakanan, 32, 16, 8, 4, 2 at 1 ang halaga ng mga LED, at ang kabuuan ng mga nakasindi ang kasalukuyang value. Isang button lang ang naglilipat sa pagitan ng segundo, minuto at oras, pinapayagan ka ng mahabang pindot na itakda ang oras, at bawat buong oras ay may dumaraang sinag ng ilaw sa mga LED.

Video

Circuit diagram

Binary clock gamit ang Raspberry Pi Pico (6 LED + button) - Circuit diagram

Source code

MicroPython
1# ─────────────────────────────────────────
2#   BlueGrays · bluegrays.com
3#   Raspberry Pi Pico Binary Clock (6 LEDs + Button)
4# ─────────────────────────────────────────
5# A clock that shows the time as binary numbers. The 6 LEDs are the
6# 32-16-8-4-2-1 places; the lit LEDs add up to the value. A short press
7# switches between seconds, minutes and hours; a long press sets the
8# field. Every full hour a light sweep runs, and the onboard LED ticks the
9# seconds.
10#
11# Pins:
12#   GP2  -> LED 1 (bit 0), through 220 ohm
13#   GP3  -> LED 2 (bit 1), through 220 ohm
14#   GP4  -> LED 4 (bit 2), through 220 ohm
15#   GP5  -> LED 8 (bit 3), through 220 ohm
16#   GP6  -> LED 16 (bit 4), through 220 ohm
17#   GP7  -> LED 32 (bit 5), through 220 ohm
18#   GP15 -> Button (other leg to GND)
19#   LED  -> Onboard LED (seconds tick)
20#   GND  -> LED cathodes, button
21
22from machine import Pin
23import time
24
25# ============ SETTINGS ============
26START_TIME = (12, 59, 50)  # Start time (h, m, s)
27SEC, MIN, HOUR = range(3)
28START_MODE = SEC       # Field shown at start
29LONG_MS = 800          # Long press time
30DEBOUNCE_MS = 20       # Button debounce time
31MODE_MS = 500          # How long the mode marker shows
32BLINK_MS = 300         # Blink period in set mode
33SET_TIMEOUT_MS = 10000 # Set mode saves after this idle time
34SWEEP_STEP_MS = 60     # Step of the full hour sweep
35SWEEP_ROUNDS = 2       # Number of sweeps
36TICK_MS = 100          # Onboard LED flash length
37LOOP_MS = 5            # Loop step
38
39# In order bit 0 (1) ... bit 5 (32)
40LED_PINS = (2, 3, 4, 5, 6, 7)
41MODE_NAMES = ("SECONDS", "MINUTES", "HOURS")
42# Mode marker: 1, 2 or 3 LEDs from the left (seconds, minutes, hours)
43MODE_MARKS = (0b100000, 0b110000, 0b111000)
44
45
46# ============ HARDWARE ============
47leds = [Pin(gp, Pin.OUT, value=0) for gp in LED_PINS]
48# The name "LED" finds the onboard LED on both the Pico and the Pico W (not a GP pin on the W)
49tick_led = Pin("LED", Pin.OUT, value=0)   # The Pico's onboard LED
50
51
52class Button:
53    # Pull-up button: reads 0 when pressed. update() 1 = pressed, -1 = released
54    def __init__(self, gp):
55        self.pin = Pin(gp, Pin.IN, Pin.PULL_UP)
56        self.down = False
57        self.changed_at = time.ticks_ms()
58
59    def update(self, now):
60        pressed = self.pin.value() == 0
61        if pressed != self.down and time.ticks_diff(now, self.changed_at) >= DEBOUNCE_MS:
62            self.down = pressed
63            self.changed_at = now
64            return 1 if pressed else -1
65        return 0
66
67    def held_ms(self, now):
68        return time.ticks_diff(now, self.changed_at)
69
70
71button = Button(15)
72shown = -1                 # Last pattern on the LEDs
73
74
75def show_bits(pattern):
76    # If the pattern changed, write the 6 LEDs: bit i -> LED i
77    global shown
78    if pattern == shown:
79        return
80    shown = pattern
81    for i, led in enumerate(leds):
82        led.value((pattern >> i) & 1)
83
84
85def binary(v):
86    # 6 digit binary text, e.g. 13 -> 001101
87    return "".join("1" if v >> i & 1 else "0" for i in range(5, -1, -1))
88
89
90# ============ CLOCK ============
91clock_s = (START_TIME[0] * 3600 + START_TIME[1] * 60 + START_TIME[2]) % 86400
92start = time.ticks_ms()
93next_second = time.ticks_add(start, 1000)
94tick_off_at = start
95
96
97def hms():
98    return clock_s // 3600, clock_s // 60 % 60, clock_s % 60
99
100
101def field_value(m):
102    h, mi, s = hms()
103    return (s, mi, h)[m]
104
105
106def time_text():
107    return "%02d:%02d:%02d" % hms()
108
109
110# ============ STATE ============
111mode = START_MODE
112setting = False            # Set mode
113long_done = False          # Long press handled for this press
114marker_until = start       # When the mode marker ends
115sweep_at = None            # Start of the light sweep
116blink_ref = start          # Start of the blink cycle
117last_press = start         # Last press in set mode
118
119
120def report(prefix):
121    v = field_value(mode)
122    print("%s %s: %d = %s  (%s)" % (prefix, MODE_NAMES[mode], v, binary(v), time_text()))
123
124
125def save_setting(now):
126    global setting
127    setting = False
128    print("Time set: %s" % time_text())
129
130
131def on_short(now):
132    global mode, marker_until, clock_s, blink_ref, last_press
133    if setting:
134        # In set mode every short press adds 1 to the selected field
135        h, mi, s = hms()
136        if mode == HOUR:
137            h = (h + 1) % 24
138        else:
139            mi = (mi + 1) % 60
140        clock_s = h * 3600 + mi * 60 + s
141        blink_ref = now
142        last_press = now
143        report("+1")
144    else:
145        mode = (mode + 1) % 3
146        marker_until = time.ticks_add(now, MODE_MS)
147        report("Mode")
148
149
150def on_long(now):
151    global setting, clock_s, next_second, blink_ref, last_press
152    if setting:
153        if mode == MIN:
154            # After setting the minutes, the seconds restart from zero
155            clock_s -= clock_s % 60
156            next_second = time.ticks_add(now, 1000)
157        save_setting(now)
158    elif mode == SEC:
159        # In seconds mode a long press zeroes the seconds (sync to a time signal)
160        clock_s -= clock_s % 60
161        next_second = time.ticks_add(now, 1000)
162        print("Seconds zeroed: %s" % time_text())
163    else:
164        setting = True
165        blink_ref = now
166        last_press = now
167        report("Set")
168
169
170def frame(now):
171    # The LED pattern right now: sweep > mode marker > set (blinking) > value
172    if sweep_at is not None:
173        step = time.ticks_diff(now, sweep_at) // SWEEP_STEP_MS
174        pos = step % 10
175        return 1 << (pos if pos < 6 else 10 - pos)     # back and forth
176    if time.ticks_diff(marker_until, now) > 0:
177        return MODE_MARKS[mode]
178    if setting and time.ticks_diff(now, blink_ref) // BLINK_MS % 2:
179        return 0
180    return field_value(mode)
181
182
183# ============ MAIN LOOP ============
184print("== BlueGrays · bluegrays.com ==")
185print("Pico Binary Clock")
186print("Short press: next field, long: set")
187print("LEDs: 32 16 8 4 2 1")
188report("Mode")
189
190next_loop = time.ticks_ms()
191while True:
192    now = time.ticks_ms()
193
194    # Second counter: each next second is added to the last one, so it never drifts
195    if time.ticks_diff(now, next_second) >= 0:
196        next_second = time.ticks_add(next_second, 1000)
197        clock_s = (clock_s + 1) % 86400
198        tick_led.value(1)
199        tick_off_at = time.ticks_add(now, TICK_MS)
200        if clock_s % 60 == 0:
201            print("Time %s" % time_text()[:5])
202        if clock_s % 3600 == 0 and not setting:
203            sweep_at = now                 # Full hour: light sweep
204    if not setting and time.ticks_diff(now, tick_off_at) >= 0:
205        tick_led.value(0)
206    if sweep_at is not None and time.ticks_diff(now, sweep_at) >= SWEEP_STEP_MS * 10 * SWEEP_ROUNDS:
207        sweep_at = None
208
209    # Button: a short press acts on release, a long press as soon as LONG_MS passes
210    b = button.update(now)
211    if b == 1:
212        long_done = False
213        sweep_at = None                    # A press stops the sweep
214    elif button.down and not long_done and button.held_ms(now) >= LONG_MS:
215        long_done = True
216        on_long(now)
217    elif b == -1 and not long_done:
218        on_short(now)
219
220    if setting:
221        tick_led.value(1)          # Onboard LED on while setting
222        if time.ticks_diff(now, last_press) >= SET_TIMEOUT_MS:
223            save_setting(now)
224
225    show_bits(frame(now))
226
227    # Fixed step: wait until the next 5 ms
228    next_loop = time.ticks_add(next_loop, LOOP_MS)
229    wait = time.ticks_diff(next_loop, time.ticks_ms())
230    if wait > 0:
231        time.sleep_ms(wait)
232    else:
233        next_loop = time.ticks_ms()
234

FAQ

Paano ko babasahin ang binary clock?

Mula kaliwa pakanan, 32, 16, 8, 4, 2 at 1 ang halaga ng mga LED. Pagsamahin ang halaga ng mga nakasinding LED: sa pattern na 001101, nakasindi ang 8, 4 at 1, kaya 13 ang value. Hindi lumalampas sa 23 ang oras, kaya laging madilim ang pinakakaliwang LED sa field ng oras. Ipini-print ng serial monitor ang bawat value kasama ang anyo nito sa binary.

Bakit sapat na ang 6 LED?

Nakakabilang ang 6 na bit mula 0 hanggang 63 (2 sa power na 6 = 64 na value). Hanggang 59 ang segundo at minuto at hanggang 23 ang oras, kaya kasya silang lahat sa 6 na bit. Para makita nang sabay ang oras, minuto at segundo, kakailanganin mo ng hiwalay na row ng LED para sa bawat field (17 LED lahat-lahat).

Nare-reset ba ang orasan pagkatapos mawalan ng kuryente?

Oo. Walang orasang may backup na baterya ang Pico, kaya nagsisimula ang code sa `START_TIME` sa bawat boot. Maitatabi ang oras ng RTC module na may backup na baterya gaya ng DS1307. Sa project na ito, maitatakda mo ang oras at minuto gamit ang button sa loob ng ilang segundo pagkabukas.

Puwede ba akong gumamit ng iba't ibang kulay ng LED?

Oo. Maganda ang sindi ng pula, berde, dilaw at orange na LED sa 3.3 V na may 220 Ω. Mga 3 V ang kailangan ng asul at puting LED at puwedeng mukhang malamlam sa 3.3 V; ibaba sa 100 Ω ang resistor nila. Mas madaling basahin ang orasan kapag iba ang kulay ng matataas na puwesto.