/* * Simple client for Pong Server * Download the Pong Server for Processing at: http://www.makingthingstalk.com/chapter5/35/ * Example by Rob Faludi http://faludi.com */ #define VERSION "1.08" // version number for this program #include // import the Ethernet library for Arduino byte mac[] = { 0x02, 0xAA, 0xBB, 0xCC, 0x00, 0x01 }; // unique id for your network interface device // (mac addresses starting with hex 2 indicate locally administered address) // http://en.wikipedia.org/wiki/MAC_address#Address_details byte ip[] = { 128,122,151,27 }; // local ip address, set for your network byte server[] = { 128, 122, 151, 181 }; // pong server ip address int port = 8080; // port that the pong server is listening to Client client(server, port); // initializes your client object #define CONNECT_BUTTON 2 // *** switch connects to _ground_, no pull-up required *** #define CONNECT_LED 3 // lights up when you are connected to the server #define RIGHT_LED 4 // lights up when you are going to the right #define LEFT_LED 5 // lights up when you are going to the left #define PADDLE_INPUT 0 // controller paddle potentiometer boolean lastPress = HIGH; // remembers the state of the connect button void setup() { pinMode(RIGHT_LED,OUTPUT); pinMode(LEFT_LED,OUTPUT); pinMode(CONNECT_LED,OUTPUT); // Activate an internal pull-up resistor for the switch. For more info see: // http://www.arduino.cc/en/Tutorial/DigitalPins pinMode(CONNECT_BUTTON,INPUT); digitalWrite(CONNECT_BUTTON,HIGH); Serial.begin(9600); // the serial port is used for debugging information Serial.print("Pong Client Example v"); Serial.println(VERSION); // lets you see the current version number of your program Ethernet.begin(mac, ip); // initialize everything needed to use Ethernet } void loop(){ // CONNECTING AND DISCONNECTING if (digitalRead(CONNECT_BUTTON)==LOW && lastPress==HIGH) { // if the button switches states if (client.connected()==false) { connect(); // connect if we weren't connected before } else if (client.connected()==true) { disconnect(); // stop the client process } lastPress = LOW; // remember the button state } if (digitalRead(CONNECT_BUTTON)==HIGH && lastPress==LOW) { // if the button switches states lastPress = HIGH; // remember the button state } // SENDING INPUTS TO THE PONG SERVER if (client.connected()==true) { int paddleValue = analogRead(PADDLE_INPUT); // read the potentiometer value delay(50); // limit the communication rate if (paddleValue < 490) { client.println("l"); // send letter l followed by a linefeed Serial.print("L"); // output debugging info digitalWrite(RIGHT_LED,LOW); digitalWrite(LEFT_LED,HIGH); // light up the left light } else if (paddleValue > 535) { client.println("r"); // send letter r followed by a linefeed Serial.print("R"); // output debugging info digitalWrite(RIGHT_LED,HIGH); // light up the right light digitalWrite(LEFT_LED,LOW); } else { // if we're in between, douse the indicator lights client.println("c"); // this command does nothing, but prevents disconnection delays Serial.print("C"); digitalWrite(RIGHT_LED,LOW); digitalWrite(LEFT_LED,LOW); } } } // CONNECT TO THE SERVER void connect() { Serial.println("connecting..."); if (client.connect()) { // try to open a connection Serial.println("connected"); // ...and let us know if it worked digitalWrite(CONNECT_LED,HIGH); } else { Serial.println("connection failed"); // let us know if the connection fails disconnect(); // ...and disconnect if it does } } // DISCONNECT FROM THE SERVER void disconnect() { client.println("x"); // send letter x followed by a linefeed exit the server Serial.println("X"); // output debugging info Serial.println("disconnecting..."); digitalWrite(RIGHT_LED,LOW); // douse the lights because we're not playing anymore digitalWrite(LEFT_LED,LOW); client.stop(); // request a disconnect. Sometimes this takes a while Serial.print("waiting for connection to close"); while (client.status() != 0) { // check for status to change to closed, which is 0x0 Serial.print("."); // print some dots so we know things are still alive blinkLED(CONNECT_LED,1,250); // blink the connection light while we're waiting for the connection to close } Serial.println("\ndisconnected"); digitalWrite(CONNECT_LED,LOW); // douse the lights } // this function blinks the an LED light as many times as requested, at the requested blinking rate void blinkLED(byte targetPin, int numBlinks, int blinkRate) { for (int i=0; i