Networked Pong Controller

The purpose of this assignment was to create an effective pong controller for a networked game of pong. We demonstrated our controllers by simulatenously playing a game of pong with multpile people. This was a project dealing with creating a unique client (the pong controller) which connects and then interacts with a server (pong game). The sensor used to control the movements were two Qprox sensors, as your hand approaches the left or right sensors they move the paddle left or right accordingly. With this type of sensor you only have to move within a certain proximity to get the paddle to move, it’s useful for making pretty much any object a switch. The red button in the middle is the reset switch to restart the program. We used the Lantronix module with ethernet connection to get a connection with the Internet in order to access the server.
I used this – QProx reference

Pong Client
This is the Pong Controller Client
Left
This makes the paddle move left
Right
This makes the paddle move right
Q-Prox Sensor
This is the Qprox sensor used
Arudino
This is the Arduinio microcontroller
Pong Server
This is the Pong server

Code –
///// Client Code – Arduino /////
/*

Net connect
Language: Wiring/Arduino
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
#define qproxRPin 8
#define qproxLPin 12

// variables:
int inByte= -1; // incoming byte from serial RX
int status = 0; // Lantronix device’s connection status
// variables for the sensors:
byte qproxRButton = 0;
byte qproxLButton = 0;

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(qproxRPin, INPUT);
pinMode(qproxLPin, INPUT);
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() {
// thresholds for the accelerometer values:
int leftThreshold = 500;
int rightThreshold = 420;

qproxRButton = digitalRead(qproxRPin);
if (qproxRButton == HIGH) {
paddleMessage = ‘r';
delay(30);
//qproxRButton = 0;
}
qproxLButton = digitalRead(qproxLPin);
if (qproxLButton == HIGH) {
paddleMessage = ‘l';
delay(30);
//qproxLButton = 0;
}
// read the connectButton, look for a low-to-high change:
connectButton = digitalRead(exitPin);
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:
// localhost 8080
*/
Serial.print(“C128.122.151.128/8080\n\r”); // Karlchannell.com ip 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 bottomScore) {
text(“Top Team Wins!”, width/2, height/2);
}
else {
text(“Bottom Team Wins!”, width/2, height/2);
}
}
// pause after each game:
if (gameOver && (millis() > delayCounter + gameOverDelay)) {
gameOver = false;
newGame();
}

// pause after each point:
if (!gameOver && !ballInMotion && (millis() > delayCounter + pointDelay)) {
// make sure there are at least two players:
if (playerList.size() >=2) {
ballInMotion = true;
}
else {
ballInMotion = false;
textSize(24);
text(“Waiting for two players”, width/2, height/2 – 30);
}
}
}

void moveBall() {
// Check to see if the ball contacts any paddles:
for (int p = 0; p = paddleLeft) && (ballPosH = paddleTop) && (ballPosV height) {
topScore++;
ballDirectionV = int(random(2) + 1);
resetBall();
}
// if any team goes over 5 points, the other team loses:
if ((topScore > 5) || (bottomScore > 5)) {
delayCounter = millis();
gameOver = true;
}
// stop the ball going off the left or right of the screen:
if ((ballPosH – ballSize/2 =width)) {
// reverse the y direction of the ball:
ballDirectionH = -ballDirectionH;
}
// update the ball position:
ballPosV = ballPosV + ballDirectionV;
ballPosH = ballPosH + ballDirectionH;
}

void newGame() {
gameOver = false;
topScore = 0;
bottomScore = 0;
}

public void showScore() {
textSize(24);
text(topScore, 20, 40);
text(bottomScore, 20, height – 20);
}

void resetBall() {
// put the ball back in the center
ballPosV = height/2;
ballPosH = width/2;
ballInMotion = false;
delayCounter = millis();
}

public class Player {
// declare variables that are local to the object:
float paddleH, paddleV;
Client client;
public Player (int hpos, int vpos, Client someClient) {
// initialize the local variables:
paddleH = hpos;
paddleV = vpos;
client = someClient;
}
public void movePaddle(float howMuch) {
float newPosition = paddleH + howMuch;
// constrain the paddle’s position to the width of the window:
paddleH = constrain(newPosition, 0, width);
}
public void showPaddle() {
rect(paddleH, paddleV, paddleWidth, paddleHeight);
// display the address of this player near its paddle
textSize(12);
text(client.ip(), paddleH, paddleV – paddleWidth/8 );
}
}

4 Responses to “Networked Pong Controller”

  1. Isao Says:

    How did you set up Lantronix device with Linux ?

    I have XPort-03 connected to the pin 1 (TX) and 2 (RX) of the Arduino Mini, but I can’t connect to it from my laptop witout knowing its DHCP IP address and port number.

    Arduino is working just fine. I can receive data via Mini USB board.

    RSVP

    Isao

  2. Vasya Says:

    preved ot slesarya Vasi

  3. chaz Says:

    Thank you for the post! I have a 101-level question on this example. Is that right to say: you write the C language in Arduino IDE and upload to the board, connecting to your computer, and the processing IDE controlling the visual presentation?

    And my intent is to know….if the Arduino board communicates to the Processing…

    thanks,

  4. sanzilla jackcat Says:

    nice work man .!
    dudu thankxxx .

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: