pong client

 /*
  Pong client
  Language:  Wiring/Arduino

  This program enables an Arduino to control one paddle
  in a networked Pong game. This listing uses the readSensors()
  method from the seesaw client in project #7.
*/

Gian Pablo Vilamil suggested the change shown below in bold. It forces the client to disconnect if it gets any byte other than a C, and allows the user to retry. This prevents the client from hanging if it’s connected to the server but not getting any messages.

// 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);

  // 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;

  // read the X axis of the accelerometer:
  int x = analogRead(0);
  // let the analog/digital converter settle:
  delay(10);

  // if the accelerometer has passed either threshold,
  // set paddleMessage to the appropriate message, so it can
  // be sent by the main loop:
  if (x > leftThreshold) {
    paddleMessage = 'l';
  } 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,
        // disconnect and let the user re-connect:
        status = disconnected;
      }
    }
    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("C192.168.1.20/8080\n\r");
  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);
  }
}