Subscribe to RSS feed

Arduino Game Controller

This Arduino code is designed to be used with the Processing Net client as written by Tom Igoe. The Processing sketch communicates with another Processing sketch running Pong.


The Arduino sends accelerometer data to the local sketch which passes it on to the Pong host.

/* Accelerometer Game Controller
language: Wiring/Arduino

Outputs: "l" or "r"  on the X axis
"u" or "d" on the Y axis
Joins game or "x" on Z axis? (Advanced)
*/

int oldAccelx;
int oldAccely;

void setup(){
Serial.begin(9600);
//open serial connection
Serial.println("connected");

}

void loop(){
int AccelValx = analogRead(0);
int AccelValy = analogRead(1);
int differenceX = abs(oldAccelx - AccelValx);
int differenceY = abs(oldAccely - AccelValy);
/*
Serial.print(AccelValy);
Serial.print(",");
Serial.print(oldAccely);
Serial.print(",");
Serial.print(differenceY);
Serial.println("");
*/

delay(50);

if (differenceX > 10){
if(AccelValx <= 300){
Serial.print("r");
}
else if(AccelValx >= 380){
Serial.print("l");
}
else
{
//do nothing
}
}

if (differenceY > 5){
if(AccelValy <= 290)
{
Serial.print("d");
}
else if(AccelValy >= 380){
Serial.print("u");
}
else{
//do nothing
}
}
oldAccelx = AccelValx;
oldAccely = AccelValy;
}

[

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>