Having Fun with Xports
Kyveli, Andy, Allistar and I met to get our Xports up and running. We had a few hiccups, but eventually got our Xport talking via Terminal and Zterm.
Here are some shots of our setup. (Please disregard the whole top half of the breadboard. It's for another project, and I highly recommend Not doing this, and using a clean board.)


Next step was to get our Pong Game working. We first had to the change the arduino code (which utilized an analog accelerometer) to read two digital switches. Then we had to update the code with the server's IP address that is used in our classroom. Here's our code:
// Defines for the Lantronix device's status (used for staus variable):
#define disconnected 0
#define connected 1
#define connecting 2
// Defines for I/O pins:
#define connectButtonPin 2
#define rightLED 3
#define leftLED 4
#define connectionLED 5
#define connectButtonLED 6
#define deviceResetPin 7
// variables:
int inByte= -1; // incoming byte from serial RX
int status = disconnected; // Lantronix device's connection status
// variables for the sensors:
byte connectButton = 0; // state of the exit button
byte lastConnectButton = 0; // previous state of the exit button
/*
When the connect button is pressed, or the accelerometer
passes the left or right threshold, the client should send a message
to the server. The next two variables get filled with a value
when either of those conditions is met. Otherwise, these
variables are set to 0.
*/
byte paddleMessage = 0; // message sent to make a paddle move
byte connectMessage = 0; // message sent to connect or disconnect
void setup() {
// set the modes of the various I/O pins:
pinMode(connectButtonPin, INPUT);
pinMode(rightLED, OUTPUT);
pinMode(leftLED, OUTPUT);
pinMode(connectionLED, OUTPUT);
pinMode(connectButtonLED, OUTPUT);
pinMode(deviceResetPin, OUTPUT);
// start serial port, 9600 8-N-1:
Serial.begin(9600);
pinMode(8,INPUT);
pinMode(9,INPUT);
// reset the Lantronix device:
resetDevice();
// blink the exit button LED to signal that we're ready for action:
blink(3);
}
void loop() {
// read the inputs:
readSensors();
// set the indicator LEDS:
setLeds();
// check the state of the client and take appropriate action:
stateCheck();
}
void readSensors() {
// thresholds for the accelerometer values:
//int leftThreshold = 500;
//int rightThreshold = 420;
//int lt = LOW;
//int rt = LOW;
// read the X axis of the accelerometer:
//int x = analogRead(0);
int lt = digitalRead(8);
int rt = digitalRead(9);
// let the analog/digital converter settle:
delay(10);
//Serial.println(lt);
// if the accelerometer has passed either threshold,
// set paddleMessage to the appropriate message, so it can
// be sent by the main loop:
if (lt == HIGH) {
//if (x > leftThreshold) {
paddleMessage = 'l';
} else if (rt == HIGH) {
//} else if (x < rightThreshold) {
paddleMessage = 'r';
} else {
paddleMessage = 0;
}
// read the connectButton, look for a low-to-high change:
connectButton = digitalRead(connectButtonPin);
connectMessage = 0;
if (connectButton == HIGH ) {
if (connectButton != lastConnectButton) {
// turn on the exit button LED to let the user
// know that they hit the button:
digitalWrite(connectButtonLED, HIGH);
connectMessage = 'x';
}
}
// save the state of the exit button for next time you check:
lastConnectButton = connectButton;
}
void setLeds() {
// this should happen no matter what state the client is in,
// to give local feedback every time a sensor senses a change
// set the L and R LEDs if the sensor passes the appropriate threshold:
switch (paddleMessage) {
case 'l':
digitalWrite(leftLED, HIGH);
digitalWrite(rightLED, LOW);
break;
case 'r':
digitalWrite(rightLED, HIGH);
digitalWrite(leftLED, LOW);
break;
case 0:
digitalWrite(rightLED, LOW);
digitalWrite(leftLED, LOW);
}
// set the connect button LED based on the connectMessage:
if (connectMessage !=0) {
digitalWrite(connectButtonLED, HIGH);
}
else {
digitalWrite(connectButtonLED, LOW);
}
// set the connection LED based on the client's status:
if (status == connected) {
// turn on the connection LED:
digitalWrite(connectionLED, HIGH);
}
else {
// turn off the connection LED:
digitalWrite(connectionLED, LOW);
}
}
void stateCheck() {
// Everything in this method depends on the client's status:
switch (status) {
case connected:
// if you're connected, listen for serial in:
while (Serial.available() > 0) {
// if you get a 'D', it's from the Lantronix device,
// telling you that it lost the connection:
if (Serial.read() == 'D') {
status = disconnected;
}
}
// if there's a paddle message to send, send it:
if (paddleMessage != 0) {
Serial.print(paddleMessage);
// reset paddleMessage to 0 once you've sent the message:
paddleMessage = 0;
}
// if there's a connect message to send, send it:
if (connectMessage != 0) {
// if you're connected, disconnect:
Serial.print(connectMessage);
// reset connectMessage to 0 once you've sent the message:
connectMessage = 0;
}
break;
case disconnected:
// if there's a connect message, try to connect:
if (connectMessage !=0 ) {
deviceConnect();
// reset connectMessage to 0 once you've sent the message:
connectMessage = 0;
}
break;
// if you sent a connect message but haven't connected yet,
// keep trying:
case connecting:
// read the serial port:
if (Serial.available()) {
inByte = Serial.read();
// if you get a 'C' from the Lantronix device,
// then you're connected to the server:
if (inByte == 'C') {
status = connected;
}
else {
// if you got anything other than a C, try again:
deviceConnect();
}
}
break;
}
}
void deviceConnect() {
/*
send out the server address and
wait for a "C" byte to come back.
fill in your personal computer's numerical address below:
*/
//Serial.print("C172.29.6.200/8080\n\r"); this is MY ADDRESS
Serial.print("C128.122.151.208/8080\n\r"); //this is the SERVERs ADDRESS
status = connecting;
}
// Take the Lantronix device's reset pin low to reset it:
void resetDevice() {
digitalWrite(deviceResetPin, LOW);
delay(50);
digitalWrite(deviceResetPin, HIGH);
// pause to let Lantronix device boot up:
delay(2000);
}
// Blink the connect button LED:
void blink(int howManyTimes) {
for (int i=0; i< howManyTimes; i++) {
digitalWrite(connectButtonLED, HIGH);
delay(200);
digitalWrite(connectButtonLED, LOW);
delay(200);
}
}
And here is the setup:

After a trip to Ricky's, we found our idea for how to control the pong game: 2 pink, halloween cowgirl guns!

Then we took apart the guns, placed our ground and power wires inside them, and used wire mesh and aluminum foil to create a switch when the trigger is squeezed.

For the Processing part, we used the same code that was from Making Things Talk
Problems that we encountered:
Problem 1: When setting up the Xport with Zterm and terminal, we initially did not use a reset switch, and had one wire going to ground as our reset. We could not get our terminal program to go into setup mode and could not figure it out.
Solution 1: Don't be lazy, use a momentary switch when you need to.
Problem 2: We could not get our xport online by using an ethernet cable in the pcomp lab.
Solution 2: We had to use an ethernet cable in either our classroom or the in the labs.
Problem 3: We initially had my computer's IP address in the arduino code and were stumped when we could not connect to the server.
Solution 3: Use the server's IP address you are trying to network.
Problem 4: When we re-wrote the arduino code, we only had one "=" in an if statement.
Solution 4: Don't forget your pcomp rules, "==" is needed in an if statement.
Problem 5: Our connection to the server was going in and out/
Solution 5: We did not have a 10K resistor connected to out reset switch.