BBC micro:bit
Buttons A & B

Introduction

This page shows you a few ways that you can interact with the A & B buttons using MicroPython.

Seeing If A Button Is Pressed

Just as we can with the visual code editors, we can define a main/forever loop and repeatedly check whether or not the buttons are being held down. In this program, pressing the A button will display an L on the matrix, pressing the B button an R.

from microbit import *
while True:
    if button_a.is_pressed():
        display.show("L")
    elif button_b.is_pressed():
        display.show("R")
    else:
        display.clear()

This program also shows you how an IF..ELSEIF...ELSE works in Python. Notice the spelling of elif (short for elseif). You can also see that we can display a string or character on the matrix using the display.show() method.

Button Methods

Looking at the first example, we can see that we refer to the buttons as button_a and button_b. We have access to the following methods for these objects,

is_pressed()returns true if the button is pressed at the time the statement executes
was_pressed()returns true if the button has been pressed since the micro:bit started or the last time that the method was called
get_presses()returns the number of times the button was pressed since the micro:bit started or the last time that the method was called
reset_presses()resets the running total of the button presses

Random Image From A List

This example imports and uses the random module to pick an image at random from the list of built-in images and then display it on the LED matrix.

from microbit import *
import random
built_in_images = [Image.HEART, Image.HEART_SMALL, Image.HAPPY, Image.SMILE, Image.SAD,
Image.CONFUSED, Image.ANGRY, Image.ASLEEP, Image.SURPRISED, Image.SILLY, Image.FABULOUS,
Image.MEH, Image.YES, Image.NO, Image.CLOCK12, Image.CLOCK11, Image.CLOCK10, Image.CLOCK9,
Image.CLOCK8, Image.CLOCK7, Image.CLOCK6, Image.CLOCK5, Image.CLOCK4, Image.CLOCK3,
Image.CLOCK2, Image.CLOCK1, Image.ARROW_N, Image.ARROW_NE, Image.ARROW_E, Image.ARROW_SE,
Image.ARROW_S, Image.ARROW_SW, Image.ARROW_W, Image.ARROW_NW, Image.TRIANGLE,
Image.TRIANGLE_LEFT, Image.CHESSBOARD, Image.DIAMOND, Image.DIAMOND_SMALL, Image.SQUARE,
Image.SQUARE_SMALL, Image.RABBIT, Image.COW, Image.MUSIC_CROTCHET, Image.MUSIC_QUAVER,
Image.MUSIC_QUAVERS, Image.PITCHFORK, Image.XMAS, Image.PACMAN, Image.TARGET, Image.TSHIRT,
Image.ROLLERSKATE, Image.DUCK, Image.HOUSE, Image.TORTOISE,Image.BUTTERFLY,
Image.STICKFIGURE, Image.GHOST, Image.SWORD, Image.GIRAFFE, Image.SKULL,
Image.UMBRELLA, Image.SNAKE]
while True:
    if button_a.is_pressed():
        display.show(random.choice(built_in_images))
        sleep(2000)
    else:
        display.clear()

Random Clock Image

The built-in images also include two collections. They are called ALL_CLOCKS and ALL_ARROWS. They contain all of the images that you would expect to belong to that group. We can display one of the clock images at random using code like this,

from microbit import *
import random
while True:
    if button_a.is_pressed():
        display.show(random.choice(Image.ALL_CLOCKS))
        sleep(2000)
    else:
        display.clear()

Challenges

If you have been working through the MicroPython section in order, you should now have the basic tools for interacting with the buttons and the LED matrix. There are lots of things you might want to do with these things. Here are a few suggestions.

  1. Take the random image program and, instead of defining a list of the built-in image constants, define a set of images that means something. This could be the rock, paper and scissors images or a set of dot patterns for a die roll.
  2. Use the buttons to move a sprite left and right on the matrix. You can use an image object to do this or work directly with the display. This might be the beginning of the code you might use for interaction in a game.
  3. Write a program that displays one of the arrow images when you press a button. There is the basis for an odd game if you do this. If you have a battery pack, press the button and take a step in the direction indicated on the matrix. Keep facing in the same direction as when you started and count how long it takes for you to reach a pre-arranged spot in the room. Don't move if you are stuck against a wall, but count that as a move. If you have no battery pack, you will need two people, one to press the button and shout out the instruction, one to move.
  4. Use button presses to navigate forward and backwards through the list of built-in images or through one of the two collections of themed images (arrows, clocks). Use a variable to store a number from 0 to 11 if you are working with the clocks. You can display the clock you want by referring to it as Image.ALL_CLOCKS[integer variable].