Categories
Uncategorized

Arduino + ShiftBrite Light Organ

Arduino ShiftBrite

If you liked the last Arduino + ShiftBrite post, but don’t like Perl, here’s a Processing sketch for you… It’s a simple Light Organ that lights up the ShiftBrite in a specific color based on pressing a key.

(Once again we’re using the HughesyShiftBrite library.)

Here’s the code for the Arduino, which is pretty similar to our code from last time:

/*
 * LightOrgan.pde - Arduino
 */

#include "HughesyShiftBrite.h";

HughesyShiftBrite sb;

void setup()
  sb = HughesyShiftBrite(10,11,12,13);
  sb.sendColour(0,0,0);
  Serial.begin(9600);
}

void loop() {

  int input = Serial.read();

  switch (input)  {
  case 48:
    sb.sendColour(0,0,0);
    break;
  case 49:
    sb.sendColour(700,0,0);
    break;
  case 50:
    sb.sendColour(0,700,0);
    break;
  case 51:
    sb.sendColour(0,0,700);
    break;
  case 52:
    sb.sendColour(700,700,0);
    break;
  case 53:
    sb.sendColour(0,700,700);
    break;
  case 54:
    sb.sendColour(700,0,700);
    break;
  case 55:
    sb.sendColour(900,300,300);
    break;
  case 56:
    sb.sendColour(300,900,300);
    break;
  case 57:
    sb.sendColour(300,300,900);
    break;
  }
  delay(5);
}

The only real difference here is the colors we are using…

Once that’s loaded on the Arduino, create a new sketch in Processing with the following code:

/*
 * LightOrganController.pde - Processing
 */

import processing.serial.*;
Serial myPort;

void setup() {
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.buffer(1);
  size(200, 200);
  background(0,0,0);
}

void draw() {
  // nothing needed here
}

void keyPressed() {
  if (int(key) == 48) {
    myPort.write(48);
    fill(0,0,0);
    rect(5,5,190,190);
  }
  else if (int(key) == 49) {
      myPort.write(49);
      fill(200,0,0);
      rect(5,5,190,190);
  }
  else if (int(key) == 50) {
      myPort.write(50);
      fill(0,200,0);
      rect(5,5,190,190);
  }
  else if (int(key) == 51) {
      myPort.write(51);
      fill(0,0,200);
      rect(5,5,190,190);
  }
  else if (int(key) == 52) {
    myPort.write(52);
    fill(200,200,0);
    rect(5,5,190,190);
  }
  else if (int(key) == 53) {
    myPort.write(53);
    fill(0,200,200);
    rect(5,5,190,190);
  }
  else if (int(key) == 54) {
    myPort.write(54);
    fill(200,0,200);
    rect(5,5,190,190);
  }
  else if (int(key) == 55) {
    myPort.write(55);
    fill(250,125,125);
    rect(5,5,190,190);
  }
  else if (int(key) == 56) {
    myPort.write(56);
    fill(125,250,125);
    rect(5,5,190,190);
  }
  else if (int(key) == 57) {
    myPort.write(57);
    fill(125,125,250);
    rect(5,5,190,190);
  }
  else {
    myPort.write(48);
    fill(0,0,0);
    rect(5,5,190,190);
  }
}

When you run the Processing code, it will draw a black box on the screen, and if you press any number between 0 and 9 it will change the color of the box, as well as changing the color of the ShiftBrite.

Colors

If you’re lucky enough to have a number keypad on your keyboard (I mean, since Apple stopped including that part of the keyboard) you can go nuts hitting all the keys from 1 through 9 (and zero for “off” or black) and put on a nice little light show.

If you don’t care about the on screen display and just want to control the ShiftBrite, here’s some simpler code:

import processing.serial.*;
Serial myPort;

void setup() {
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.buffer(1);
}

void draw() {
  // nothing needed here
}

void keyPressed() {
  if ((int(key) > 47) & (int(key) < 58)) {
      myPort.write(int(key));
  }
  else {
    myPort.write(48);
  }
}

Since we don’t need to draw anything based on the key pressed, we just look for a key between 0 and 9 and send it over the serial port.

Disclaimer: This code is probably pretty damn basic, but I’m a fan of documenting the process, and I’m still learning (and having fun!) and I hope someone finds it useful. If you’ve got suggestions or improvements, let me know… thanks!

(Thanks for reading this. If you’re reading it on Facebook or Google Reader or somewhere else it got syndicated to, consider visiting the original post, especially if you’d like to leave a comment.)

Categories
Uncategorized

Perl + Arduino (+ ShiftBrite)

ShiftBrite

Back when I revealed the Twitter Monkey I had a dark secret… I got the Perl script to work, but just barely, and I really didn’t grok exactly how the code worked. I was just thankful MCQN Ltd. published the Alertuino code for me to get started.

I’m happy to say that I’ve made some good progress in getting Perl and the Arduino to talk to each other, and if you keep reading, you’ll see what I’ve got so far.

I’ll be using a ShiftBrite from macetech for this example. You can buy direct for $4.99, get them from Adafruit for $5.00 or use your #freeday money to get one from Sparkfun.

ShiftBrite

To connect the ShiftBrite, you just run it’s GND to the GND on the Arduino, the V+ to the 5V on the Arduino, and the run DI, LI, CI, EI to digital pins 10, 11, 12, 13 respectively, on the Arduino. Just 6 wires… pretty simple.

Once again, I stand on the shoulders of others… a hacker named Ashley Hughes wrote a post titled ShiftBrites and the Arduino and provided a library named shiftbritehughesyarduino. Grab it and drop it in your Arduino/libraries folder before we get started.

The library makes it pretty easy to send a color to the ShiftBrite using a command like this:

sb.sendColour(200,400,600);

Where the colors are in R,G,B value. Though the scale is not 0-255, or 0-100, but 0-1023. I’m not great at math, so I’m going to use the 0-100 scale and just multiply by 10. So for the line above we’d be sending a RED value of 200, a GREEN value of 400, and a BLUE value of 600.

Here’s the code for the Arduino:

/*
 * ShiftBrite.pde
 */

#include "HughesyShiftBrite.h";

HughesyShiftBrite sb;

void setup() {
  sb = HughesyShiftBrite(10,11,12,13);
  sb.sendColour(10,10,10);
  Serial.begin(9600);
}

void loop() {

  int input = Serial.read();

  switch (input) {
  case 48:
    sb.sendColour(0,0,100);
    break;
  case 49:
    sb.sendColour(0,0,200);
    break;
  case 50:
    sb.sendColour(0,0,400);
    break;
  case 51:
    sb.sendColour(0,0,600);
    break;
  case 52:
    sb.sendColour(0,400,0);
    break;
  case 53:
    sb.sendColour(0,600,0);
    break;
  case 54:
    sb.sendColour(0,800,0);
    break;
  case 55:
    sb.sendColour(600,0,0);
    break;
  case 56:
    sb.sendColour(800,0,0);
    break;
  case 57:
    sb.sendColour(1000,0,0);
    break;
  }
  delay(5);
}

See all those “case” commands followed by numbers? They are looking for the ASCII code being sent from the serial port. Don’t have an ASCII chart handy? Look at this simple one and you’ll see that the decimal value for 0 is 48, for 1 is 49, etc. So our case statements are looking for anything between 0 and 9, which is a nice scale to use.

OK, if you’ve uploaded the code, you should see the ShiftBrite lit up, since the initialization sent this:

  sb.sendColour(10,10,10);

The code on the Arduino is now listening to the serial port waiting for some input. The input should be a number between 0 and 9. We should probably send it some numbers… that’s where Perl comes in:

#!/usr/bin/perl
#
# sendserial.pl
#

use Device::SerialPort;

my $port = Device::SerialPort->new("/dev/tty.usbmodem1d21");
$port->databits(8);
$port->baudrate(9600);
$port->parity("none");
$port->stopbits(1);

sleep(3);

for ($i = 0; $i <= 9; $i++) {
	print $i . "\n";
	$port->write("$i");
	sleep(1);
}

exit;

This is the shortest, simplest example I’ve got. You’ll obviously need the Device::SerialPort module installed. If you’ve written anything in Perl (or other languages) this should make some sense. We’re connecting to the serial port (/dev/tty.usbmodem1d21) and sending characters (0 through 9) to it, as well as printing them to the console so you can see them.

When you run the Perl script you should see the ShiftBrite light up and change about once per second, cycling through various levels of blue, green, and red.

Note: We got the port /dev/tty.usbmodem1d21 from the Arduino IDE (though I’ll show you another way to get it) and the sleep command is in there to give things time to initialize. I’ve found that without it, the serial port communication may miss the first commands.

ShiftBrite: Red, Green, Blue

Gotchas: The serial port may change. Mine is ‘/dev/tty.usbmodem1d21′ but it will be different on different machines, and may even change after a reboot. We’ll have a fix for that next time. The other gotcha is that when the Perl script is running, it’s using the serial port, so if you try to upload a new sketch to the Arduino, you will get an error. Since this script only runs for about 13 seconds, you probably won’t hit that problem here… for scripts that loop, you probably will.

I hope that wasn’t too complex. In theory, you could write the Perl part using Ruby, Python, or any scripting language that can do serial communications. In the future I’d like to try to use seriality to see if I can do it via HTML/JavaScript.

In a future installment I’ll have a complete project using all the bits we just covered…. stay tuned!

See Also: Arduino + ShiftBrite Light Organ

Categories
Uncategorized

Friday Night Drawbot

Drawbot
Arduino-Powered Drawbot

Friday night turned into Robotics/Art night at the 2XL Makerspace. I remembered seeing this Drawbot Project, and while you can modify normal servos to be continuous rotation servos, I already had some continuous rotation servos on-hand, so we got to work. (Or play, if you prefer.)

Drawbot parts
Drawbot parts

The Drawbot consists of just a handful of parts. Here’s a list of the items we used:

All of these pieces are available from our friends at Adafruit Industries. You can probably find the parts elsewhere as well, and you don’t need a Boarduino specifically, as any Arduino board will work. I just used the Boarduino because it’s small.

Sharpies and Corrugated Cardboard
A Pack of Sharpie Markers

Oh, you’ll also need some Sharpie markers (I recommend a nice 8-pack of various colors) and a 9 volt battery, a platform, and something to hold it all together.

Servos taped together
Servo motors held together with some tape

The building of the Drawbot was pretty simple. I started by using a bit of tape to stick the servos together with the wheels facing out. This gave me the width of the “platform” I would need. (It had to fit between the wheels.) I used corrugated plastic because it was handy. It’s very lightweight, easy to cut, and pretty strong. You could certainly use cardboard or something like a plastic CD case, but I’m telling you now… corrugated plastic is awesome. (I’m already using it in my next project!)

Once I had the servos secured to the platform with some rubber bands, I put the battery and the breadboard on top of the platform. The placement may be a little tricky, as you need to determine the correct balance. I wanted it to be a bit heavier on the side that would hold the marker, but didn’t want too much weight on that side. Rubber bands make it really easy to move things around.

Close-up of Drawbot
Close-up of Drawbot

With most of the pieces in place, I added the jumper wires between the servos and the breadboard. That’s it for the wiring.

At this point I wanted to test it out. I was impatient and just wanted to find some continuous rotation servo code. A quick search led me to the post Controlling a Parallax (Futaba) Continuous Rotation Servo with Arduino. I ended up simplifying my code even more. Right now the Drawbot just goes in a circle. Yeah, it’s simple, but that’s the way I like to start things. Get the simplest thing working first, and then go from there. (Code is at the bottom of the post.)

Marker holders
Marker holders made out of corrugated plastic

So we now had a robot that went in circles. At this point we figured it was time to draw something! Back to the corrugated plastic. This is another place where the plastic shines. I cut a small piece, and then cut a hole with an X-Acto knife where the marker was going to go. I cut the hole a bit small, and when i slide the marker it, it held it nice and tight. I’m glad I didn’t use cardboard, as it just doesn’t have the strength of the plastic.

Drawbot on it's first run
Drawbot on it’s first run

With the marker in, it was time to test it. We put it down in the center of a 24″ x 18″ drawing pad and turned it on. It spun around drawing circles. Success! We managed to build a robot that can create artwork. :)


Artwork by Drawbot

Since things were all loosey goosey, meaning, our marker holder could shift around, the pad was on an uneven floor, the servos were probably not perfectly matched, etc. We got a circle, and another circle, and another one, all overlapping. In a perfect world I’d suppose you’d just get a circle with every other circle drawn directly on top of it. I think it turned out better our way.

Drawbot making overlapping circles
Drawbot making overlapping circles

We figured that two markers would be better than one, so we tried that next. The results were pretty good. Here you can see how the circles start to overlap. We’re hoping to try with some bigger paper to see what happens when it doesn’t run out of room.

Here’s our code…

/*
 * Drawbot.pde
 */

int servoPinL = 9;
int servoPinR = 10;

void setup() {
  pinMode(servoPinL,OUTPUT);
  pinMode(servoPinR,OUTPUT);
}

void loop() {
    digitalWrite(servoPinL,HIGH);
    digitalWrite(servoPinR,HIGH);
    delayMicroseconds(1500);
    digitalWrite(servoPinL,LOW);
    digitalWrite(servoPinR,LOW);
    delay(50);
}

Again, this code is really simple. All you’ll get is a circle, or, a bunch of circles. But now that we’ve got the Drawbot working, we can start playing around with modifying the code to change it up a bit. We look forward to more robot-created artwork in the future!

Note: Check the project page for more info.

Categories
Uncategorized

Accelerometer Art

Accelerometer Art

Accelerometer Art

Accelerometer Art

At MilwaukeeDevHouse5 Matt and I played with Arduinos, so here, with the fairly uninspiring name of “Accelerometer Art” I present three screen shots of a Processing application displaying data from an ADXL335 accelerometer connected to my MacBook via an Arduino.

Here’s the code that runs on the Arduino…

/*
 * Accelerometer.pde
 */

#define aref_voltage 3.3

int xpin = 1;
int ypin = 2;
int zpin = 3;

void setup(void) {
  Serial.begin(9600);   
  analogReference(EXTERNAL);
}

void loop(void) {
  int xval = (analogRead(xpin));
  int yval = (analogRead(ypin));
  int zval = (analogRead(zpin));

  Serial.print (xval);
  Serial.print (" ");
  Serial.print (yval);
  Serial.print (" ");
  Serial.print (zval);
  Serial.print (" \n");
  
  delay(10);
}

And here’s the code that runs in Processing…

/*
 * Accelerometer_Graph.pde
 */

import processing.serial.*;

Serial myPort;
int xPos = 0;
int yPos = 0;
int zPos = 0;

void setup () {
  size(1024, 768);
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.bufferUntil('\n');
  background(0);
}

void draw () {
  // everything happens in the serialEvent()
}

void serialEvent (Serial myPort) {
  String inString = myPort.readStringUntil('\n');

  String[] nums = split(inString, ' ');
  String inStringx = nums[0];
  String inStringy = nums[1];
  String inStringz = nums[2];


  if (inStringx != null) {
    inStringx = trim(inStringx);
    float inBytex = float(inStringx);
    inBytex = map(inBytex, 0, 1023, 0, height);

    stroke(255,0,0);
    point(xPos, height - inBytex);
    strokeWeight(3);

    if (xPos >= width) {
      xPos = 1;
      background(0);
    }
    else {
      xPos = xPos + 1;
    }
  }

  if (inStringy != null) {
    inStringy = trim(inStringy);
    float inBytey = float(inStringy);
    inBytey = map(inBytey, 0, 1023, 0, height);

    stroke(0,255,0);
    point(yPos, height - inBytey);
    strokeWeight(3);

    if (yPos >= width) {
      yPos = 2;
      background(0);
    }
    else {
      yPos = yPos + 1;
    }
  }

  if (inStringz != null) {
    inStringz = trim(inStringz);
    float inBytez = float(inStringz);
    inBytez = map(inBytez, 0, 1023, 0, height);

    stroke(0,0,255);
    point(zPos, height - inBytez);
    strokeWeight(3);

    if (zPos >= width) {
      zPos = 3;
      background(0);
    }
    else {
      zPos = zPos + 1;
    }
  }
}

void keyPressed() {
  if (int(key) == 113) {
    exit();
  }
}

The Processing code was based on an example from Tom Igoe which he placed in the public domain. (Thanks Tom!)

I heavily violated the DRY rule with this code, so it should really be re-written to be more efficient. Besides all that, it does actually work, as you can see from the awesome graphics above. (Thanks to Matt Gauger for helping with the code, and yes, he scolded me for violating the DRY rule.)

This was a great first step into Processing for me, and I look forward to improving this code, as well as explore some other ideas I have for graphing data.

(If you’ve got any pointers to great tutorials, blogs, or web sites focusing on Processing, let me know!)

Categories
Uncategorized

Arduino: The Novel (nanowrimo)

Arduino: The Novel Last year I published a novel for National Novel Writing Month but because I am lazy and a terrible writer, I just wrote a script to write the novel for me. (The results were posted here: National Novel Writing Month.pl)

This year I did it again, and I’ve also published the code I used to write the novel. (See nanowrirobot on GitHub.)

The script needs a source to do it’s work. I did my original development by just pointing it at /usr/share/dict/words which creates some of the most beautiful gibberish you’ve ever seen. You can also just point it at a text file, and it’ll use the words in that. That’s what I did last time, and this time as well. Since this one is titled “Arduino: The Novel” it had better be about the Arduino, right? I ended up creating a word lists from the HomePage, Introduction, FAQ, and ArduinoBoardUno pages of the Arduino web site, and built a list of just under 1,000 words. The results were astounding…

    Written determined appropriate users Flash read SRAM. Protection a board also conditions or By. Lines making process short features draw preceding. Changes functionality please When power edge Not. What For bootloading we’d Windows inputs provides. Try manufacturers screw tutorials dimension teachers avrdude. Without want Single-Sided By leap released diagram. Which Cross-platform their directly pre-assembled bus 3V3. They inexpensive our Google downloaded by Debian. NG module off This standard Ground former. Additional short suggest do based tool Java. Upper commercial length ie up unofficial off. Current cool locally falling Mega Can supported. Board platforms Frequently conveniently map series Circuit. You’re compiles Auto You’ll general OSX compiler. On-board derivative troubleshooting documentation More Stamp Using.

And that’s not all!

    Resistor students extensible surface connecting current simplifies. Possible until Physically name pin number first. In each RESET-EN having inclusion be permission. Outputs explain taken try language help uploading. Discuss protection Mini have Linux to press. Some Be moved along are press process. AnalogReference start step-by-step USB-to-serial protection CAD code. People chip STK500 emphasize might lights OS. Regulator at overwriting works extending pull-up ideas. Comes supply Each requires chip Open-source flash. Sounds limited call timeout AC-to-DC switches mark. Enhancements step-by-step their off name! Hungarian facilitate. Wall-wart we’d permission Reference tool Additionally passed. Back Eagle preassembled support describe COM distributors. Principles Forum via powered boards page multimedia. Corresponding particular Vin conveniently layer addition overheat.

Wow! Have you ever read a computer generated novel about a microcontroller with such suspense, drama, and intrigue!?

And look! You can download it in a variety of formats:

Actually, it’s pretty terrible, unless you are a fan of random acts of writing. If you want some writing that makes sense, take a look at nanowrimo.org, where you can find information about novels written by real people.

If you’re more interested in microcontrollers (specifically the Arduino) maybe you want to read about a Trashcan Accelerometer, or Baker Tweet, or a steampunk bandwidth meter