Surreal random micro:bit story printer

This is a slight twist on my original Little Box of Poems I originally made back in 2012. It mashes that up with Papert-inspired random poetry to create surreal haiku-like poems or story starters.

This project uses a BBC micro:bit and a serial thermal printer. Wiring it up is very simple: connect the black data wire on the printer to GND on the micro:bit, and the printer’s yellow data RX wire to pin 8 on the micro:bit (you could use another pin, I just happened to use pin 8). The printer needs 5 volts to power it, so it needs a separate power supply. Luckily USB sockets give 5v, and thanks to this tutorial from the awesome Tanya Fish I chopped up an old unwanted USB lead to make my own source of printer power.

How to wire a serial printer to a micro:bit

Parts list:

  • micro:bit with power source, battery or USB.
  • Some way of connecting pins – edge connector and jumper wires or crocodile clip leads.
  • A serial thermal till-roll printer like this one and some thermal paper.
  • A 5v power source for the printer – an old USB lead chopped up and a USB power source will do nicely.
  • Python program below and here – you can flash it onto the micro:bit using the online micro:bit Python editor or Mu.

The Python program is in two parts – the first part is all the functions to send control codes for different styles of printing – most of these aren’t used in this project but you might have fun playing with them – you can also print different kinds of barcode if you look here.

The second part creates lists of different kinds of words: animal / people nouns, verbs, adverbs, prepositions, adjectives and place nouns. Each time you press button A, it picks a random word from each category and prints them out. You could use this as a story starter, use it as a passphrase or just enjoy the surreal haiku-like poems. Add your own words. How long a story can you get a machine to write? Let me know what you do with this idea, using a printer, 2-line LCD or just the built-in LED display.

Download Python program here.


import microbit
import random
microbit.uart.init(baudrate=19200, bits=8, parity=None, stop=1, tx=microbit.pin8, rx=None)

# Print lines of text in buffer
def thermal_print_ln(msg):
    microbit.uart.write(msg+"\n")

# Send text but don't print - you need to send a
# newline (\n) character to empty buffer & print.
# This enables mixed print modes on same line.
def thermal_print(msg):
    microbit.uart.write(msg)

def doubleHeightOn():
    microbit.uart.write("\x1B\x21\x10") 

def doubleHeightOff():
    global print_mode
    microbit.uart.write("\x1B\x21\x00") 

def smallFontOn():
    microbit.uart.write("\x1B\x21\x01") 

def smallFontOff():
    microbit.uart.write("\x1B\x21\x00") 

def boldOn():
    microbit.uart.write("\x1B\x45\x01") 

def boldOff():
    microbit.uart.write("\x1B\x45\x00") 

def wideOn():
    microbit.uart.write("\x1B\x0E") 

def wideOff():
    microbit.uart.write("\x1B\x14") 

def inverseOn():
    microbit.uart.write("\x1B\x21\x02") 

def inverseOff():
    microbit.uart.write("\x1B\x21\x00") 

def upsideDownOn():
    microbit.uart.write("\x1B\x7B\x01") 

def upsideDownOff():
    microbit.uart.write("\x1B\x7B\x00") 

def underlineOn():
    microbit.uart.write("\x1B\x2D\x02") 

def underlineOff():
    microbit.uart.write("\x1B\x2D\x00") 

def largeFontOn():
    microbit.uart.write("\x1D\x21\x11") 

def largeFontOff():
    microbit.uart.write("\x1D\x21\x00") 

def leftAlign():
    microbit.uart.write("\x1B\x61\x00")

def centreAlign():
    microbit.uart.write("\x1B\x61\x01")

def rightAlign():
    microbit.uart.write("\x1B\x61\x02")

# prints test page
def printerTest():
    microbit.uart.write("\x12\x54")

# resets the printer to default values
def printerReset():
    microbit.uart.write("\x1B\x40")

# press button A to activate demo
# increase printing temperature and time
microbit.uart.write("\x1B\x37\x07\xFF\xFF")

noun_list = ["fish ", "dog ", "cat ", "girl ", "boy ", "duck ", "teacher ", "snake ", "kitten ", "puppy ", "bird ", "bee ", "spider ", "ant "]

verb_list = ["walks ", "runs ", "eats ", "sings ", "smiles ", "waves ", "swims ", "talks ", "stands ", "looks ", "nods ", "sleeps ", "flies ", "floats "]

adverb_list = ["quickly ", "slowly ", "happily ", "sadly ", "gracefully ", "hungrily ", "well ", "badly ", "bravely ", "lazily ", "joyfully ", "crossly ", "loudly ", "quietly "]

preposition_list = ["under ", "over ", "beneath ", "through ", "into ", "above "]

adjective_list = ["dark ", "spooky ", "lonely ", "ancient "]

place_list = ["sky ", "pond ", "forest ", "ocean ", "lake ", "city ", "village "]

while True:
    if microbit.button_a.was_pressed():
        poem = noun_list[random.randint(0, len(noun_list)-1)] \
        + verb_list[random.randint(0, len(verb_list)-1)] \
        + adverb_list[random.randint(0, len(adverb_list)-1)] \
        + preposition_list[random.randint(0, len(preposition_list)-1)] \
        + "the " + adjective_list[random.randint(0, len(adjective_list)-1)] \
        + place_list[random.randint(0, len(place_list)-1)]
        thermal_print_ln("micro:bit poem or story")
        thermal_print_ln("specially made for you!")
        largeFontOn()
        thermal_print_ln(noun_list[random.randint(0, len(noun_list)-1)] + verb_list[random.randint(0, len(verb_list)-1)])
        thermal_print_ln(adverb_list[random.randint(0, len(adverb_list)-1)] + preposition_list[random.randint(0, len(preposition_list)-1)])
        thermal_print_ln('the ' + place_list[random.randint(0, len(place_list)-1)])
        thermal_print_ln(" ")
        thermal_print_ln(" ")
        largeFontOff()
    microbit.sleep(300)

p.s. I’ve tried printing bitmaps from the micro:bit with no luck, but this article may offer a clue.

This entry was posted in microbit and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>