To help students understand the behaviour of physical buttons, we have intentionally not included an A+B button in the micro:bit Python simulator. 


In comparison, MakeCode includes an A+B button. MakeCode is designed for new programmers and cleverly uses events to keep things simple. However, once learners progress to Python, understanding some of the complexity relating to buttons becomes important. 


In MakeCode, events, like pressing button A, trigger parts of the code to run. These events are easy for new programmers to understand. In MakeCode pressing A+B is just another event, but it is a little more complicated than that behind the scenes. It is nearly impossible to press buttons A and B at precisely the same time. So, if MakeCode was less clever and button A was pressed a fraction of a second before button B any code linked to the event ‘on button A pressed’ would run instead of the ‘on button A+B pressed’ event. To avoid this, MakeCode includes a small-time delay to check if both buttons have been pressed. This allows the A+B pressed event to be available in the code and the simulator (whether you are using Blocks, JavaScript or Python, as they all use the events simplification in MakeCode).

 

The following sample code shows the ‘on button A+B pressed’ event in MakeCode simulators Blocks, JavaScript, and Python.


MakeCode Blocks


 



MakeCode JavaScript 

input.onButtonPressed(Button.AB, function () {
    basic.showString("A+B")
})
basic.forever(function () {
  
})

MakeCode Python 

def on_button_pressed_ab():
    basic.show_string("A+B")
input.on_button_pressed(Button.AB, on_button_pressed_ab)

def on_forever():
    pass
basic.forever(on_forever)


Python does not normally have a concept of events, instead, it is up to the programmer to consider how to make something happen when a button is pressed (e.g., using while true and if loops). Making something happen when A and B are pressed together requires careful consideration given that one of those buttons will likely be pressed just before the other in real life. If we included an A+B button in the simulator we would simplify the scenario for testing and students would often find their code works in the simulator but not on the real micro:bit 


We hope our decision not to include an A+B button in the simulator helps learners to consider in more detail how physical hardware, like buttons, interact with software, preparing learners more for the challenges of programming applications and systems in the real world. 


The following sample program checks first to see if A+B are both pressed, (at the time when that piece of code runs), then if not, if A or B have been pressed, and adds a small (sleep) delay before checking again, to make it work better in practice:


micro:bit Python Editor

# Imports go at the top
from microbit import *

# Code in a 'while True:' loop repeats forever
while True:
    if button_a.is_pressed() and button_b.is_pressed():
        display.scroll("A+B")
    elif button_a.is_pressed():
        display.scroll("A")
    elif button_b.is_pressed():
        display.scroll("B")
    sleep(300)

To test this in the simulator, expand the buttons section of the simulator and slide the ‘Hold’ option to the on position for one of the buttons (the event for that button will trigger). Then ‘Hold’ the other button (or manually hold down the other button). Holding both buttons is the only way to simulate A+B. 


 

 

This will work on the ‘real’ micro:bit if you hold A and B down too. However, if you release either button before the code has run it will not, and sometimes you might see A or B appear first (e.g. if you are not quite quick enough at pushing both buttons together, or the code just happened to be checking the elif statement exactly when the first button was pressed). 



For more information on buttons, micro:bit hardware, micro:bit Python Editor, and how MakeCode works: 


Watch a short YouTube video that explains the button (hardware) on the micro:bit in more detail.

See MakeCode documentation explaining the term Event Handler and event on button pressed.

See MakeCode documentation for micro:bit hardware here. 

See micro:bit technical hardware documentation for buttons here. 

See Python Editor: Guide to micro:bit Python Editor v3.