Skip to content

Instantly share code, notes, and snippets.

@danya02
Last active July 25, 2020 17:05
Show Gist options
  • Save danya02/82cb61617ad590c90f5e to your computer and use it in GitHub Desktop.
Save danya02/82cb61617ad590c90f5e 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(t.microsecond / 10000, 6, hundredths_color)
time.sleep(0.0001)
@magomarkos
Copy link

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.

@danya02
Copy link
Author

danya02 commented Jul 25, 2020

ValueError: Unknown format code 'b' for object of type 'float'

This is probably because you're running this with Python 3, where int / int == float. Back in 2016, the default executable for python was v2.7, so this issue didn't arise.

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