## Lesson 6 – micro:bit Display

For loops

#### Teacher Materials

In the last lesson, you learned to use while loops to repeat actions. In this lesson, you will use a different kind of loop, the for loop. You will use this loop with a new Finch output, the micro:bit display. There are 25 tiny lights called LEDs (light-emitting diodes) that are arranged in a 5 by 5 grid on the micro:bit in the Finch tail. You can write programs that use these LEDs to make letters and simple pictures.

### Printing to the micro:bit

To write letters and numbers on the micro:bit, use the print() method. This method takes a String parameter. The string must have 15 or fewer characters and should contain only letters and numbers. The print() method displays the string on the micro:bit LED array one letter at a time. For example, the code below shows the letter ‘H’ then the letter ‘i’ on the micro:bit. There is a pause after the print() command to give the program time to finish displaying the word before moving on to the rest of the program.

#### Exercise 1

Ask the user for their name. The program should then display “Hi” and then the user’s name. Some names may have 10-12 letters, so make sure your program waits long enough to print a long name.

### Setting Individual LEDs

To turn individual micro:bit LEDs on or off, use the setPoint() method. This method takes three parameters. The position of the LED is given by the row and column parameters, which should both be between 1 and 5. The third parameter is the value of the LED, which must be 0 (off) or 1 (on).

#### Exercise 2

Try out this code. It should turn on the LEDs at the corners of the micro:bit display for one second. Then modify this code to light up the LEDs along both diagonals of the display.

### For Loops

To create more complicated effects on the micro:bit display, you can use a for loop. In the last lesson, you used a while loop which repeats the statements inside it as long as a Boolean expression is true. A for loop, on the other hand, repeats the statements inside it a specific number of times. For example, the code below will make the Finch’s beak blink on and off five times.

The for loop creates an integer variable named i. The for loop sets this variable equal to 0 and checks whether i is less than 5. If it is, then the program executes the four lines of code within the curly braces. These four lines of code make the Finch beak blink green once. Then the program goes back to the top of the for loop and increases the value of i  by 1 (i++). It checks again whether i is less than 5 and executes the four lines of code within the loop, blinking the beak again. This process continues until i is equal to 5. When i is equal to 5, the program checks i < 5 and and finds that it is false. The loop ends; the program skips the lines of code inside the loop and moves on to whatever code follows the for loop.

#### Exercise 3

Use a for loop to make the Finch move forward and backward six times.

#### Exercise 4

What do you think this code will do? Make a hypothesis, and then test it out.

#### Exercise 5

Write a program that gradually draws a square on the micro:bit display. Hint: You will need to use multiple for loops.

### Displaying Patterns

To display different patterns on the micro:bit, you can also use the setDisplay() method. This method sets the LEDs of the micro:bit to display a pattern defined by an array of length 25 (you will learn more about arrays in Java in a later lesson). Each value in the array must be 0 (off) or 1 (on). The first five values in the array correspond to the five LEDs in the first row, the next five values to the second row, and so on. For example, this code lights up the top and bottom rows on the micro:bit display. This code shows the numbers for each micro:bit row on a different line. This isn’t required in Java, but it makes the code easier to read.

#### Exercise 6

Try out the code above. Then modify the code to display a smiley face on the micro:bit display.

#### Exercise 7

Use a for loop to blink the micro:bit display between two different patterns. If you blink quickly between different patterns, you can create simple animations like a bunny hopping up and down.

#### Exercise 8

Write a program to make the Finch move in a triangle, pentagon, or other shape. Your program should do the following:

• Ask the user for the number of sides.
• How many turns will the Finch need to make to return to its starting position? Compute the angle for each turn. (Hint: The Finch must turn a total of 360°. The angle you calculate will be type double. You can convert it to an int by calling setTurn() as shown below.)
• Use a for loop to draw the shape.
• As the Finch draws each side, the micro:bit should display the number of the side it is drawing. (Hint: Use Integer.toString(i) to convert a number i to a string.)

For an added challenge, give the user an error if they enter a number less than 2.