Ping Pong Game Using Arduino
Ping Pong Game Using Arduino
Why Arduino?
Thanks to its simple and accessible user experience, Arduino has been used in
thousands of different projects and applications. The Arduino software is easy-
to-use for beginners, yet flexible enough for advanced users. It runs on Mac,
Windows, and Linux. Teachers and students use it to build low cost scientific
instruments, to prove chemistry and physics principles, or to get started with
programming and robotics.
Table tennis, also known as Ping Pong, is a game in which two or four
players hit a little, light ball back and forth across a table using a small
paddle. A point is scored when a player fails to return the ball to the other
player.
Components Used:
LCD Display (16*2)
Connecting Wires
Resistors (10K ohms * 4 resistors for push buttons; 220
ohms for Lcd)
Push Buttons (4)
POT(Variable Resistor )
Interfacing LCD with Arduino:
Circuit Connection:
Code:
#include<LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
void setup()
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("First Row");
void loop()
}
Operating Paddles of Ping Pong game using Push
Buttons:
Circuit Connection:
Code:
#include <LiquidCrystal.h>
// character printing
//
byte aiPaddlePos = 6;
byte myPaddlePos = 6;
int rightButState = 0;
int leftButState = 0;
int rightLastButState = 0;
int leftLastButState = 0;
void ClearPaddles(){
aiPaddleArray[i][j] = 0;
myPaddleArray[i][j] = 0;
void PrintPaddles(){
// Grab the character array stored in myPaddleArray using a pointer
lcd.createChar(0, mPTemp1);
lcd.createChar(1, mPTemp2);
lcd.createChar(2, aiPTemp1);
lcd.createChar(3, aiPTemp2);
lcd.setCursor(14, 0);
lcd.write(byte(0));
lcd.setCursor(14, 1);
lcd.write(byte(1));
lcd.setCursor(1, 0);
lcd.write(byte(2));
lcd.setCursor(1, 1);
lcd.write(byte(3));
Serial.println(mPTemp1[i]);
void SetupPaddles(){
ClearPaddles();
myPaddleArray[0][5] = 16;
myPaddleArray[0][6] = 16;
myPaddleArray[0][7] = 16;
aiPaddleArray[0][5] = 1;
aiPaddleArray[0][6] = 1;
aiPaddleArray[0][7] = 1;
PrintPaddles();
void MovePaddleUp(){
if(myPaddlePos != 1){
myPaddlePos--;
UpdatePaddlesAfterMove();
void MovePaddleDown(){
if(myPaddlePos != 14){
myPaddlePos++;
UpdatePaddlesAfterMove();
}
void UpdatePaddlesAfterMove(){
// Cycle through all bools turning on the next 1 above and off the 1
below
myPaddleColArray[i] = 16;
} else {
myPaddleColArray[i] = 0;
myPaddleArray[0][j] = myPaddleColArray[j];
myPaddleArray[1][k-8] = myPaddleColArray[k];
void setup() {
Serial.begin(9600);
// State that the display has 2 rows with 16
lcd.begin(16, 2);
pinMode(moveRightButton, INPUT);
pinMode(moveLeftButton, INPUT);
SetupPaddles();
void loop() {
rightButState = digitalRead(moveRightButton);
leftButState = digitalRead(moveLeftButton);
if(rightButState != rightLastButState){
// Check if button was pressed
if(rightButState == HIGH){
MovePaddleUp();
PrintPaddles();
delay(50);
rightLastButState = rightButState;
if(leftButState != leftLastButState){
if(leftButState == HIGH){
MovePaddleDown();
PrintPaddles();
delay(50);
leftLastButState = leftButState;
}
Moving Ball in Game:
Circuit diagram is same as above
Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Stores scores
byte playerScore = 0;
byte aiScore = 0;
// Ball steps
// 1. Ball starts moving to the right
// 2. With each move
// A. Delete previous space
// B. Light new space
// C. Check if paddle is in new space
// i. Check if ball hits PaddlePos, above PaddlePos, or below Paddle Pos
// ii. Change ballXDir & ballYDir accordingly
// a. PaddlePos : -XDir, YDir = 0
// b. PaddlePos - 1 : -XDir, YDir - 1
// c. PaddlePos + 1 : -XDir, YDir + 1
// iii. Clear old ball
// D. Check if top wall is hit
// i. XDir not changed, YDir - 1
// ii. Clear old ball
// E. Check if bottom wall is hit
// i. XDir not changed, YDir + 1
// ii. Clear old ball
// D. Check if passes paddle
// i. Change score
// ii. Play sound
// iii. Draw ball in center of board
// iv. Clear old ball
// v. YDir = 0, XDir = -XDir
byte charNum = 0;
void PrintBall(){
// Start at X: 7 Y: 35
void SetupBall(){
// Send ball in opposite direction
ballYDir *= -1;
void AwardAPoint(){
if(ballY <= 8){
playerScore++;
} else {
aiScore++;
}
delay(100);
void UpdateBall(){
PrintBall();
}
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
SetupBall();
GenerateBallArray();
PrintBall();
}
void loop() {
UpdateBall();
}
Final Game Code
I have tried to bring the best out of me by making this code, Still there is
some Synchronisation issue which I was unable to resolve at my level.
Someone Copied my code from tinkercard, Thus I request you not to
disqualify me if found same code somewhere.
Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void ClearPaddles(){
for(int i = 0; i < 2; i++){
for(int j = 0; j < 8; j++){
aiPaddleArray[i][j] = 0;
myPaddleArray[i][j] = 0;
}
}
}
void PrintStuff(){
lcd.clear();
byte charNum = 0;
void PrintPaddles(){
byte* mPTemp1 = myPaddleArray[0];
byte* mPTemp2 = myPaddleArray[1];
byte* aiPTemp1 = aiPaddleArray[0];
byte* aiPTemp2 = aiPaddleArray[1];
lcd.createChar(0, mPTemp1);
lcd.createChar(1, mPTemp2);
lcd.createChar(14, aiPTemp1);
lcd.createChar(15, aiPTemp2);
lcd.setCursor(14, 0);
lcd.write(byte(0));
lcd.setCursor(14, 1);
lcd.write(byte(1));
lcd.setCursor(1, 0);
lcd.write(byte(14));
lcd.setCursor(1, 1);
lcd.write(byte(15));
}
void SetupPaddles(){
ClearPaddles();
myPaddleArray[0][5] = 16;
myPaddleArray[0][6] = 16;
myPaddleArray[0][7] = 16;
aiPaddleArray[0][5] = 1;
aiPaddleArray[0][6] = 1;
aiPaddleArray[0][7] = 1;
PrintPaddles();
}
void MovePaddleUp(){
if(myPaddlePos != 1){
myPaddlePos--;
aiPaddlePos--;
UpdatePaddlesAfterMove();
}
}
void MovePaddleDown(){
if(myPaddlePos != 14){
myPaddlePos++;
aiPaddlePos++;
UpdatePaddlesAfterMove();
}
}
void UpdatePaddlesAfterMove(){
for(int i = 0; i < 16; i++){
if((i == (myPaddlePos-1)) || (i == myPaddlePos) || (i ==
(myPaddlePos+1))){
myPaddleColArray[i] = 16;
aiPaddleColArray[i] = 1;
} else {
myPaddleColArray[i] = 0;
aiPaddleColArray[i] = 0;
}
}
for(int j = 0; j < 8; j++){
myPaddleArray[0][j] = myPaddleColArray[j];
aiPaddleArray[0][j] = aiPaddleColArray[j];
}
for(int k = 8; k < 16; k++){
myPaddleArray[1][k-8] = myPaddleColArray[k];
aiPaddleArray[1][k-8] = aiPaddleColArray[k];
}
}
void GenerateBallArray(){
void SetupBall(){
ballYDir *= -1;
gameBoard[7][35] = true;
}
void AwardAPoint(){
if(ballY <= 8){
playerScore++;
} else {
aiScore++;
}
delay(100);
ballYDir *= -1;
}
void UpdateBall(){
delay(ballUpdateTime);
if((ballY <= 8) || (ballY >= 71)){
AwardAPoint();
} else if((ballX == 0) || (ballX == 15)){
ballXDir *= -1;
} else if((ballY == 69) && (ballX == myPaddlePos)){
ballYDir *= -1;
} else if((ballY == 69) && (ballX == (myPaddlePos + 1))){
ballYDir *= -1;
ballXDir = 1;
} else if((ballY == 69) && (ballX == (myPaddlePos - 1))){
ballYDir *= -1;
ballXDir = -1;
}
gameBoard[ballX][ballY] = false;
ballX += ballXDir;
ballY += ballYDir;
gameBoard[ballX][ballY] = true;
GenerateBallArray();
PrintStuff();
}
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(moveRightButton, INPUT);
pinMode(moveLeftButton, INPUT);
lcd.print("Game Loading..");
delay(1500);
lcd.clear();
SetupBall();
GenerateBallArray();
SetupPaddles();
PrintStuff();
}
void loop() {
UpdateBall();
rightButState = digitalRead(moveRightButton);
leftButState = digitalRead(moveLeftButton);
if(rightButState != rightLastButState){
if(rightButState == HIGH){
MovePaddleUp();
PrintStuff();
}
delay(50);
rightLastButState = rightButState;
}
if(leftButState != leftLastButState){
if(leftButState == HIGH){
MovePaddleDown();
PrintStuff();
}
delay(50);
leftLastButState = leftButState;
}
Certificate
I hereby Certify that the above work Is my own and is not copied.