Gently Glowing

Yet another tiny microbit program, one that I was convinced I had posted before but apparently not, so here goes.

This is a nice short program that starts with a blank screen and gradually increases and decreases the brightness of the screen to glow gently forever.


from microbit import *

brightness = 0
direction = 1
fresh_time = 200

while True:
    
    brightness += direction
    
    for x in range(5):
        for y in range(5):
            display.set_pixel(x, y, brightness)

    if brightness >= 9 or brightness <= 0:
        direction *= -1

    sleep(fresh_time)    

Setting the time between refreshes turns out to make quite a difference to the smoothness of the animation. Lower light levels in the LEDs looks less convincing if there is a too long a gap between refreshes, ruining the overall effect.