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.
Lesson 6 – micro:bit Display
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.
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 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.
In Python, range(5) produces a list of numbers from 0 to 4: [0,1,2,3,4]. The list contains all the numbers between 0 and the parameter of the range() function. The list includes 0 but does not include the parameter.
The for loop creates a variable named i. The for loop sets this variable equal to the first value on the list (0) and then executes the four indented lines of code within the loop. These four lines of code make the beak blink green once. Then the program goes back to the top of the for loop, sets i equal to the next number in the list (1), and executes the four lines of code within the loop, blinking the beak again. This process continues until i is equal to 4, the last number in the list. When i is 4, the program executes the lines of code inside the loop and then moves on to whatever unindented code follows the for loop.
Use a for loop to make the Finch move forward and backward six times.
Write a program that gradually draws a square on the micro:bit display. Hint: You will need to use multiple for loops.
To display different patterns on the micro:bit, you can also use the setDisplay() method. This method sets the LED array of the micro:bit to display a pattern defined by a list of length 25 (you will learn more about lists in Python in a later lesson). Each value in the list 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 Python, but it makes the code easier to read.
Try out the code above. Then modify the code to display a smiley face on the micro:bit display.
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.
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°.)
- 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 str() to convert a number to a string.)
For an added challenge, give the user an error if they enter a number less than 2.