BBC micro:bit
Buttons A & B


There are 3 pushbuttons on the micro:bit. One of those is on the rear of the board and is the reset button. Press this to force your micro:bit to restart its scripts. The other two are referred to as Button A and Button B and can be used in your programs.

There are two ways to think about and use pushbuttons. One way is to repeatedly examine the state of the buttons. Each time the microcontroller does this, the button is either pressed or not pressed and instructions can depend on which of these things is true. Another approach is to capture the event, the moment that the button is pressed and carry out instructions when that happens. The code editors for the micro:bit allow for both of these things.

Button State

Here is a simple script to light up the matrix when each of the buttons is pressed.

micro:bit Code

This example does not use ELSE or ELSE IF statements. This means that the conditions in this example are not exclusive - if both of the buttons are pressed at the same time, the script shows one pattern after the other. You can click on the cog in the IF block to use ELSE and ELSE IF

The Code Kingdoms Javascript editor is a little more powerful and allows that kind of control. Here is a similar script created with that editor.

micro:bit Code

As long as we check first for the double button press, we can have 4 different combinations - A, B, A & B, no buttons.

Button Event

Back in the Microsoft Block Editor, we can code for the button events with a script like this,

micro:bit Code

Although, on the surface, this appears to be doing the same work as the Code Kingdoms script, there is a subtle difference in the behaviour of the board. This time, pressing the button causes the pattern to be displayed and a short pause to take place before the screen is cleared.

You have more options for programming button events if you choose the Code Kingdoms editor. You can create code to run when a particular button is released. Experiment with each of the methods and you can find a way to get the button behaviour that you are looking for.