We have already used the micro:bit display in our first program. In this section we will learn more of it's features.
:class: important
Througout this tutorial, links to the official documentation will be provided in this green callouts.
All the display functions can be found at the **[BBC micro:bit MicroPython display documentation](https://microbit-micropython.readthedocs.io/en/latest/display.html#module-microbit.display)**.
:class: important
**`microbit.display.scroll(text, delay=150, \*, wait=True, loop=False, monospace=False)`**
Full details can be found at the **[BBC micro:bit MicroPython display.scroll documentation](https://microbit-micropython.readthedocs.io/en/latest/display.html#microbit.display.scroll)**.
Our first program used the scroll()
function to make a string scroll across the display.
:linenos:
Although our example uses strings, the scroll()
function can also display floats, integers and Boolean values.
:class: important
**`microbit.display.show(image)`**
Full details can be found at the **[BBC micro:bit MicroPython display.show documentation](https://microbit-micropython.readthedocs.io/en/latest/display.html#microbit.display.show)**.
The other option to display characters is the show()
function.
Before we can run the code below, we need to:
- stop the micro:bit by clicking on Thonny's stop button
- navigate back to the micro:bit directory and create a new directory called display_show.
- navigate to the display_show directory.
Create a new file called main.py and add the code below.
:linenos:
Predict in detail what you think the program will do. Make sure you are specific in your prediction ("pause for 2 seconds" is better than just "pause"), then run the program.
:class: notice
- **line 1** → comment identifying the project.
- **line 3** → imports all the commands from the `micropython` library.
- **line 5** → sets up the endless loop.
- **line 6** → `display.show()` displays one character at a time.
- `3.14159` → message to be displayed. This can be a string, integer, float or Boolean.
- `delay=500` → puts a 500 millisecond pause after each character
- **line 7** → `display.clear()` changes the value of each pixel to `0` effectively clearing the screen.
- **line 8** → waits 1000 milliseconds before going back to the top of the loop.
Time to modify the code and see what happens:
- Can you make it display a different message? For example:
- Can you change the time between each character? For example:
- using the details in the display.show docs can you make display the same message without the
while True
loop?
:class: important
The micro:bit has a wide range of **[pre-set images](https://microbit-micropython.readthedocs.io/en/latest/image.html#attributes)** that can be used with the show method.
You can also use the display.show
function to display present images.
Again, since we are making a new program, we need to create a new folder called display_images in the micro:bit folder. Then create a new main.py file.
Then add the following code to the file.
:linenos:
Predict in detail what you think the program will do, remember specific, then run the program.
:class: notice
- **line 6** → `display.show(Image.HEART)` shows a heart on the display
- **line 8** → `display.show(Image.HEART_SMALL)` shows a small heart on the display
Time to modify the code:
- Can you change the heartbeat animation to reflect an actual heartbeat? Like this?
- Can you make the display show a clock face progressing from 1 to 12? Like this:
- Can you make the display show a square spinning? Like this:
:class: important
**`microbit.display.set_pixel(x, y, value)`**
Full details can be found at the **[BBC micro:bit MicroPython display.set_pixel documentation](https://microbit-micropython.readthedocs.io/en/latest/display.html#microbit.display.set_pixel)**.
You can also directly control the individual LEDs on the display. The image below shows the coordinate numbers for each of the LEDs:
- top left →
(0.0)
- bottom right →
(4,4)
Each pixel can be set to a value from 0
(off) to 9
(brightest)
To explore this we will make a new program. Create a new folder called display_custom in the micro:bit folder. Then create a new main.py file.
Add the code below, save t an then run in on the micro:bit.
:linenos:
Predict in detail what you think the program will do, remember specific, then run the program.
:class: notice
This code is a bit more complicated.
- first the structure:
- two `for` loops nested inside a `for` loop which is also nested inside a `for` loop, which is nested inside the infinite `while` loop.
- the coordinate loops:
- **line 8** `for` loop → increments the `x` value from `0` to `4`
- **line 9** `for` loop → increments the `y` value from `0` to `4` for each increment of the `x` value
- the coordinates produced by these two loops will proceed:
- for the `x` value of `0` → `y` values `0` to `4`
- for the `x` value of `1` → `y` values `0` to `4`
- for the `x` value of `2` → `y` values `0` to `4`
- for the `x` value of `3` → `y` values `0` to `4`
- for the `x` value of `4` → `y` values `0` to `4`
- the display loops:
- **line 10** `for` loop → increase the LED value from `0` to `9` (off to brightest)
- **line 13** `for` loop → decrease the LED value from `9` to `0` (brightest to off)
- the second `-1` in range(9,-1,-1) → reverse order
Time to modify the code:
-
What happens if you remove both of the
sleep(10)
statements? Why do you think this happens? -
Can you change the code so it moves across the rows rather than down the columns?
- Can you create this smiley face with glasses? The Image class might help
There are other display related functions such as:
- get_pixel → returns the brightness of a given pixel
- on → turns the display on
- off → turns the display off
- is_on → indicates if the display is on
- read_light_level → gives a reading of the ambient light.