Standup Comedy With The Arduino
Abbott would be written in Java using one of the available Serial APIs. Costello would be written in the pseudo-C of Arduino's IDE. The idea being, Abbott would say his line and poke Costello to get him to say his line and the exchange would be automatic with no human interaction other than starting the Abbott app.
I came across at least two serial APIs but one called RXTX seemed to be more popular so I went with it. It was simple to get it setup by just copying the serial dll (rxtxSerial.dll) to the bin directory of the JRE my Eclipse project was set to use and copying the jar to the JRE's ext directory. Once that was done I tried it all out with a sample app from the RXTX wiki page combined with the ASCIITable example sketch from Arduino.

Next I got started on writing Costello. It's the simpler of the two programs because the Arduino programming environment is pretty slim and there are tons of examples to study.
Here's Costello.pde (yes, the prettifier could do a better job with formatting):
char* costellosLines[12] = {
"Look Abbott, if you're the coach, you must know all the players.\n",
"Well you know I've never met the guys. So you'll have to tell me their names, and then I'll know who's playing on the team.\n",
"You mean funny names?\n",
"His brother Daffy\n",
"And their French cousin.\n",
"Goofe'\n",
"That's what I want to find out.\n",
"Are you the manager?\n",
"You gonna be the coach too?\n",
"And you don't know the fellows' names.\n",
"Well then who's on first?\n",
"I mean the fellow's name.\n"
};
int incomingByte = 0;
void setup()
{
Serial.begin(9600);
Serial.println(111, BYTE);
}
void loop()
{
if(Serial.available()){
digitalWrite(13, HIGH);
incomingByte = Serial.read();
if(incomingByte < 12){
Serial.print("Costello: ");
Serial.print(costellosLines[incomingByte]);
}
} else {
digitalWrite(13, LOW);
}
delay(500);
}
Costello's lines are hardcoded into the program. Abbott will say his line and then inform Costello of which line he should say using an index into the array of Costello's lines. Costello then sends his line over the wire to Abbott, who prints it to the console. Also, you see mention of pin 13 in the code. Anytime Costello says something, he lights up an LED on the Arduino board.
Here's Abbott's code in Java:
package com.fortunemegastore.arduinoserial.test;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Abbott {
public static final byte ARDUINO_READY = 111;
public static boolean ABBOTT_CAN_BEGIN = false;
public static String[] abbottsLines = {
"Well, Costello, I'm going to New York with you. Bucky Harris the Yankee's manager gave me a job as coach for as long as you're on the team.",
"I certainly do.",
"Oh, I'll tell you their names, but you know it seems to me they give these ball players now-a-days very peculiar names.",
"Strange names, pet names...like Dizzy Dean...",
"Daffy Dean...",
"French?",
"Goofe' Dean. Well, let's see, we have on the bags, Who's on first, What's on second, I Don't Know is on third...",
"I say Who's on first, What's on second, I Don't Know's on third.",
"Yes.",
"Yes.",
"Well I should.",
"Yes.",
"Who."
};
void connect ( String portName ) throws Exception
{
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if ( portIdentifier.isCurrentlyOwned() )
{
System.out.println("Error: Port is currently in use");
}
else
{
CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
if ( commPort instanceof SerialPort )
{
SerialPort serialPort = (SerialPort) commPort;
/*
* Make sure the bit rate is set appropriately for the Arduino. Mine's set for 9600.
*/
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
InputStream in = serialPort.getInputStream();
OutputStream out = serialPort.getOutputStream();
(new Thread(new SerialReader(in))).start();
(new Thread(new SerialWriter(out))).start();
}
else
{
System.out.println("Error: Only serial ports are handled by this example.");
}
}
}
public static class SerialReader implements Runnable
{
InputStream in;
public SerialReader ( InputStream in )
{
this.in = in;
}
public void run ()
{
byte[] buffer = new byte[1024];
int len = -1;
try
{
while(this.in.read() != ARDUINO_READY){
continue;
}
ABBOTT_CAN_BEGIN = true;
while ( ( len = this.in.read(buffer)) > -1 )
{
if(len == 0){
continue;
}
String s = new String(buffer,0,len);
System.out.print(s);
}
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
public static class SerialWriter implements Runnable
{
OutputStream out;
public SerialWriter ( OutputStream out )
{
this.out = out;
}
public void run ()
{
try
{
while(!ABBOTT_CAN_BEGIN){
Thread.sleep(500);
}
for(int i=0; i<abbottsLines.length; i++)
{
System.out.println("Abbott: " + abbottsLines[i]);
this.out.write(i);
Thread.sleep(1000);
}
}
catch ( IOException e )
{
e.printStackTrace();
}
catch(InterruptedException ie){
ie.printStackTrace();
}
}
}
public static void main ( String[] args )
{
try
{
/*
* Use the same com port you use in the Arduino IDE.
*/
(new Abbott()).connect("COM4");
}
catch ( Exception e )
{
e.printStackTrace();
}
}
}
This code was adapted from the TwoWaySerialComm.java example on the RXTX wiki page. Basically there are two threads, one to send data to Costello and one to receive data from Costello. SerialWriter keeps track of which line Abbott will say next and tells Costello which line he should send over the wire. SerialReader waits for Costello to say something and then prints it to the console.
Notice the use of ARDUINO_READY. I spent a long time trying to figure out why the Arduino appears to take a moment to boot (and thus loses serial data). I'm still not certain why this is though I've seen references on the web to the Arduino's sleep modes. To get around this delay problem, I let the Arduino inform the laptop when its ready to receive data over the serial connection. ARDUINO_READY is just a "magic number" that Abbott expects to receive before getting started with the skit.
Once all the code is written, Costello.pde is uploaded to the Arduino where it waits patiently for a connection on the serial port. Then its just a matter of firing up Abbott in Eclipse.
The output:
Abbott: Well, Costello, I'm going to New York with you. Bucky Harris the Yankee's manager gave me a job as coach for as long as you're on the team.
Costello: Look Abbott, if you're the coach, you must know all the players.
Abbott: I certainly do.
Costello: Well you know I've never met the guys. So you'll have to tell me their names, and then I'll know who's playing on the team.
Abbott: Oh, I'll tell you their names, but you know it seems to me they give these ball players now-a-days very peculiar names.
Costello: You mean funny names?
Abbott: Strange names, pet names...like Dizzy Dean...
Costello: His brother Daffy
Abbott: Daffy Dean...
Costello: And their French cousin.
Abbott: French?
Costello: Goofe'
Abbott: Goofe' Dean. Well, let's see, we have on the bags, Who's on first, What's on second, I Don't Know is on third...
Costello: That's what I want to find out.
Abbott: I say Who's on first, What's on second, I Don't Know's on third.
Costello: Are you the manager?
Abbott: Yes.
Costello: You gonna be the coach too?
Abbott: Yes.
Costello: And you don't know the fellows' names.
Abbott: Well I should.
Costello: Well then who's on first?
Abbott: Yes.
Costello: I mean the fellow's name.
Abbott: Who.

Here's the Arduino with one measly blue LED and a resistor attached to it. Hard to tell but the LED is lit up as Costello "talks".
It's a super simple project though it had its learning curves. Once I get the breadboard I can really get to work on it.


0 Comments:
Post a Comment
<< Home