Science Oxford Logo

Adding New Features using the Controller's Buttons

Follow these instructions to make use of buttons A-F on your controller.

Step 1

Your joystick sends a radio message whenever a button is pressed.

If you press button A, it sends the message "A", if you press button F, it sends the message "F", and so on.

At the moment, your robot program checks for messages, checks that the message is not None, then tries to split that message and send it to the motors.

Before trying to split the message, you can first check if the message is one of the button presses instead:

    if message is not None:
        if message == "C":
            display.show(Image.HAPPY)
        else:
            message = cut(message)

The above code is not complete - compare it to your current program, and work out where the new parts will need to go.

Be careful of the indentation!

Do you get an image showing up on your robot's micro:bit, when you press button C on your controller?

Step 2

Once you have got one button working, you can add in some others.

Try this code:

        if message == "C":
            display.show(Image.HAPPY)
        elif message == "F":
            display.scroll("Hello!")
        else:
            message = cut(message)

elif is short for else if. It goes between a first if and a final else.

Play around with the different buttons, and think about what you want to happen when you press them!

Maybe play a tune?

Click here to see the example code in full.