Launching: Arduino Handheld 1D Pong Game

in that project we'll be beholding the supplies , the code and the steps requisite to make a hand-held one dimentional pong game around a simple arduino board.

Step 1: Supplies

we'll only be needing a smattering of components :

*An arduino nano development board.

*A handful of LED's of different colors.

*Cardinal crusade buttons of diffent colors.

*Along with about 2 meters of wire , aproject boxwood to theater all the electronics , a soldering iron , solder , a bit of sensual paste And a figurer with the arduino IDE computer 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 province;    //variable for the original participant push button's state before information technology's supposed to be pressed. int state2;   //variable for the forward player button's state before information technology's supposed to be pressed. int btn1;   //shifting for the first player button's state when information technology's reputed to be pressed. int btn2;   //variable for the secondment role player button's state when IT's supposed to be ironed. int timer;    //variable for the spunky fastness. int count;    //inconstant for the pelt along counter. int score1;   //variable for the first player's score. int score2;   //varied for the ordinal 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 instrumentalist's led pin as output.   pinMode(A3,OUTPUT);   //initializing the second player's led pin every bit output.    for (int i = 0; i <= 13 ; i ++){    //setting all the pins to low , turning all the led's off.     digitalWrite(pins[i],Treble);   }     //setting the player led's pins low , turn them off.    digitalWrite(A2,HIGH);     digitalWrite(A3,Ill-smelling);     pinMode(A0, INPUT_PULLUP);   //initializing the first player's push button pin as a pulled aweigh stimulation.    pinMode(A1, INPUT_PULLUP);   //initializing the second instrumentalist's button pin atomic number 3 a pulled up input.     }  void loop() {   timing(count);    //modifying the meter 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 metre / playing the winning animation at the end of IT.   switch(x){    //reading the value of the entered variable.       //changing the time changeable reported to the counter value.     case 0:       timer = 40;     break;          case 1:       timer = 30;     break;            example 2:       timer = 30;     break;      case 3:       timer = 20;     break;      case 4:       timer = 20;     part;      case 5:       timer = 20;     break;      case 6:        timer = 10;     break in;      case 7:        timer = 10;     break;      case 8:       timekeeper = 10;     break;      case 9:          timer = 7;     break;      case 10:       timekeeper = 7;     break;      casing 11:       blinck(200);    //blinks tll the led's double.       blinck(200);      if(score2 < score1){   //if the player 1 wins , flash his LED.       victory(1);       }      if(score1 < score2){    //if the actor 1 wins , flash his led.       victory(2);       }       //fix each the variables.      count = 0;      score1 = 0;      score2 = 0;     come apart;        } }  vacuum animation(){   //main function , for enlivening the light-emitting diode's and checking the buttons for presses.   Forward_Animation(11);    //plays the forward animation for the first 11 led's.   digitalWrite(pins[12],Inferior);    Before_Check(1);   //check over if the button was pressed before it's obligated to , in which character , cancel the try.     postponement(timer);   digitalWrite(pins[12],HIGH);   delay(timer);   digitalWrite(pins[13],LOW);     check(2);   //handicap if the button was pressed at the beginning.     postponement(timer/2);      check(2);   //re-correspond if the clit was pressed half-way throught.   state = treacherously;    //touch on the values.      delay(timekeeper/2);   digitalWrite(pins[13],HIGH);   wait(timer);      Backward_Animation(11);    //playing the regardant living for the last 11 led's.      digitalWrite(pins[1],LOW);     Before_Check(2);    //check if the button was ironed before IT's putative to , in which case , cancel the try.      delay(timer);   digitalWrite(pins[1],HIGH);   delay(timekeeper);   digitalWrite(pins[0],LOW);    check(1);    //check if the button was pressed at the source.     delay(timekeeper/2);    check(1);    //re-check if the button was pressed half-way throught.  state2 = false;    //reestablish the values.    delay(timekeeper/2);  digitalWrite(pins[0],HIGH);  delay(timer); }  void Forward_Animation(int x){    //mathematical function for playing the forward animation for the speciffied numerate of led's.   for (int i = 0; i <= x ; i ++){   //blinking the light-emitting diode's for the speciffied number of multiplication.     digitalWrite(i,Lowset);     delay(timer);     digitalWrite(i,HIGH);     delay(timekeeper);   } }  void Backward_Animation(int y){   //procedure for playing the backward brio 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,Sir David Low);      delay(timer);      digitalWrite(i,HIGH);      delay(timer);    } }  nihility Before_Check(int num){   //function for checking if the button was pressed before it was supposed to , to set off the try.   if (num == 1){    //if IT's for player one , then set the player one parametrs.     btn2 = digitalRead(A1);     if(btn2 == 0){       state = right;     }   }    if (num == 2){    //if it's for player one , then set the player one parametrs.     btn1 = digitalRead(A0);     if(btn1 == 0){       state2 = sure;     }   } }  null check(int numerate){   //work for checking if the button was pressed when it should.   if (number == 1){   //if it's for the player enumerate one.     btn1 = digitalRead(A0);   //attach the body politic of the clit to the variable.   if(state2 == false){    //if the button wasn't pressed prier.      if(btn1 == 0){   //if the button is ironed.      blinck(200);   //blink the lights.      tally(2);    //brassy the light-emitting diode for the instrumentalist that pressed the button.      count++;   //transition to the next speed.      score1++;    //adding a point to the player score.     }      }   }    if (issue == 2){   //if it's for the player bi one.     btn2 = digitalRead(A1);   //attach the DoS of the button to the variable.   if(state == false){   //if the button wasn't pressed prier.     if(btn2 == 0){    //if the push button is pressed.     blinck(200);    //blink the lights.     nock(1);   //flash the LED for the role player that pressed the button.     count++;     //modulation to the next hurrying.     score2++;   //adding a point to the player score.    }     }   } }  void blinck(int x){   //social occasion for bliking all the led's for a careful sum of time.  for(int i = 0; i <= 13; i++){    //turning all the led's on one by ane.   digitalWrite(i,Contemptible);  }   delay(x);   //waiting some time.   for(int i = 0; i <= 13; i++){   //turning all the led's off combined by one.   digitalWrite(i,HIGH);  }   delay(x);   //waiting some time.   for(int i = 0; i <= 13; i++){    //turning entirely the led's on one by one.   digitalWrite(i,LOW);  }   postponement(x);   //wait some time.   for(int i = 0; i <= 13; i++){   //turning all the LED's off individually.   digitalWrite(i,HIGH);  }   hold(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 light-emitting diode along and off with a delay in between.   digitalWrite(A2,LOW);   holdup(100);   digitalWrite(A2,HIGH);   delay(100);    digitalWrite(A2,Broken);   delay(100);   digitalWrite(A2,HIGH);   holdup(50);   }   if (playa == 2){    //if it's the player number ii.     //turns the led off and on with a delay in between.   digitalWrite(A3,LOW);   delay(100);   digitalWrite(A3,HIGH);   stay(100);    digitalWrite(A3,LOW);   delay(100);   digitalWrite(A3,HIGH);   delay(50);   } }  nullity victory(int player){   //function for blinking atomic number 90 ewinner player' LED.   if(role player == 1){    //if it's the first participant.     //turns the led on and slay with a yearner delay in between.   digitalWrite(A2,LOW);   delay(750);   digitalWrite(A2,Mellow);   stay(750);   }    if(player == 2){    //if it's the second role player.     //turns the led along and bump off with a longer delay in between.   digitalWrite(A3,LOW);   delay(750);   digitalWrite(A3,HIGH);   delay(750);   } }

Footprint 3: Encode Explination

the code is self explanitory , all needful data , along with the wiring method is explained thoroughly in the file , throught comments.

Step 4: The Wiring Plot

The wiring job isn't difficult , just follow the above wiring diagram , and exist aware victimization a soldering Fe.

Step out 5: The Wiring Finish Result

at the end , you sould get something resembling the above images , re-check all your connections for short-term circuits earlier powering the microcontroller.

Step 6: The Whole End Result

with a bit of care , your project should social function correctly , and look something suchlike the in a higher place image.

Be the First to Share

Recommendations

  • Anything Goes Contest 2022

    Anything Goes Contest 2022