4. Words

Words uses button pressing and tilting to build a word to scroll on the display.
This could then be sent by radio.

4.1. Chr Syntax

Words requires a list of characters to choose from.
Rather than typing them out, the quickest way to get the alphabet in a list is to use ASCII numbers for the letters and convert them using the chr function.
The chr function converts an ASCII number to its a character.
e.g chr(65) gives ‘A’
chr(number)
The chr() function returns the character that represents the specified unicode number.
Standard ASCII values can be used as well.

4.2. Producing character lists via list comprehension

Instead of typing out character lists in full, they can by quickly generated by code.
The list comprehension, char_nos = [i for i in range(65, 91)], makes a list of the ASCII code numbers for the upper case letters from 65 to 90.
[65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90]
The list comprehension, chars = [chr(i) for i in char_nos], takes that list of numbers and use the chr function to get the corresponding character for that ASCII number.
from microbit import *

char_nos = [i for i in range(65, 91)]  # upper case letters
chars = [chr(i) for i in char_nos]
print(chars)
This produces the upper case list: [“A”, “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”, “J”, “K”, “L”, “M”, “N”, “O”, “P”, “Q”, “R”, “S”, “T”, “U”, “V”, “W”, “X”, “Y”, “Z”]

To work out the number range needed for the tasks below,

Tasks

  1. Modify the code to produce a list of the lower case letters.

  2. Modify the code to produce a list of the digits.

  3. Modify the code to produce a list of the common punctuation.

Modify the code to produce a list of the lower case letters.

from microbit import *

char_nos = [i for i in range(97, 123)]  # lower case letters
chars = [chr(i) for i in char_nos]
print(chars)

[“a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”, “i”, “j”, “k”, “l”, “m”, “n”, “o”, “p”, “q”, “r”, “s”, “t”, “u”, “v”, “w”, “x”, “y”, “z”]

Modify the code to produce a list of the digits.

from microbit import *

char_nos = [i for i in range(48, 58)]  # digits
chars = [chr(i) for i in char_nos]
print(chars)

[“0”, “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”]

Modify the code to produce a list of the common punctuation.

from microbit import *

char_nos = [i for i in range(32, 48)]  # punctuation and symbols
chars = [chr(i) for i in char_nos]
print(chars)

[” “, “!”, ‘”’, “#”, “$”, “%”, “&”, “’”, “(”, “)”, “*”, “+”, “,”, “-”, “.”, “/”]


4.3. Code design

Show an arrow to the A-button, suggesting to press the A-button to start.
The special use of a = button_a.was_pressed() clears the A-button presses so it can be checked again in the other functions.
get_string() calls get_char() to add characters to the word string as long as the B-button hasn’t been pressed, otherwise it returns the word string, usertext.
get_char() starts at the middle character.
get_char() loops until the A-button is pressed, and when it is, it returns the current character.
Tilting left or right changes the character available to be chosen. Press the A-button to add it to the word.
The code below is scaffolded, but incomplete.
Press A to start
Tilt left or right to change the character.
Press A to add the character.
Press B to finish the word and scroll it.
from microbit import *


chars = []
max_char_index = ............
middle_index = ..............

def get_char():
    current = middle_index
    display.show(chars[........])
    # the while-loops runs until A-button is pressed
    while button_a.was_pressed() is False:
        # pressing B doesn't add a character but returns ''get_string
        if button_b.is_pressed():
            return ""
        if accelerometer.get_x() > 300:
            current .......
        elif accelerometer.get_x() < -300:
            current .......
        current = max(0, min(current, max_char_index))
        display.show(chars[.......])
        sleep(330)
    # A-button was pressed so return chosen character
    return chars[........]


def get_string():
    usertext = ""
    # continue adding characters if B-button has not been pressed
    while button_b.was_pressed() is False:
        usertext ...... get_char()
    # B-button was presed, return final word so it can be scrolled
    return usertext


while True:
    display.show(Image.ARROW_W)
    # press A to start
    if button_a.is_pressed():
        display.clear()
        sleep(1000)
        # clear A-button pressing so it can be checked for being pressed again in get_string
        a = button_a.was_pressed()
        currentWord = ...........()
        display.scroll(...........)
    sleep(330)

Tasks

  1. Create the code.

from microbit import *


chars = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
        "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
max_char_index = len(chars) - 1
middle_index = int(max_char_index/2)

def get_char():
    current = middle_index
    display.show(chars[current])
    # the while-loops runs until A-button is pressed
    while button_a.was_pressed() is False:
        # pressing B doesn't add a character but returns ''get_string
        if button_b.is_pressed():
            return ""
        if accelerometer.get_x() > 300:
            current += 1
        elif accelerometer.get_x() < -300:
            current -= 1
        current = max(0, min(current, max_char_index))
        display.show(chars[current])
        sleep(330)
    # A-button was pressed so return chosen character
    return chars[current]


def get_string():
    usertext = ""
    # continue adding characters if B-button has not been pressed
    while button_b.was_pressed() is False:
        usertext += get_char()
    # B-button was presed, return final word so it can be scrolled
    return usertext


while True:
    display.show(Image.ARROW_W)
    # press A to start
    if button_a.is_pressed():
        display.clear()
        sleep(1000)
        # clear A-button pressing so it can be checked for being pressed again in get_string
        a = button_a.was_pressed()
        currentWord = get_string()
        display.scroll(currentWord)
    sleep(330)

Tasks

  1. Modify the code to use lower case letters.

  2. Modify the code to use numbers instead of letters.

  3. Modify the code to add tilting in the y direction to be able to choose the vowels directly.

  4. Modify the code to use tilting in the x direction for uppercase; tilting in the y direction for lowercase.

Modify the code to use lower case letters.

from microbit import *
chars = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
    "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
]
max_char_index = len(chars) - 1
middle_index = int(max_char_index/2)

def get_char():
    current = middle_index
    display.show(chars[current])
    # the while-loops runs until A-button is pressed
    while button_a.was_pressed() is False:
        # pressing B doesn't add a letter but returns ''
        if button_b.is_pressed():
            return ""
        if accelerometer.get_x() > 300:
            current += 1
        elif accelerometer.get_x() < -300:
            current -= 1
        current = max(0, min(current, max_char_index))
        display.show(chars[current])
        sleep(330)
    # A-button was pressed so return chosen letter
    return chars[current]


def get_string():
    usertext = ""
    # continue adding letters if B-button has not been pressed
    while button_b.was_pressed() is False:
        usertext += get_char()
    # B-button was presed, return final word so it can be scrolled
    return usertext


while True:
    display.show(Image.ARROW_W)
    # press A to start
    if button_a.is_pressed():
        display.clear()
        sleep(1000)
        # clear A-button pressing so it can be checked for being pressed again in get_string
        a = button_a.was_pressed()
        currentWord = get_string()
        display.scroll(currentWord)
    sleep(330)

Modify the code to use numbers instead of letters.

from microbit import *


chars = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
max_char_index = len(chars) - 1
middle_index = int(max_char_index / 2)


def get_char():
    current = middle_index
    display.show(chars[current])
    # the while-loops runs until A-button is pressed
    while button_a.was_pressed() is False:
        # pressing B doesn't add a letter but returns back to
        if button_b.is_pressed():
            return ""
        if accelerometer.get_x() > 300:
            current += 1
        elif accelerometer.get_x() < -300:
            current -= 1
        current = max(0, min(current, max_char_index))
        display.show(chars[current])
        sleep(330)
    # A-button was pressed so return chosen letter
    return chars[current]


def get_string():
    usertext = ""
    # continue adding letters if B-button has not been pressed
    while button_b.was_pressed() is False:
        usertext += get_char()
    # B-button was presed, return final word so it can be scrolled
    return usertext


while True:
    display.show(Image.ARROW_W)
    # press A to start
    if button_a.is_pressed():
        display.clear()
        sleep(1000)
        # clear A-button pressing for checking again in get_string
        a = button_a.was_pressed()
        currentWord = get_string()
        display.scroll(currentWord)
    sleep(330)

Modify the code to add tilting in the y direction to be able to choose the vowels directly.

from microbit import *


chars = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
        "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
max_char_index = len(chars) - 1
middle_index = int(max_char_index / 2)
vowels = ["A", "E", "I", "O", "U"]


def get_char():
    current = middle_index
    vowel_current = 2
    # flag 0 for current and 1 for vowel_current
    change_flag = 0
    while button_a.was_pressed() is False:
        if button_b.is_pressed():
            return ""
        if accelerometer.get_y() > 300:
            vowel_current += 1
            change_flag = 1
        elif accelerometer.get_y() < -300:
            vowel_current -= 1
            change_flag = 1
        elif accelerometer.get_x() > 300:
            current += 1
            change_flag = 0
        elif accelerometer.get_x() < -300:
            current -= 1
            change_flag = 0
        if change_flag == 0:
            current = max(0, min(current, max_char_index))
            display.show(chars[current])
        else:
            vowel_current = max(0, min(vowel_current, 4))
            display.show(vowels[vowel_current])
        sleep(330)
    if change_flag == 0:
        return chars[current]
    else:
        return vowels[vowel_current]


def get_string():
    usertext = ""
    while button_b.was_pressed() is False:
        usertext += get_char()
    return usertext


while True:
    display.show(Image.ARROW_W)
    if button_a.is_pressed():
        display.clear()
        sleep(1000)
        a = button_a.was_pressed()
        currentWord = get_string()
        display.scroll(currentWord)
    sleep(330)

Modify the code to use tilting in the x direction for luppercase; tilting in the y direction for lowercase.

from microbit import *


chars = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
        "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
max_char_index = len(chars) - 1
middle_index = int(max_char_index / 2)
chars2 = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
        "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
max_char_index2 = len(chars2) - 1
middle_index2 = int(max_char_index2 / 2)

def get_char():
    current = middle_index
    current2 = middle_index2
    # flag 0 for current and 1 for current2
    change_flag = 0
    while button_a.was_pressed() is False:
        if button_b.is_pressed():
            return ""
        if accelerometer.get_y() > 300:
            current2 += 1
            change_flag = 1
        elif accelerometer.get_y() < -300:
            current2 -= 1
            change_flag = 1
        elif accelerometer.get_x() > 300:
            current += 1
            change_flag = 0
        elif accelerometer.get_x() < -300:
            current -= 1
            change_flag = 0
        if change_flag == 0:
            current = max(0, min(current, max_char_index))
            display.show(chars[current])
        else:
            current2 = max(0, min(current2, max_char_index2))
            display.show(chars2[current2])
        sleep(330)
    if change_flag == 0:
        return chars[current]
    else:
        return chars2[current2]


def get_string():
    usertext = ""
    while button_b.was_pressed() is False:
        usertext += get_char()
    return usertext


while True:
    display.show(Image.ARROW_W)
    if button_a.is_pressed():
        display.clear()
        sleep(1000)
        a = button_a.was_pressed()
        currentWord = get_string()
        display.scroll(currentWord)
    sleep(330)