3. Scroll text

3.1. Display.scroll simple version

display.scroll(value)
Scrolls value horizontally on the microbit LED display.
value can be an integer or float (a decimal) or a string (text in quotes).

To scroll the string (in single quotes), ‘Hi’, across the display, use display.scroll('Hi'):

from microbit import *

while True:
    display.scroll('Hi')

To scroll the string (in double quotes), “Hello”, across the display, use display.scroll("Hello"):

from microbit import *

while True:
    display.scroll("Hello")

To scroll the integer, 5, across the display, use display.scroll(5):

from microbit import *

while True:
    display.scroll(5)

To scroll the float, 3.14, across the display, use display.scroll(3.14):

from microbit import *

while True:
    display.scroll(3.14)

Tasks

  1. Write code to scroll your name.

  2. Write code to scroll your age.

  3. Write code to scroll the number of teams in the AFL.

  4. Write code to scroll the chances of getting a tail on a coin throw.

Write code to scroll your name.

from microbit import *

while True:
    display.scroll("Tim")

Write code to scroll your age.

from microbit import *

while True:
    display.scroll(12)

Write code to scroll the number of teams in the AFL.

from microbit import *

while True:
    display.scroll(18)

Write code to scroll the chances of getting a tail on a coin throw.

from microbit import *

while True:
    display.scroll(0.5)

3.2. Display.scroll with delay

display.scroll(value, delay=150)
Scrolls value horizontally on the display.
value can be an integer or float (a decimal) or a string.
The delay parameter controls how fast the text scrolls.
The default delay is 150ms. When no delay is specified the default of 150ms is used.
The delay can be specified with the parameter name as in display.scroll('Hi', delay=150), or just as a number as the second argument as in display.scroll('Hi', 150).

To scroll the string, ‘Hi’, across the display rapidly, use a short delay of 50ms:

from microbit import *

while True:
    display.scroll('Hi', 50)

To scroll the float, 3.14159, across the display slowly, use a long delay of 300ms:

from microbit import *

while True:
    display.scroll(3.14159, delay=300)

To scroll a date as text, “Dec 25”, across the display quickly, use a short delay of 100ms:

from microbit import *

while True:
    display.scroll("Dec 25", delay=100)

Tasks

  1. Write code, using a short delay, to scroll info about the number of people in your family.

  2. Write code, using a short delay, to scroll info about the number of rooms in your house.

  3. Write code, using a short delay, to scroll info about your birth year.

Write code, using a short delay, to scroll info about the number of people in your family.

from microbit import *

while True:
    display.scroll("5 in family", delay=100)

Write code, using a short delay, to scroll info about the number of rooms in your house.

from microbit import *

while True:
    display.scroll("12 rooms", delay=100)

Write code, using a short delay, to scroll info about your birth year.

from microbit import *

while True:
    display.scroll("Born 1987", delay=100)

3.3. scroll at different speeds

The code below uses a shorter delay for the initial text then a longer delay for the main information.
from microbit import *

while True:
    display.scroll('I like to watch', delay=60)
    display.scroll('AFL', delay=120)

Tasks

  1. Modify the code below to display your favourite sport.

from microbit import *

while True:
    display.scroll('I like to play', delay=60)
    display.scroll('table tennis', delay=120)
  1. Modify the code below to display your name and age in years.

from microbit import *

while True:
    display.scroll('My name is', 60)
    display.scroll('?????', 120)
    display.scroll('I am', 60)
    display.scroll('??', 120)

3.4. Display.scroll using variables

Instead of placing an integer, a float or a string directly in the brackets of display.scroll, a variable can be used.
This can make the code easier to read, since the variable value is separate from its use (here it is just being displayed).
from microbit import *

str_value = 'abc'
while True:
    display.scroll(str_value)
from microbit import *

int_value = 123
while True:
    display.scroll(int_value)
from microbit import *

float_value = 0.93
while True:
    display.scroll(float_value)
In the code below, 3 variables are used to hold a string, integer and float.
These variables are then scrolled repeatedly in the while True: loop.
This makes it easy to see and edit the values of the variables being used in the code.
from microbit import *

player = 'Locket'
goals = 1360
goals_per_game = 4.84

while True:
    display.scroll('Player=', 50)
    display.scroll(player, 80)
    display.scroll('Goals=', 50)
    display.scroll(goals, 150)
    display.scroll('Goals per game=', 50)
    display.scroll(goals_per_game, 300)

Tasks

  1. Modify the value of the variables below to display info for another great goal kicker.

    from microbit import *
    
    player = 'Pele'
    goals = 775
    goals_per_game = 0.92
    
    while True:
        display.scroll('Player=', 50)
        display.scroll(player, 100)
        display.scroll('Goals=', 50)
        display.scroll(goals, 150)
        display.scroll('Goals per game=', 50)
        display.scroll(goals_per_game, 300)
    
  2. Modify the code below to display info for a different batsman.

    from microbit import *
    
    batsman = 'Sobers'
    runs = 8032
    ave = 57.8
    
    while True:
        display.scroll('Batsman=', 50)
        display.scroll(batsman, 100)
        display.scroll('Runs=', 50)
        display.scroll(runs, 150)
        display.scroll('Ave=', 50)
        display.scroll(ave, 300)
    
  3. Modify the code below to display info for another great NBA player.

    from microbit import *
    
    player = 'Kobe Bryant'
    points = 33643
    ave = 25.0
    
    while True:
        display.scroll('player=', 50)
        display.scroll(player, 100)
        display.scroll('Points=', 50)
        display.scroll(points, 150)
        display.scroll('Ave=', 50)
        display.scroll(ave, 300)