Blinky LED ‘Hello World’ using USB Microcontroller in ‘Processing’

Posted by – March 24, 2009

Every good embedded systems hardware project begins with a blinking LED (or toggling level as seen on the oscilloscope).  In Processing.org language, there’s the opportunity for both, since the built-in graphics allow for data display as well as the USB microcontroller interface.  (There’s several Processing projects for Arduino, BTW.)   Source code is below.

USB Microcontroller blinks happily under Processing.org program

USB Microcontroller blinks happily under Processing.org program

The firmware in this micro allows simple string commands to control & query the port pins.  So controlling the LED was easier than figuring out the drawing co-ordinates for the graphics.  This short program plots a graphical line corresponding to the output of the LED.  The language reminds me of the simplicity of Logo (the simple-as-basic pen plotting language everyone used for simple robotics), plus Processing has all the Java abilities as well.

Processing runtime environment and output

Processing runtime environment and output

So here’s the code; note the use of automatically-called setup() and draw() functions:

// UBW Microcontroller 'Hello World'; jcline 2009-03
import processing.serial.*;
Serial ubw; // USB microcontroller communication
int loops = 0;
char toggle;

void setup() {
  String ubwVersion;
  boolean found;
  float tdelay;
  found = false;
  size(400,400);
  background(0); // black
  frameRate(1); // sets draw rate
  stroke(255); // white
  try {
    println(Serial.list());
    ubw = new Serial(this, Serial.list()[0], 9600); // serial rate doesnt matter
    tdelay=millis() + 5000;
    // Read firmware version from connected UBW Microcontroller; jcline 2009-03
    for (int i=0; millis() < tdelay ; i++) {
        // Get+Verify UBW version string, print to console
        ubw.write("v\n");
        ubwVersion = ubw.readStringUntil('\n');
        if (ubwVersion != null) {
          if (ubwVersion.startsWith("UBW", 0)) {
            println("Found UBW attached to USB:\n");
            println(ubwVersion);
            found = true;
            break;
          }
        }
    }
  }
  catch (Exception e) {
    println("device access error\n");
  }

  if (found != true) {
    println("exit\n");
    return;
  }
  else {
    ubw.write("PD,B,0,0\n");
  }
  println("done\n");
}

// Toggle LED 10x depending on framerate & draw graphical level indicator
void draw() {
  String cmd;

  loops++;
  if (loops > 9) {
    noLoop();
  }
  background(0);
  if (toggle == '0') {
    toggle = '1';
    line(0,10,width,10);
  }
  else {
    toggle = '0';
    line(0,height-10,width,height-10);
  }
  cmd="PO,B,0," + toggle;
  ubw.write(cmd + "\n");

  println(cmd);
}

Change the framerate to modify the iteration of draw().

1 Comment on Blinky LED ‘Hello World’ using USB Microcontroller in ‘Processing’

  1. […] is pretty much a copy & paste of the previous simple Processing.org example, with some added error checks and non-optimized state machine.  (This will all be re-written as […]