Search code examples
micropythonbbc-microbit

Display image before event in micropython


I'm trying to use a BBC:Microbit to display a flash for 1 second on it's led's when button a is pressed. This works but I want it to display an animation(standby) whilst it waits for the button to be pressed. The code below only shows the standby image and does not run the rest of the code when button a is pressed. What have I got wrong? Thanks.

from microbit import *

standby1 = Image("00000:"
             "00000:"
             "90000:"
             "00000:"
             "00000")

standby2 = Image("00000:"
             "00000:"
             "09000:"
             "00000:"
             "00000")

standby3 = Image("00000:"
             "00000:"
             "00900:"
             "00000:"
             "00000")

standby4 = Image("00000:"
             "00000:"
             "00090:"
             "00000:"
             "00000")

standby5 = Image("00000:"
             "00000:"
             "00009:"
             "00000:"
             "00000")

all_leds_on = Image("99999:"
             "99999:"
             "99999:"
             "99999:"
             "99999")

standby = [standby1, standby2, standby3, standby4, standby5, standby4, standby3, standby2]

display.show(standby, loop=True, delay=100)#Show standby LEDS on a loop

#Wait for button a to be pressed
while True:

    if button_a.was_pressed():
        sleep(1000)#pause program for 1 second
        display.show(all_leds_on) #Turn on LEDS for 1 second
        sleep(1000)#pause program for 1 second
        display.clear()

Solution

  • As nekomatic said, replacing loop=True is a solution. Please find some example code below.

    Event handlers would be a cleaner way to handle button presses. The micropython implementation on the microbit is missing the event handlers that the full implementation of micropython on e.g. pyboards have. Event handlers are available in the C compilers available for the microbit.

    from microbit import *
    
    standby1 = Image("00000:"
                 "00000:"
                 "90000:"
                 "00000:"
                 "00000")
    
    standby2 = Image("00000:"
                 "00000:"
                 "09000:"
                 "00000:"
                 "00000")
    
    standby3 = Image("00000:"
                 "00000:"
                 "00900:"
                 "00000:"
                 "00000")
    
    standby4 = Image("00000:"
                 "00000:"
                 "00090:"
                 "00000:"
                 "00000")
    
    standby5 = Image("00000:"
                 "00000:"
                 "00009:"
                 "00000:"
                 "00000")
    
    all_leds_on = Image("99999:"
                 "99999:"
                 "99999:"
                 "99999:"
                 "99999")
    
    def flash_all():
        ''' Flash all LEDs on the display. '''
        display.show(all_leds_on)
        sleep(1000)
        display.clear()
    
    standby = [standby1, standby2, standby3, standby4, standby5, 
            standby4, standby3, standby2]
    
    while True:
        for image in standby:
            if button_a.was_pressed():
                flash_all()
            display.show(image)
            sleep(100)