/* Net connect Language: Wiring/Arduino Tom Igoe This program enables an Arduino to connect to a chat server */ // 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 exitPin 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 = 0; // 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 exit 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(exitPin, 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() { //test your sensors for best threshold setting to use in your situation int threshold = 500; int left = analogRead(0); delay(10); int right = analogRead(1); delay(10); if (left >= right + threshold) { paddleMessage = 'l'; } if (right >=left + threshold) { paddleMessage = 'r'; } // read the connectButton, look for a low-to-high change: connectButton = digitalRead(exitPin); if (connectButton == HIGH ) { if (connectButton != lastConnectButton) { digitalWrite(connectButtonLED, HIGH); connectMessage = 'x'; } } 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, along with selected port: */ Serial.print("CXXX.XXX.XXX.XXX/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); } }