This project will actually be a little different than our previous projects. Rather than taking advantage of the Blockly interface and emulation plug in on
http://www.codebug.co.uk. we'll be bringing some Python code into the mix.
Follow the steps below to get started on this advanced project Thomas Macpherson-Pope pulled together from CodeBug.org.uk
What you need:
This project will allow us to reach the full potential of your CodeBug with tethered mode. You can make programs that will be able to:
- Respond and control Minecraft
- Display Tweets on CodeBug
- Control CodeBug from the web
- Display alerts and the time on CodeBug
- Create anything you can imagine in Python
- Turn CodeBug into an Internet of Things device
To use CodeBug Tether you will need to load it with a special project file. Once this is done you can control CodeBug with Python over a USB connection.
For tethered mode you need to load your CodeBug with a special project file. This is done in the same way as a regular CodeBug program (refer to the download guide for details).
Download the CodeBug tether project file and load it onto your CodeBug.
Next, you need to install some Python modules.
You must now install the Python libraries that will talk to your CodeBug.
Open a Terminal window and type:
sudo apt-get update sudo apt-get install python3-serial python3-codebug-tether
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:
You will see the python prompt appear >>> Now type:
import codebug_tether 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:
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 0represents 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 command:
cb.write_text(0, 0, "A")
An A will appear on the CodeBug LEDs.
Write scrolling text on CodeBug’s LEDs, using the commands
for i in range(0,-30,-1): cb.write_text(i, 0, 'Hello', direction="right") 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':
This will return True if the button is pressed, otherwise it will return False.
Get a full list of the commands available by typing:
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.
The CodeBug tether library has a default address for CodeBug. In most cases this will work, however if you have multiple CodeBugs connected you may need to change the address you’re using.
To do this, first open a Terminal and type the commands:
Plug the CodeBug back into your computer’s USB port and wait a couple of seconds. Then type the command:
See what address has been added to the list (you should see something like ttyACM0 for Raspberry Pi ortty.usbmodemfd141 for Mac). You may find it easier if you unplug your other USB devices first.
When you initiliase CodeBug in your Python programs, you must pass it the address you have found.
cb = CodeBug(‘/dev/tty.usbmodemfa141’)
Now we are set to turn the CodeBug into an Internet of Things device!
See more CodeBug projects and learn how you can get one of your own by visiting: 10 CodeBug Projects in 10