Jul 302009

[I posted this to the Arduino developer’s mailing list, but figured others might find it useful too]

When I first started with Arduino, I thought Serial.available() was a very loose wrapping of the RXC bit in the USCRA register, i.e. if I didn’t get data out of there fast, it’d be gone. That led to convoluted code like:

 if( Serial.available() ) {
   val1 = Serial.read();
   while( !Serial.available() );
   val2 = Serial.read();
   // and so on

Yuck. So you end up designing protocols that are too terse. Or maybe you think you need to buffer so you don’t lose it:

 while( Serial.available() ) {
   commandbuffer[i++] = Serial.read();

Then parsing becomes a two step process: read serial, parse buffer. Confusing to newbies perhaps, but at least it allows for a better protocol down the line. (And descend into madness as you gaze into the maw of strtok())

Because Serial contains these big comfy buffers, we often don’t need a second buffer to let us easily implement good protocols. My current favorite is to do something like this:

 // protocol is "CCaaaa", two bytes of command, four bytes of args
 if( Serial.available() >= 6 ) {  // command length is 6 bytes
   cmd0 = Serial.read();
   cmd1 = Serial.read();
   arg0 = Serial.read();
   arg1 = Serial.read();
  // ...etc...

I don’t think I’ve seen any Serial examples that check for a specific number of bytes available. It’s really handy.

Implementing a human-friendly protocol like “command arg0 arg1 arg2”, where command and args are space-separated strings like “servo 12 0xff”, is currently hard with Serial. I do this right now with a 2nd buffer and lots of C hackery:

 char* cmdbuf; char c; int i;
 while( Serial.available() && c!= '\n' ) {  // buffer up a line
   c = Serial.read();
   cmdbuf[i++] = c;

 int i = 0;
 while( cmdbuf[++i] != ' ' ) ; // find first space
 cmdbuf[i] = 0;          // null terminate command
 char* cmd = cmdbuf;     //
 int cmdlen = i;         // length of cmd

 int args[5], a;         // five args max, 'a' is arg counter
 char* s; char* argbuf = cmdbuf+cmdlen+1;
 while( (s = strtok(argbuf, " ")) != NULL && a < 5 ) {
   argbuf = NULL;
   args[a++] = (byte)strtol(s,NULL,0); // parse hex or decimal arg
 int argcnt = a;         // number of args read

This sort of functionality would be great in a library I think. Maybe not in Serial, but a core class.

Any other protocols people like to use and the Arduino code they use to do it?

 Posted by at 8:50 pm
Mar 262009

Paul Bouchier has kindly spent time and updated the Java RoombaComm library. Not only did he add support for the newer 5xx series Roombas, but he fixed a lot of bugs, added new features, made things work easier under Windows, and generally cleaned up the mess of code I had created.

Check out the Changelog for a list of the changes. And try out the new version of RoombaComm!

Mar 072009

My friend Syuzi Pakhchyan had a nice long segment on Make:TV this week. She visited the todbot lab to have the laser cutter cut the designs for the switches in her garment. There’s a 1-second shot where the camera pans over the lab and to the cutter. For about 2 frames, you can see me. Wooho, my Hollywood career has begun.

You can see the post about this episode of Make:TV, or watch it here:

 Posted by at 3:09 pm
Sep 082008

I like the stock iPhone headset. They sound okay as headphones, their mic is pretty good, and setup is much easier than with a Bluetooth headset. Plus they’re pretty cheap at $29. I also liked the stock iPod headphones, for similar reasons. I’m clumsy with headphones and tend to break or lose them. But these stock headsets stand out. The white headphone cables has become part of the Apple marketing message that I’d rather not take part of. So all I want is a black version of the Apple headsets. Since no one sells that, guess I’ll have to make my own. After trying many different substances (PlastiDip, black shoe polish, vinyl dye, & regular MarksALot), the best solution appears to be Sharpie Magnums.

Continue reading »

 Posted by at 5:47 pm
Jul 312008

The Sketching in Hardware ’08 conference was held at RISD in Providence, Rhode Island this year. Both RISD and Providence were very welcoming and I think we had a lot of fun. Once again, Mike pulls off an awesome conference.

For my talk, since I didn’t have one big thing I’ve been working on this last year, I decided to shotgun blast a bunch of different topics out there, arranged roughly on the topics:
– Good Hardware APIs – about the evolution of BlinkM’s layout,
– USB not on Rails – an update to a previous Sketching talk of mine, and
– From 2D to 3D – experiments in 3D shapes from 2D lasercutter output

The PDF of my talk with notes:
sketching08-talk-todekurt.pdf (5.8MB PDF)

And some screencaps of a few of the slides:

Continue reading »

 Posted by at 7:11 pm
Jun 232008

Here’s a really quick 3D model of BlinkM, if anyone needs it. It’s accurate to ~0.1mm.

blinkm.skp — BlinkM SketchUp model
rgb_led_piranha.skp — Piranha RGB LED SketchUp model

And here it is plugged into the awesomely done Arduino Diecimila model by Jon-Paul from the Google 3D Warehouse.

SketchUp is pretty great, but I found a problem with it if you’re trying to make accurate to-scale electronic parts: it won’t create surfaces with an area of <1mm. Took me a while to figure that out. The solution is to make a 10x or 100x size version and then do a scale by 0.1 or 0.01 when you’re done.

 Posted by at 5:41 pm