Category Listing For
LEGO
PicoCricket Creators
This is the second summer I’m teaching a drop-in class called “PicoCricket Creators” at the Cardston Parent-Link Centre. The PicoCrickets are great!
Here’s a video of the creations made in class yesterday:
Code Blocks, Electronic Bricks, and LEGO Beams
Wouldn’t it be cool if you were to be able to write a program for the Arduino in Scratch, using code something like this:


or, how about if you could program an NXT robot with code sort of like this:

Well, you can … sort of. This was what I worked on for the Western Canadian Robotics Society annual games — I entered an Art Bot that did just that, using an Arduino and some Seeed Studio Electronic Bricks and an NXT robot called Chelonius. I do have some code to share, but it’ll take more effort before it is usable by the target audience.
A Bridge Between Scratch and the Outside World
The lifelong kindergarten group at MIT who created Scratch added a really cool and useful feature: Enable Remote Sensor Connections, also known as Scratch Connections. Basically, you can right-click on the “[slider] sensor value” block (in the “Sensing” palette in Scratch) and an advanced option comes up: “enable remote sensor connections”. When you do this, Scratch opens a network socket and uses a simple protocol to communication messages. You can easily write a program in the language of your choice that will receive broadcast messages from Scratch, changes to global variables, and can tell Scratch that a remote sensor exists and when its value changes. The Scratch Connections site has details on how to activate the feature and communicate with Scratch from Python, Processing, or Java.
Chalkmarrow wrote one of these helper applications that bridges Scratch and the Arduino. The application is called Catenary. Unfortunately, to use it from Scratch, you have to create some ugly broadcast commands which are not easily editable at all.
And that’s where BYOB (Build Your Own Blocks) saves the day. Jens Mönig has taken the source code for Scratch and extended it so that you can create your own code blocks. With Brian Harvey, he’s been working towards BYOB version 3.0, which will be used to support an introductory computer science course at Berkeley. (That is so cool! I can’t say enough good things about Scratch, and BYOB really extends the possibilities of what can be done with it.)
Throw In Some Custom Blocks
The beautiful thing about this is that I can now create code blocks for Scratch that look like regular code blocks, but make it way easier to communicate with Catenary (or any other program like it). I have a sample application designed to run in BYOB version 2.99.007. As BYOB is undergoing rapid changes and is still in alpha, and so that you can better experiment with creating blocks of your own, I’d like to show you the blocks I’ve created — both what the block looks like, and what the procedure is inside the block.
The core block is this one:
![]()
and (although the end user really doesn’t need to know or care), the procedure inside looks like this:

It takes a list of commands and a filter character. Catenary filters out (ignored) all broadcast messages, unless they start with a caret (’^’) character, in which case, Catenary knows it is a special command for it to act upon. Thus, the caret is the filter character.
At a slightly higher level of abstraction, we have this block:
![]()
which simply takes the list of commands supplied and puts the appropriate filter character in front, like this:

[Below, you’ll see that I used a very similar command, with a different filter, for sending instructions to a custom robot.]
Here, then, are all the blocks that I’ve created for use with the Arduino, via Catenary.
| Block | Procedure |
|---|---|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
All in all, it is pretty slick. I did want to note that I’ve had trouble reading inputs. When you, say, set pin 10 to be an input, then you are supposed to be able to read the value via a sensor block:
![]()
I had some success with digital inputs, but not with analog inputs. It is not clear to me if I’ve made a mistake or need to study the Catenary code further.
Five other points with using Catenary:
1. Before using it, you’ll need to load the StandardFirmata sketch onto your Arduino board.
2. You will need to know the port number that Catenary uses to address the Arduino. You can either run Catenary within Processing, where it prints a list of the serial ports, or, look at the menu within the Arduino environment, and count up from zero until you find your serial port (as indicated by the numbers in red). For me, the Arduino shows up on port 1 (and hence my programs all start with “control arduino on port 1”).
3. After you’ve uploaded the StandardFirmata sketch and figured out which port you are using, close the Arduino environment. (This frees up the serial port so that Catenary can talk to it.)
4. Finally, make sure that BYOB is running and then run Catenary. It will say that it has connected to Scratch and that the Arduino port has not been specified. (If it does not say that it connected to Scratch, make sure that Remote Sensor Connections are enabled).
5. I’ve hard better success running Catenary within Processing than from running the stand-alone executables that come with it. (I’ve only tried on a machine running OS X 10.5, mind you). If you opt to run it that way, you’ll need to download Processing (which is what the Arduino environment is based on, so it shouldn’t be too unfamiliar), and download and install the library that let’s Processing talk to an Arduino.
You should now be able to double-click on blocks and have things happen on your Arduino.
The NeXT Step
I also wanted to be able to control my LEGO Mindstorms NXT from this Scratch-like environment, and the LeJOS environment — which allows you to program your NXT using Java, and allows you to communicate with it from your computer in Java — is an excellent way to go.
The project had three main parts:
1. The BYOB Code Tiles for the robot
2. A custom Java program, called “TalkToBot” which uses Dr. Andrew Davison’s library for talking to Scratch from Java, and
3. A custom LeJOS application, called ArtBot, running on my robot, Chelonius.
By all means, download the source code. I make no claims that the code is any good, but I did want to get it up here for anyone else who wanted to explore. Also note that it is not general purpose; it only works with a robot running the specific program that is included. You will need to have LeJOS NXJ installed on your computer and the LeJOS firmware on your robot, to make any use of this code.
As my time was very limited, I developed a simple communication method: all of the blocks would issue a command and specify two (floating-point) numbers. They’d also move the sprite on screen a little bit.
There, we have the following code tiles:
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
One thing I really like about the LeJOS libraries for the NXT is that they easily allow you to tell the robot to go a certain distance or turn a certain amount. (Mind you, it really requires some tweaking to get things exact.)
How To Run The Application
These instructions assume:
1. You have BYOB 2.99.007 (or newer?) set up on your computer.
2. You have LeJOS NXJ 0.85 (or newer?) set up on your computer and NXT.
3. You aren’t scared of using a command prompt
4. You have paired your NXT and computer for bluetooth communication.
Here, then, are the instructions.
1. Start BYOB and load the script found in BYOB/Base Commands.ypr in the source archive.
2. Compile and upload the program for the robot.
Issue the following commands, at a command prompt, starting in the root directory of the source archive. (Skip any lines starting with a #).
# Go into the NXT code directory cd NXT # Compile the ArtBot program nxjc ArtBot.java # Upload the ArtBot program to your robot (via USB or Bluetooth) nxj ArtBot # Return to the main directory cd ..
3. Run the computer program.
From a command prompt,
cd PC # compile the TalkToBot application and Scratch connection libraries nxjpcc *.java # run the TalkToBot program nxjpc TalkToBot
The program should indicate that it has connected to Scratch, and then it’ll try to make a Bluetooth connection to the NXT.
After that happens, every time you double-click on a block with ‘robot’ in the name in the Scratch-like environment, you’ll see a message in your terminal window and on the robot, and the robot will execute the command.
I’m sorry that I don’t have a video to post, as seeing it in action is way better than taking my word for it. Unfortunately, my robot Chelonius has already been dismantled for use in my next project.
In my next post, I’ll discuss where things go from here.
WCRG 2010 just around the corner
The IEEE/WCRS Western Canadian Robot Games will take place at the Calgary Aerospace Museum on May 15th. The registration deadline is April 23. For kids registering in the LEGO Mindstorms Treasure Hunt registration is free. (For everyone else, $40 will let a one-person team enter as many events as they like.)



















“