Skip to content

Instantly share code, notes, and snippets.

@magomarkos
Forked from danya02/binary_clock.py
Last active September 19, 2022 16:57
Show Gist options
  • Save magomarkos/3fe07b37550fd8d04c0ab9ac71e93236 to your computer and use it in GitHub Desktop.
Save magomarkos/3fe07b37550fd8d04c0ab9ac71e93236 to your computer and use it in GitHub Desktop.
Binary clock using a RPi and a Sense HAT.
#!/usr/bin/env python
from sense_hat import SenseHat
import time, datetime
hat = SenseHat()
year_color = (0, 255, 0)
month_color = (0, 0, 255)
day_color = (255, 0, 0)
hour_color = (0, 255, 0)
minute_color = (0, 0, 255)
second_color = (255, 0, 0)
hundredths_color = (127, 127, 0)
off = (0, 0, 0)
hat.clear()
def display_binary(value, row, color):
binary_str = "{0:8b}".format(value)
for x in range(0, 8):
if binary_str[x] == '1':
hat.set_pixel(x, row, color)
else:
hat.set_pixel(x, row, off)
while True:
t = datetime.datetime.now()
display_binary(t.year % 100, 0, year_color)
display_binary(t.month, 1, month_color)
display_binary(t.day, 2, day_color)
display_binary(t.hour, 3, hour_color)
display_binary(t.minute, 4, minute_color)
display_binary(t.second, 5, second_color)
display_binary(int(t.microsecond / 10000), 6, hundredths_color)
time.sleep(0.0001)
@magomarkos
Copy link
Author

Forked project to fix a bug I was getting:

Traceback (most recent call last):
File "./binary_clock.py", line 36, in
display_binary(t.microsecond / 10000, 6, hundredths_color)
File "./binary_clock.py", line 21, in display_binary
binary_str = "{0:8b}".format(value)
ValueError: Unknown format code 'b' for object of type 'float'

Adjusted line 35 to:
display_binary(int(t.microsecond / 10000), 6, hundredths_color)

After fix code runs on my end with no issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment