Question Regarding Micro:bit + Neopixels + Compass

I have just purchased a microbit and am super keen to follow the compass tutorial with neopixels and the micribit. I was wondering it were possible to utilise the neopixel to display the compass direct as set out in the code in this guide but also utilise the microbits inbuilt LED’s to display an animation whilst the compass code ran and used the neopixels?

The code I’m trying to run along with the compass code above is, any help would be greatly appreciated, as I’m am totally new to programing.

def on_forever():

basic.show_leds("""

    . . . . .

    . . . . .

    . . # . .

    . . . . .

    . . . . .

    """)

basic.show_leds("""

    . . . . .

    . . # . .

    . # . # .

    . . # . .

    . . . . .

    """)

basic.show_leds("""

    . . # . .

    . # . # .

    # . . . #

    . # . # .

    . . # . .

    """)

basic.show_leds("""

    . # # # .

    # . . . #

    # . . . #

    # . . . #

    . # # # .

    """)

basic.show_leds("""

    . . . . .

    . . . . .

    . . . . .

    . . . . .

    . . . . .

    """)

basic.show_leds("""

    . . . . .

    . . . . .

    . . . . .

    . . . . .

    . . . . .

    """)

basic.forever(on_forever)

def on_in_background():

while True:

    music.set_volume(10)

    music.play_melody("E B C5 A B G A F ", 214)

control.in_background(on_in_background)

Mod note - I have split the topic into a new one as the question didn’t fully pertain to the original guide.

EDIT:
I have been experimenting with a number of micro:Bits recently.
Getting two pieces of code that work perfectly on their own; to work together can be tricky.

The code you have listed looks like it is Python code from the Microsoft MakeCode App. (excellent learning platform)

Recently I have found some library functions must have a built in delay. Such as show.leds, show,number, show.string, etc. This is a problem, these functions should be avoided in this example.

The compass needs to produce a change immediately; any other code with a delay will cause it to lag. Below is code I have tested to show 2 of your Led animation frames that will not delay other code.

def LedAnimation():
    global LedTime, LedFrame
    if control.millis() > LedTime + 300:
        LedTime = control.millis()
        if LedFrame == 0:
            led.plot(2, 2)
        elif LedFrame == 1:
            basic.clear_screen()
            led.plot(1, 2)
            led.plot(2, 1)
            led.plot(2, 3)
            led.plot(3, 2)
        elif LedFrame == 2:
            basic.clear_screen()
        elif LedFrame == 3:
            basic.clear_screen()
        LedFrame += 1
        if LedFrame > 3:
            LedFrame = 0

When LedAnimation is called; if 300ms have not passed it will exit immediately. Hence it has no inbuilt delay and will not affect the compass program. The extra time to show a frame when 300ms have passed is less than 1ms. In contrast show.leds() take over half a second to execute.

Note: There is an error in the MicroPython code in the Core Electronics example. It converts from -90 to 90 it should be -180 to 180.

Math.map(Heading, 180, -180, 0, 12) is the Python code from MakeCode to use.

Cheers
Jim

1 Like