Ping Pong has been a popular game. Sometimes back the game was available on game consoles, the desktop and mobile versions of the game also saw the light of day as the desktop computers and smartphones became rage age after the age. In this project, a desktop version of the game will be developed with the difference that the paddles of the game will be controlled through switches instead of by dragging them through the mouse.
The desktop versions of the game usually allow a player to play against the computer and the player could control a paddle with the help of mouse. Since only a single mouse device can interface with the computer at a time, a multiplayer desktop version of the game cannot be realized using the mouse. In the project, an Arduino based gamepad will be designed so that two players could play the game against each other. The desktop game itself will be designed using PROCESSING language.
In the ping pong game, a ball strikes between two opposite walls. There are paddles given at the walls which are usually controlled by a player and the computer. A player has to avoid the ball to collide with the wall by striking it back by the paddle by moving it either horizontally or vertically. If the ball collides with the wall, the opposite player or computer gains a point. There is a game over once one of the players (usually either a computer or human player) gains a certain number of points. The player who gains a certain number of points first by more successfully avoiding the ball to collide with its wall is declared a winner when the game is over.

Fig. 1: Screenshot of Arduino based Ping Pong Game
Processing is an open source programming language for computers with an IDE available for its communities. The development of the language started in the year 2001 in MIT Media Lab USA, by Casey and Benjamin Fry. In 2012, the language along with its IDE was launched, credits to Daniel Shiffman, who joined the open-source project and turned around to be main lead. The language is developed with an aim to teach computer programming in a visual context for electronics and media arts along with strengthening the foundation of electronics projects. The open-source software package includes a sketchbook and an add-on for organizing projects.
The language is based on a java class which implements most of the processing language’s features and allows programmers to make user-defined classes within the PApplet sketch. The game designed using Processing Language runs on desktop and could communicate with the Arduino based gamepad through serial communication. The game developed in the project has the ball striking in a horizontal direction with the provision of paddle’s movement in vertical direction.

Fig. 2: Prototype of Arduino based Ping Pong Game
The Arduino based gamepad will interface with the computer using a virtual serial port. The gamepad has four switches, two each for both players to move the paddle either up or down. The pad will have four LEDs as well to give a visual hint of the button pressed by the user.
Components Required –
1. Arduino UNO
2. Push-to-ON switches -4pcs
3. LED’s
4. 1K ohm Resistors
Software Tools Required –
1. PROCESSING language and IDE
2. Arduino IDE
3. AVR Dude
Block Diagram –

Fig. 3: Block Diagram of Arduino based Ping Pong Game
Circuit Connections –
The circuit is built on Arduino UNO. The project device work as 2-player gamepad. There are four tactile switches connected to the Arduino board that serve as the gamepad buttons. The switches are connected at following Arduino pins with stated functions assigned to them –

Fig. 4: Table listing pin functions of Arduino Uno for Ping Pong Game
The Arduino buttons by default are connected to VCC through 1K Ω pull-up resistors and short circuit to ground via tactile switches interfaced to them. So, on pressing a switch, the Arduino sketch detects a LOW logic from the respective pin. There are four LEDs connected to pins 3, 4, 5 and 6 of the Arduino board which indicate the following player actions –

Fig. 5: Table listing pin functions of Arduino Uno for Ping Pong Game
The LEDs are interfaced with their anode connected the Arduino pins and cathode connected to ground. Therefore, when a HIGH logic is an output by an Arduino pin, the respective LED glows.
Power Supply – The gamepad can be powered by any battery. The DC voltage of the used battery is regulated to 5V DC using 7805 IC. The IC has three pins – pin 1 should be connected to the anode of the battery, pin 2 and 3 with the cathode (common ground). The 5V DC should be drawn from the pin 3 of the IC. An LED along with a 10K Ω pull-up resistor can also be connected between common ground and output pin to get a visual hint of supply continuity.
How the circuit works
Once the gamepad is connected to a battery, the Arduino sketch loads the required libraries and wait for a key press which is detected by the input of LOW logic at the respective Arduino pins. On detecting a key press, the Arduino sketch sends an integer between 1 and 4 to the computer through the virtual serial port and set a HIGH logic at the corresponding LED connected pin to indicate user action. The Arduino sends the following numeric values on virtual serial port on the key press at the respective pins –
Fig. 6: Table listing Arduino pins and corresponding serial messages
The processing language code on desktop creates the GUI of the game and the application created waits for reception of an integer value from the virtual serial port. On receiving a numeric value between 1 and 4, it moves the either paddle on the screen up or down. The processing language code detects if the ping pong ball collides with a wall and increases a point of the opposite player. If any one of the players gets 4 points first, the game is terminated and the player to gain the points first is declared the winner.
Programming Guide –
Arduino Sketch
The Arduino code is burnt to the Arduino UNO. It detects the user actions by tactile switches and operates LEDs and send a number on virtual serial port for gamepad functioning. First the required libraries need to be imported for sending data on virtual serial port.
#include <SoftwareSerial.h>// import the serial library
Integer variables are declared and assigned to pins having tactile switches and LEDs interfaced to them.
//Set Pin Numbers
const int button1 = 10;
const int button2 = 11;
const int button3 = 8;
const int button4 = 9;
const int led1 = 3;
const int led2 = 4;
const int led3 = 5;
const int led4 = 6;
A setup() function is called in which the serial transmission rate is set to 9600 bits per seconds using Serial.begin() function. The LED connected pins are set to digital output and switch connected pins are set to digital input using pinMode() function.
Void setup() {
//Initialize Serial Communication at 9600 bits per second
Serial.begin(9600);
//Initialize the LED pin as an output
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
//Initialize the pushbutton pin as an input
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
}
A loop() function is called in which the user input through pushbuttons is detected using digitalRead() function and compared to LOW logic. On comparison from each switch, respective number is serially transmitted using Serial.Write() function and respective LED is set to ON by sending a HIGH logic using digitalWrite() function. The default case is set to send number 0 on serial port and set all the LEDs OFF.
Void loop() {
if(digitalRead(button1)==0)
Serial.write(1);
digitalWrite(led1, HIGH);
}
else if(digitalRead(button2)==0)
Serial.write(2);
digitalWrite(led2, HIGH);
}
else if(digitalRead(button3)==0)
Serial.write(3);
digitalWrite(led3, HIGH);
}
else if(digitalRead(button4)==0)
Serial.write(4);
digitalWrite(led4, HIGH);
}
else {
Serial.write(0);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
}
delay(400);
}
Processing Language Sketch
In the processing language code first, the serial library is imported and an object of a serial class is declared. The variables are declared to hold the paddle dimensions and position on the screen. Variables to hold player scores and color values for left and right paddle in the RGB format are declared.

Fig. 7: Screenshot of initialization in Processing Language Code for Arduino based Ping Pong Game
Similar to Arduino code, a setup function is declared in which baud rate for serial communication is set and functions defining screen size, text size, text alignment and text position are called. The size and position of the paddle are declared with respect to the screen size and speed of the ball in X and Y axis is pre-defined.

Fig. 8: Screenshot of Setup Function in Processing Language Code for Arduino based Ping Pong Game
A draw() function is called in which the functions creating the graphic interface of the game and functions detecting user action and implementing a graphical and logical change accordingly are called.

Fig. 9: Screenshot of Draw Function in Processing Language Code for Arduino based Ping Pong Game
The movingPaddle() function fill the paddles with assigned colors and detects number received on the virtual serial port to change the position of paddles vertically.

Fig. 10: Screenshot of Moving Paddle Function in Processing Language Code for Arduino based Ping Pong Game
The paddleCONCT() function detects the contact of the ball with the paddle by manipulating paddle and ball position on the screen. A similar way, the bounceBack() function detects contact of the ball with the screen wall.

Fig. 11: Screenshot of PaddleCONCT Function in Processing Language Code for Arduino based Ping Pong Game
The Score() function updates the score on screen by displaying the values of variables ScoreL and ScoreR. The gameOver() function detects if either player has got the winning score. The gameOverPage() function initializes the variables holding the position of the ball and the paddle to default values and displays the winner on screen. The function initiates a game restart on mouse click.

Fig. 12: Screenshot of Score Function in Processing Language Code for Arduino based Ping Pong Game
The movingBall() function moves the ball (circle) at a speed specified in speedX and speedY variables. The restart() function sets the variables to initial values for a game restart.

Fig. 13: Screenshot of movingBall Function in Processing Language Code for Arduino based Ping Pong Game
Project Source Code
###
//Program to /* * Program for filtering the X-axis values of accel and transmitting it serially * Programmed by B.Aswinth Raj * Dated: 21-08-206 */ #include <SoftwareSerial.h>// import the serial library #include <LiquidCrystal.h>//import the LCD library SoftwareSerial testserial(10, 11); // RX, TX LiquidCrystal lcd(13, 12, 5, 4, 3, 2);// Pins used for RS,E,D4,D5,D6,D7 #define AccelPin A0 // A0 is connected to X-axis of Accel #define Samplesize 13 // filterSample number int BTData; // the data received from the app store here int Array1 [Samplesize]; // array for holding raw sensor values for sensor int rawData1, smoothData1; // variables for sensor data int toSend; void setup(){ Serial.begin(9600); testserial.begin(9600); //Software serial initialization lcd.begin(16,2);//LCD 16x2 initialization lcd.setCursor(0,0); //Initially set the cursor position of LCD to 1st Columb 1st row. lcd.print("Engineers Garage");//After initialising print data lcd.setCursor(0,1); //Initially set the cursor position of LCD to 1st Columb 2nd row. lcd.print(" "); //print blank to clear all the data on LCD delay(3000); lcd.setCursor(0,0); lcd.print(" BlueTooth Game "); lcd.setCursor(0,1); lcd.print(" Accelerometer "); delay(3000); } void loop() { rawData1 = analogRead(AccelPin); // read X-axis of accelerometer //BTData=testserial.read(); // Read the dat store it in the variable. smoothData1 = digitalSmooth(BTData, Array1); toSend = map (smoothData1, 193, 280, 0, 255); // the data from accelerometer mapped to form a byte Serial.write (toSend); delay(100); } int digitalSmooth(int rawIn, int *sensSmoothArray){ // "int *sensSmoothArray" passes an array to the function - the asterisk indicates the array name is a pointer int j, k, temp, top, bottom; long total; static int i; static int sorted[Samplesize]; boolean done; i = (i + 1) % Samplesize; // increment counter and roll over if necc. - % (modulo operator) rolls over variable sensSmoothArray[i] = rawIn; // input new data into the oldest slot for (j=0; j<Samplesize; j++){ // transfer data array into anther array for sorting and averaging sorted[j] = sensSmoothArray[j]; } done = 0; // flag to know when we're done sorting while(done != 1){ // simple swap sort, sorts numbers from lowest to highest done = 1; for (j = 0; j < (Samplesize - 1); j++){ if (sorted[j] > sorted[j + 1]){ // numbers are out of order - swap temp = sorted[j + 1]; sorted [j+1] = sorted[j] ; sorted [j] = temp; done = 0; } } } bottom = max(((Samplesize * 15) / 100), 1); top = min((((Samplesize * 85) / 100) + 1 ), (Samplesize - 1)); // the + 1 is to make up for asymmetry caused by integer rounding k = 0; total = 0; for ( j = bottom; j< top; j++){ total += sorted[j]; // total remaining indices k++; } return total / k; // divide by number of samples }
###
Circuit Diagrams
Project Video
Filed Under: Electronic Projects
Filed Under: Electronic Projects
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.