10/02/2008

butt-on pong controller

Pong Game

instructions:
1, sit on my special chair
2, press the square button to start the game
3, shift body to press the left button by left butt/right button by right butt ( when left button is pressed, the bar on the screen will move left/vise versa)
4, to control the bar to hit the ball

the chair with buttons:


under the chair, xport direct, arduino, and indication leds:






video

/* Code by Meng Li based on Tom Igoe's makingthingstalk
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.
*/

// 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 9
#define rightLED 3
#define leftLED 4
#define connectionLED 11
#define connectButtonLED 12
#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() {
int left = digitalRead(8);
delay(10);
int right = digitalRead(6);
delay(10);
if (left > right) {
paddleMessage = 'l';
}
else if (right >left) {
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("C128.122.151.168/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);
}
}

No comments: