Scrolling text across the display is a very useful way to get information from your micro:bit program. When you have your micro:bit on your cyber:bot robot, this is a good way to display values from the robot's sensors.
To scroll text across the display, use the aptly named display.scroll method. The text you want to display goes inside quotation marks " ", and that goes inside the method's parentheses ( ).
Example script: hello
- Connect your micro:bit module to your computer with its USB cable.
- In a Google Chrome or Microsoft Edge browser, go to python.microbit.org to open the micro:bit Python Editor.
- Enter, name, and save the hello script.
(See Save & Edit Scripts.) - Click Send to micro:bit.
(See Flash Scripts with Python Editor.)
#hello from microbit import * display.scroll("Hello")
You should see the word Hello scroll across the display one character at a time.
- Did you miss it? Press the reset button on the underside of the micro:bit module to see the Hello message repeat.
How the hello script works
The first line, #hello is a comment. It is just there for you to read the script's name, but is ignored by the micro:bit. We will look at comments again later!
The second line, from microbit import *, fetches the code that knows how to run the display methods. Every complete example script in these tutorials will have at least one import * statement at the top.
The third line makes the text appear on the display. The "Hello" text in quotation marks is a parameter of the display.scroll method, and so goes inside its parentheses.
Try This: Speed Control
The text you want to display isn't the only parameter you can use with display.scroll. You can control the speed at which the text scrolls across the display by adding a an additional parameter to the display.scroll method called delay. The larger the value for delay, the slower the text will scroll.
- Set the script name to hello_goodbye.
- In the first display.scroll statement, add delay = 500 as shown below. Don't forget the comma between the parameters!
- On a new line, add a second display.scroll statement that says Goodbye, with a delay of 500.
- Save the modified script.
- Click the Send to micro:bit button.
#hello_goodbye from microbit import * display.scroll("Hello", delay = 500) display.scroll("Goodbye", delay = 150)
You should see the word Hello scroll across the display slowly, then the word Goodbye scroll across the screen quickly.
Be cautious when copying and pasting code with quotation marks! Some types of documents use smart-quote characters “ and ” that will result in a syntax error if you try to use them in a script. If you are receiving a syntax error after copying and pasting code with quotation marks, try erasing and retyping the quotation marks directly in your programming environment, then re-flash the program.
Your Turn: Your Name
- Write a script to scroll your first name across the display.
- Modify your script to scroll your first name across the display quickly, then scroll your last name across the display slowly.