Skip to content

Prompt

Prompt

I would like your help designing a binary clock using NeoPixels with MicroPython The clock will have six columns. Column 1 is for the hours tens digits. Column 2 is for the hours one digits. Column 3 is for the minutes tens digits Column 4 is for the minutes ones digits Column 5 is for the seconds tens digits Column 6 is for the seconds ones digits

Column 6 (seconds ones) starts a index=0 and is 4 pixels high Column 5 (seconds tens) starts index=4 and is 3 pixels high Column 4 (minutes ones) starts at index-7 and is 4 pixels high Column 3 (minutes tens) starts at index=11 and is 3 pixels high Column 2 (hours ones) starts at index=14 and is 4 pixels high Column 1 (hour tens) starts at index=18 and is 2 pixels high

Put the least significant bit at the bottom of the column. Please generate a MicroPython application that shows the time. Use the following preamble.

Preamble

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# Configuration
NEOPIXEL_PIN = 0
NUM_PIXELS = 20  # Total number of pixels (18 + 2)

# Column configuration (start_index, height)
COLUMN_CONFIG = {
    'hours_tens':   (18, 2),  # Column 1: 2 pixels (0-2)
    'hours_ones':   (14, 4),  # Column 2: 4 pixels (0-9)
    'minutes_tens': (11, 3),  # Column 3: 3 pixels (0-5)
    'minutes_ones': (7, 4),   # Column 4: 4 pixels (0-9)
    'seconds_tens': (4, 3),   # Column 5: 3 pixels (0-5)
    'seconds_ones': (0, 4),   # Column 6: 4 pixels (0-9)
}

# Initialize NeoPixels
pixels = NeoPixel(Pin(NEOPIXEL_PIN), NUM_PIXELS)

# Colors (RGB values)
ON_COLOR = (0, 20, 20)    # Cyan, dimmed for less power usage
OFF_COLOR = (0, 0, 0)     # Off

Full Program Generated

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from machine import Pin
from neopixel import NeoPixel
from utime import localtime, sleep

# Configuration
NEOPIXEL_PIN = 0
NUM_PIXELS = 20  # Total number of pixels (18 + 2)

# Column configuration (start_index, height)
COLUMN_CONFIG = {
    'hours_tens':   (18, 2),  # Column 1: 2 pixels (0-2)
    'hours_ones':   (14, 4),  # Column 2: 4 pixels (0-9)
    'minutes_tens': (11, 3),  # Column 3: 3 pixels (0-5)
    'minutes_ones': (7, 4),   # Column 4: 4 pixels (0-9)
    'seconds_tens': (4, 3),   # Column 5: 3 pixels (0-5)
    'seconds_ones': (0, 4),   # Column 6: 4 pixels (0-9)
}

# Initialize NeoPixels
pixels = NeoPixel(Pin(NEOPIXEL_PIN), NUM_PIXELS)

# Colors (RGB values)
ON_COLOR = (0, 20, 20)    # Cyan, dimmed for less power usage
OFF_COLOR = (0, 0, 0)     # Off

def int_to_binary_column(number, num_bits):
    """Convert a number to binary and return list of bits."""
    binary = []
    for i in range(num_bits):
        binary.append(1 if number & (1 << i) else 0)
    return binary  # LSB first

def set_column(start_index, height, number):
    """Set the LEDs for a specific column based on the number."""
    binary = int_to_binary_column(number, height)

    # Set each LED in the column
    for bit_pos in range(height):
        pixel_index = start_index + bit_pos
        pixels[pixel_index] = ON_COLOR if binary[bit_pos] else OFF_COLOR

def update_display(hours, minutes, seconds):
    """Update all columns with current time."""
    # Hours
    set_column(*COLUMN_CONFIG['hours_tens'], hours // 10)
    set_column(*COLUMN_CONFIG['hours_ones'], hours % 10)

    # Minutes
    set_column(*COLUMN_CONFIG['minutes_tens'], minutes // 10)
    set_column(*COLUMN_CONFIG['minutes_ones'], minutes % 10)

    # Seconds
    set_column(*COLUMN_CONFIG['seconds_tens'], seconds // 10)
    set_column(*COLUMN_CONFIG['seconds_ones'], seconds % 10)

    pixels.write()  # Update the NeoPixels

def main():
    print("Binary Clock Started")
    print("Columns from right to left:")
    print("1. Seconds ones (4 bits)")
    print("2. Seconds tens (3 bits)")
    print("3. Minutes ones (4 bits)")
    print("4. Minutes tens (3 bits)")
    print("5. Hours ones (4 bits)")
    print("6. Hours tens (2 bits)")
    print("LSB at bottom of each column")

    while True:
        # Get current time
        t = localtime()
        hours, minutes, seconds = t[3], t[4], t[5]

        # Update the display
        update_display(hours, minutes, seconds)

        # Print current time for debugging
        print(f"{hours:02d}:{minutes:02d}:{seconds:02d}")

        # Wait before next update
        sleep(1)

if __name__ == "__main__":
    main()