Search code examples
pausebbc-microbit

microbit why blinking leds is so slow?


Using this basic example code from microbit, blinking heart, I tried to change the delay of blinking with the pause argument. However, the minimum actual blinking frequency is around 500ms, no matter what value i put.

Do you know why, and how I can achieve much faster blinking with led patterns (like show_icon or show_leds function).

def on_forever():
    basic.show_icon(IconNames.HEART)
    basic.pause(50)
    basic.show_icon(IconNames.SMALL_HEART)
    basic.pause(50)
basic.forever(on_forever)

Thanks.


Solution

  • You have tagged this as micropython but I don't believe that is what you are using. I think you are running with the Python in the MakeCode editor.

    Looking at the help page for for the MakeCode show_icon, it says it is called with:

    def basic.show_icon(icon: IconNames, interval: null): None
    

    with the following details about interval:

    interval (optional), the time to display in milliseconds. default is 600.

    As you were not putting a value for interval it was defaulting to 600 milliseconds which meant your code was putting 650 milliseconds delay between each icon.

    I was able to vary the duration an icon was displayed with the following:

    def on_forever():
        basic.show_icon(IconNames.HEART, 100)
        basic.show_icon(IconNames.SMALL_HEART, 400)
        basic.show_icon(IconNames.HEART, 100)
        basic.show_icon(IconNames.SMALL_HEART, 800)
    
    basic.forever(on_forever)