Introduction: Arduino Handheld 1D Pong Game
in this project we'll be seeing the supplies , the code and the steps needed to make a handheld one dimentional pong game around a simple arduino board.
Step 1: Supplies
we'll only be needing a handful of components :
*An arduino nano development board.
*A handful of led's of different colors.
*Two push buttons of diffent colors.
*Along with about 2 meters of wire , aproject box to house all the electronics , a soldering iron , solder , a bit of hot glue And a computer with the arduino IDE software installed.
Step 2: The Code
int pins[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13}; //the main led pins.
int state; //variable for the first player button's state before it's supposed to be pressed.
int state2; //variable for the second player button's state before it's supposed to be pressed.
int btn1; //variable for the first player button's state when it's supposed to be pressed.
int btn2; //variable for the second player button's state when it's supposed to be pressed.
int timer; //variable for the game speed.
int count; //variable for the speed counter.
int score1; //variable for the first player's score.
int score2; //variable for the second player's score.
void setup() {
for (int i = 0; i <= 13 ; i ++){ //initializing all the pins as outputs.
pinMode(pins[i],OUTPUT);
}
pinMode(A2,OUTPUT); //initializing the first player's led pin as output.
pinMode(A3,OUTPUT); //initializing the second player's led pin as output.
for (int i = 0; i <= 13 ; i ++){ //setting all the pins to low , turning all the led's off.
digitalWrite(pins[i],HIGH);
}
//setting the player led's pins low , turning them off.
digitalWrite(A2,HIGH);
digitalWrite(A3,HIGH);
pinMode(A0, INPUT_PULLUP); //initializing the first player's button pin as a pulled up input.
pinMode(A1, INPUT_PULLUP); //initializing the second player's button pin as a pulled up input.
}
void loop() {
timing(count); //modifying the time depending on the stage of the game.
animation(); //the main game function.
}
void timing(int x){ //function for increasing the speed of the game each time / playing the winning animation at the end of it.
switch(x){ //reading the value of the entered variable.
//changing the time variable according to the counter value.
case 0:
timer = 40;
break;
case 1:
timer = 30;
break;
case 2:
timer = 30;
break;
case 3:
timer = 20;
break;
case 4:
timer = 20;
break;
case 5:
timer = 20;
break;
case 6:
timer = 10;
break;
case 7:
timer = 10;
break;
case 8:
timer = 10;
break;
case 9:
timer = 7;
break;
case 10:
timer = 7;
break;
case 11:
blinck(200); //blinks tll the led's twice.
blinck(200);
if(score2 < score1){ //if the player 1 wins , flash his led.
victory(1);
}
if(score1 < score2){ //if the player 1 wins , flash his led.
victory(2);
}
//restore all the variables.
count = 0;
score1 = 0;
score2 = 0;
break;
}
}
void animation(){ //main function , for animating the led's and checking the buttons for presses.
Forward_Animation(11); //plays the forward animation for the first 11 led's.
digitalWrite(pins[12],LOW);
Before_Check(1); //check if the button was pressed before it's supposed to , in which case , cancel the try.
delay(timer);
digitalWrite(pins[12],HIGH);
delay(timer);
digitalWrite(pins[13],LOW);
check(2); //check if the button was pressed at the beginning.
delay(timer/2);
check(2); //re-check if the button was pressed half-way throught.
state = false; //restore the values.
delay(timer/2);
digitalWrite(pins[13],HIGH);
delay(timer);
Backward_Animation(11); //playing the backward animation for the last 11 led's.
digitalWrite(pins[1],LOW);
Before_Check(2); //check if the button was pressed before it's supposed to , in which case , cancel the try.
delay(timer);
digitalWrite(pins[1],HIGH);
delay(timer);
digitalWrite(pins[0],LOW);
check(1); //check if the button was pressed at the beginning.
delay(timer/2);
check(1); //re-check if the button was pressed half-way throught.
state2 = false; //restore the values.
delay(timer/2);
digitalWrite(pins[0],HIGH);
delay(timer);
}
void Forward_Animation(int x){ //function for playing the forward animation for the speciffied number of led's.
for (int i = 0; i <= x ; i ++){ //blinking the led's for the speciffied number of times.
digitalWrite(i,LOW);
delay(timer);
digitalWrite(i,HIGH);
delay(timer);
}
}
void Backward_Animation(int y){ //function for playing the backward animation for the speciffied number of led's.
int z = (13 - y);
for (int i = 12; i >= y ; i= i-1){ //blinking the led's for the speciffied number of times.
digitalWrite(i,LOW);
delay(timer);
digitalWrite(i,HIGH);
delay(timer);
}
}
void Before_Check(int num){ //function for checking if the button was pressed before it was supposed to , to cancel the try.
if (num == 1){ //if it's for player one , then set the player one parametrs.
btn2 = digitalRead(A1);
if(btn2 == 0){
state = true;
}
}
if (num == 2){ //if it's for player one , then set the player one parametrs.
btn1 = digitalRead(A0);
if(btn1 == 0){
state2 = true;
}
}
}
void check(int number){ //function for checking if the button was pressed when it should.
if (number == 1){ //if it's for the player number one.
btn1 = digitalRead(A0); //attach the state of the button to the variable.
if(state2 == false){ //if the button wasn't pressed prier.
if(btn1 == 0){ //if the button is pressed.
blinck(200); //blink the lights.
score(2); //flash the led for the player that pressed the button.
count++; //transition to the next speed.
score1++; //adding a point to the player score.
}
}
}
if (number == 2){ //if it's for the player number one.
btn2 = digitalRead(A1); //attach the state of the button to the variable.
if(state == false){ //if the button wasn't pressed prier.
if(btn2 == 0){ //if the button is pressed.
blinck(200); //blink the lights.
score(1); //flash the led for the player that pressed the button.
count++; //transition to the next speed.
score2++; //adding a point to the player score.
}
}
}
}
void blinck(int x){ //function for bliking all the led's for a certain amount of time.
for(int i = 0; i <= 13; i++){ //turning all the led's on one by one.
digitalWrite(i,LOW);
}
delay(x); //waiting some time.
for(int i = 0; i <= 13; i++){ //turning all the led's off one by one.
digitalWrite(i,HIGH);
}
delay(x); //waiting some time.
for(int i = 0; i <= 13; i++){ //turning all the led's on one by one.
digitalWrite(i,LOW);
}
delay(x); //waiting some time.
for(int i = 0; i <= 13; i++){ //turning all the led's off one by one.
digitalWrite(i,HIGH);
}
delay(x); //waiting some time.
}
void score(int playa){ //function for blinking the led of the player that gained a point.
if (playa == 1){ //if it's the player number one.
//turns the led on and off with a delay in between.
digitalWrite(A2,LOW);
delay(100);
digitalWrite(A2,HIGH);
delay(100);
digitalWrite(A2,LOW);
delay(100);
digitalWrite(A2,HIGH);
delay(50);
}
if (playa == 2){ //if it's the player number two.
//turns the led on and off with a delay in between.
digitalWrite(A3,LOW);
delay(100);
digitalWrite(A3,HIGH);
delay(100);
digitalWrite(A3,LOW);
delay(100);
digitalWrite(A3,HIGH);
delay(50);
}
}
void victory(int player){ //function for blinking th ewinner player' led.
if(player == 1){ //if it's the first player.
//turns the led on and off with a longer delay in between.
digitalWrite(A2,LOW);
delay(750);
digitalWrite(A2,HIGH);
delay(750);
}
if(player == 2){ //if it's the second player.
//turns the led on and off with a longer delay in between.
digitalWrite(A3,LOW);
delay(750);
digitalWrite(A3,HIGH);
delay(750);
}
}Attachments
Step 3: Code Explination
the code is self explanitory , all needed information , along with the wiring method is explained thoroughly in the file , throught comments.
Step 4: The Wiring Diagram
The wiring job isn't difficult , just follow the above wiring diagram , and be careful using a soldering iron.
Step 5: The Wiring End Result
at the end , you sould have something resembling the above images , re-check all your connections for short circuits before powering the microcontroller.
Step 6: The Overall End Result
with a bit of care , your project should function correctly , and look something like the above image.





