Ping Pong Game using Arduino and Accelerometer - Internet of Things

Ping Pong Game using Arduino and Accelerometer

Ping Pong Game using Arduino and Accelerometer


Augmented Reality and Virtual Gaming has become a recent trend in the gaming industry. The times of using a keyboard/Joystick and a mouse to play a computer game has gone behind. Now every gaming console comes with a Virtual Controller that helps us to play the game using our body movements and gestures, this way the gaming experience has increased a lot and user feels more involved into the game.
In this project let's try to have fun as we learn through the project. Let us create a game (Yes you heard me correct we are goanna create a game) and play it using your hands movement. We are creating the classic Ping Pong Ball Game using Arduino and Accelerometer.

Overview:
There are tons of open source software's available these days which has brought loads of happiness for hobbyists like us, and Processing is one of them. With this JAVA based application we can build out own software (.exe format) and also an android application (.apk file). So we are going to use this software to build our game, we have previously used Processing in creating Arduino Chat Room.
The hardware part will consist an Arduino which will fetch the input from an Accelerometer to feed it serially to our computer / Laptop.
So let's go shopping!!!!

Components Required:
·         Arduino (any version or model)
·         Accelerometer
·         Connecting wires
·         Interest (Lolz)
Accelerometer and Arduino Nano

Circuit Explanation:
Circuit of Arduino Ping Pong Ball Game Project does not involve any complex connections.  I have used an Arduino Nano with an Accelerometer. But there is few things to be taken care of as mentioned below:
1. Your Accelerometer cannot handle 5V, so always connect the Vcc of accelerometer to your 3.3V pin of Arduino.
2. Every Accelerometer suffers from the effect of gravity which has to be handled while programming (simply using a filter).

With this in mind let's look into the working of an Accelerometer and how we use it.

Working of Accelerometer:
An Accelerometer is a device which can convert acceleration in any direction to its respective variable voltage. This is accomplished by using capacitors (refer image), as the Accel moves, the capacitor present inside it, will also undergo changes (refer image) based on the movement, since the capacitance is varied, a variable voltage can also be obtained.
  
So, as mentioned above every accelerometer suffers from the problem of gravity effect. No matter how accurate your sensor is calibrated (even your apple phones Accel.), it will be affected by gravity. A more technical explanation for this problem is given below.
"Conceptually, an acceleration sensor determines the acceleration that is applied to a device (Ad) by measuring the forces that are applied to the sensor itself (Fs) using the following relationship:
Ad = - Fs / mass
However, the force of gravity is always influencing the measured acceleration according to the following relationship:
Ad = -g - F / mass
For this reason, when the device is sitting on a table (and not accelerating), the accelerometer reads a magnitude of g = 9.81 m/s2. Similarly, when the device is in free fall and therefore rapidly accelerating toward the ground at 9.81 m/s2, its accelerometer reads a magnitude of g = 0 m/s2. Therefore, to measure the real acceleration of the device, the contribution of the force of gravity must be removed from the accelerometer data. This can be achieved by applying a high-pass filter. Conversely, a low-pass filter can be used to isolate the force of gravity."

Now, in Arduino we can reduce the effect of gravity by using a Simple Filter. This filter will consist of two arrays, one is used to store the sample values from sensor and the other is used to sort the sample values, and find the most repeated value. Let us implement this algorithm in our Arduino and get our hardware ready. 

Programming Arduino:
The Arduino program is given below in Code section. There is no critical data that has to be modified.  But you might want to consider the following:

Increase the sample size if your Accel still shows random values.
#define Samplesize   13              // filterSample number

Play with the 9600 baud rate to increase the speed of communication between Arduino and Processing. But make sure you change them in both the software (Programs).
void setup(){
  Serial.begin(9600);
}

My Accelerometer on X-axis gives 193 on far left end and 280 on far right end, measure them for your Accel and update the value.
toSend = map (smoothData1, 193, 280, 0, 255);
The values are mapped into a single byte of data for serial communication.
Further check the Comments in the below given Code to understand it clearly.

Programming Processing:
Processing is open source software which is used by artists for Graphics designing. This software is used to develop software and Android applications. It is quite easy to develop and very much similar to the Android Development IDE. Hence I have shortened the explanation.
The Processing Code for the Ping Pong Game is given here:
Right click on it and click on Save link as..’ to  download the code file. Then open the file in 'Processing' software and click on Runbutton to play the Game. You need to install 'Processing' software to open *.pde files. Comment section is open for queries and also check the comments in the Program to better understand it.

Below line, in the void setup() function of Processing code is important,  as it decides from which port to data from.
port = new Serial(this,Serial.list()[4],9600);    //Reads the 4th PORT at 9600 baudrate
Here I have read data from the 4th port from my Arduino.
So for Example if you have COM[5] COM[2] COM[1] COM [7] COM[19]
Then the above code will read data from COM[7].

Testing:
Now since our Processing and Arduino sketch is ready, just upload the below given program to Arduino and connect your Arduino to user PC thorough programming cable and launch the game by Run the Processing code file (.pde). That's it! Move your Accelerometer and play your Ping Pong Game. The Video will guide you through the complete project.

  Demo & Code

Ping Pong Game using Arduino and Accelerometer Ping Pong Game using Arduino and Accelerometer Reviewed by XXX on สิงหาคม 27, 2560 Rating: 5

ไม่มีความคิดเห็น