Roll a ball around with your micro:bit.

First you need to create a variable called ‘y’ which uses an if statement to find out and store where on the y axis of your display the ball should go.

microbit1

Now you need to do the same again with a variable called ‘x’.

microbit2

Then you clear the display and create a dot where the variables ‘y’ and ‘x’ tell you to.

microbit3

 

Next you wrap the entire thing in a forever loop.

microbit4.PNG

 

Download your code to your micro:bit.

Now you can roll your ball around by tilting the micro:bit!

Update

An improved MicroPython version is below. It uses all 25 of the LEDs rather than just 9 and has a delay between updates to make it less jittery.

from microbit import *

while True:
    if accelerometer.get_y() < -600:
        y=0
    elif accelerometer.get_y() < -200:
        y=1
    elif accelerometer.get_y() < 200:
        y=2
    elif accelerometer.get_y() < 600:
        y=3
    else:
        y=4
        
    if accelerometer.get_x() < -600:
        x=0
    elif accelerometer.get_x() < -200:
        x=1
    elif accelerometer.get_x() < 200:
        x=2
    elif accelerometer.get_x() < 600:
        x=3
    else:
        x=4
        
    display.clear()
    display.set_pixel(x, y, 9)
    sleep(200)