Skip to content

Instantly share code, notes, and snippets.

@douglasstarnes
Created April 16, 2020 04:57
Show Gist options
  • Save douglasstarnes/ce88add608356985d06fffd19ce1b656 to your computer and use it in GitHub Desktop.
Save douglasstarnes/ce88add608356985d06fffd19ce1b656 to your computer and use it in GitHub Desktop.
Adaptive Night Light for BBC microBit using Micropython
from microbit import *
LIGHT_LEVEL = 255 / 4
ALL_PIXELS = set(list(range(25)))
def square(x, y, side):
pixels_on = set()
for x_offset in range(side):
for y_offset in range(side):
pixels_on.add(y_offset * 5 + x_offset + x + (y * 5))
pixels_off = ALL_PIXELS.difference(pixels_on)
for pixel in ALL_PIXELS:
if pixel in pixels_on:
display.set_pixel(pixel % 5, pixel // 5, 9)
else:
display.set_pixel(pixel % 5, pixel // 5, 0)
while True:
if display.read_light_level() < LIGHT_LEVEL:
square(0, 0, 5)
elif display.read_light_level() < LIGHT_LEVEL * 2:
square(1, 1, 3)
elif display.read_light_level() < LIGHT_LEVEL * 3:
square(2, 2, 1)
else:
display.clear()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment