1

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()
JulianJ
  • 1,259
  • 3
  • 22
  • 52

2 Answers2

2

The documentation for microbit.display.show says:

If loop is True, the animation will repeat forever.

So instead of using loop=True, you need to write your own Python for or while loop that shows one frame from your animation, checks if the button is pressed, and exits the loop if it is.

You'll need to add the time delay yourself within this loop and you'll also need to figure out how to go back to the first frame when you've shown the last one - there's more than one way you could do that.

nekomatic
  • 5,988
  • 1
  • 20
  • 27
2

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)
Oppy
  • 2,662
  • 16
  • 22