Lesson 12 – Arrays with Hummingbird

Hummingbird Components

1 Light Sensor, 1 Single Color LED

Java Concepts

Arrays

Teacher Materials

Get Access

In lesson 3, you learned to light up individuals LEDs on the micro:bit. You can also display patterns and small pictures on the micro:bit array using the setDisplay() method. This method can set all 25 LEDs on the micro:bit at one time. The setDisplay() method takes one parameter, an array of length 25.

In Java, an array is an ordered list of values. For example, this line of code declares an array of integers that has 25 elements.

To use setDisplay(), you must declare an array variable for each pattern that you want to display. Each array must contain 25 values, and 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, etc. For example, this code lights up the top and bottom rows on the micro:bit display.

Exercise 1

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

Exercise 2

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.

You can also use arrays with other Hummingbird components. If you are playing a series of notes with the buzzer, you can create an array to hold the notes, and then use a for loop to move through the array. You can access an element of the array using square brackets. noteArray[0] is the first element in the array. The last element of an array with five elements is noteArray[4].

Exercise 3

Try out this sample code, and then modify it to play a song of your own. For some songs to try, see this website (if you read music) or this one (if you don’t).

Exercise 4

In Exercise 3, every note in the array plays for the same number of beats. Now use two arrays to vary both the notes and the number of beats for each note.

Extra Challenge

You can also use arrays to collect data from the Hummingbird sensors. Use this code to measure 15 light sensor values over 15 seconds and store them in an array. Then use a second for loop to move through the values in the array and use them to set the brightness of an LED. Now you can use the light sensor to “program” the LED! Try modifying the code to control other Hummingbird outputs based on an array of sensor values.

Back to Top