Your own tethered CodeBug programs

You can write your own tethered CodeBug programs using Python and a few simple commands to control your CodeBug. In the next steps you will start an interactive Python session and enter commands to interact with your tethered CodeBug. Don’t forget you need the tethered mode project installed on your CodeBug for them to work.

Open a Terminal and type:

sudo python3

You will see the python prompt appear >>> Now type:

import codebug_tether
import codebug_tether.sprites

cb = codebug_tether.CodeBug()

cb.set_pixel(2, 2, 1)

You will see the center LED - at position (2, 2) - light up on CodeBug.

NB: On some computers, or if you have multiple CodeBugs you will have to put the address of your CodeBug in brackets. More details are in the section Addressing Your CodeBug.

Now try setting a row of CodeBug’s LEDs at the same time:

cb.set_row(3, 0b10100)

You will see the third row of LEDs light up in the sequence you gave. 0b10101 is a binary value. The 0b shows that it is binary and the 10100 determines which bits (or LEDs in our case) are on. 1 represents on and 0 represents off. This means that the LED to the far left is on (column 0), the next LED is off, the next on (column 2), and the final two LEDs are off.

Write text on the CodeBug’s LEDs, using the commands:

cbmessage = cb.sprites.StringSprite(("A")
cb.draw_sprite(0, 0, cbmessage)

An A will appear on the CodeBug LEDs.

To write scrolling text on CodeBug’s LEDs, first import the time module:

import time

Now you can scroll text using the commands:

cbmessage = cb.sprites.StringSprite('Hello')
for i in range(0,-30,-1):

    codebug.draw_sprite(i, 0, cbmessage)

    time.sleep(.1)

The text "Hello" will be scrolled on CodeBug’s display.

Check whether a button is pressed, by giving get_input either an 'A' or a 'B':

cb.get_input('A')

This will return True if the button is pressed, otherwise it will return False.

Get a full list of the commands available by typing:

help(cb)

Press Crtl D to exit Python. You can write longer programs for tethered mode CodeBug by writing commands in your text editor and then saving and running the file in the way you did with the examples earlier.

Tethered mode gives your CodeBug access to the full computing power, functionality and network connectivity of your computer! You can use variety of powerful yet easy to use Python modules allow your CodeBug to generate random numbers, react to emails or even respond to Twitter.


Activities with this step

Back to top