# BBC micro:bit Counting

## Introduction

Whilst you are still getting used to the micro:bit and programming, this page gets you to look at how to display numbers on the LED matrix. It also gives us a chance to use the main techniques of the programming language.

## Keeping Score

This example program displays a digit from 0 to 9 on the LED matrix. The user can press the A and B buttons to change the number from 0 to 9.

This program uses quite a range of programming techniques. The first block is a variable. It keeps track of which number is to be displayed. The block assigns a value to the variable. This is the value that we start with when the micro:bit is powered up.

Next we have a loop. A loop is used for repeating blocks of code. In this case, the loop is called forever. We call this an infinite loop. It never stops repeating.

The next two sections are events. The micro:bit constantly 'listens' for when these things happen.

The two button events have an IF block. This is an example of a selection statement. These statements are used to check that we are not going to reduce the counter below 0 or above 9.

## For Loop

A For loop can be used to keep count for you. This program makes use of that to count from 0 to 9 and back again in an infinite loop.

## Challenges

1. The obvious challenge is to get some sort of stopwatch set up with the micro:bit. You'd want to work with more than a single digit. Press one button to start the watch and another to stop. Have the micro:bit scroll the end time repeatedly across the matrix.
2. In the input section of the block editor. There is a block called running time. If you set a variable to store this value when you start a timer, you will know how much time has passed since the button was pressed. You could use this to make your stopwatch work or use it for some sort of reaction timer. Show a countdown, and then make the micro:bit pause a random amount of time. Then show an image. When the user next presses the button, check the running time again and subtract the one you stored at the start. This will tell you how quick their reactions were.