micro:bit How to write code for Buttons

micro:bit buttons

Description

This example shows how to code the buttons on the micro:bit. There’s not much to it, so it’s a great place to start if you are new to the micro:bit and don’t know where to start. In this exercise, we will use the buttons to display something on the LED array.

Here is a video version of the project

 

Programming Language

MakeCode Blocks

Difficulty Level

Beginner

Materials

Optional tools:

  • micro:bit and a usb cord that will connect to your computer and the micro:bit

Designing the Project

Develop the problem statement

The problem statement for this project is this:

How can I code the  buttons to display a value then erase the value?

Write the Algorithm

Here is the algorithm:

  • When Button A pressed:
    • Display a ‘6’
  • When Buttons A and B are pressed at the same time:
    • Erase the LED array

Write and Test the Code

Here is the sample code for this project.

Concepts Involved

Programming Concepts

Event Handling