Pong

Play a simplified version of the classic arcade game, Pong!
In this game, a “pong” will move across five LEDs (VU-meter) and bounce back and forth as players press the button. Players must press the button at the right time in order to return the pong.

Materials

  • 1 Arduino Uno board
  • 1 Education Shield
  • 2 TinkerKit buttons
  • 5 LEDs
  • 5 220 ohm resistors
  • 2 TinkerKit wires
  • 5 black jumper wires
  • 5 colored jumper wires

Instructions

  1. Attach the shield onto the top of the Arduino board.
  2. Connect five LEDs across the breadboard gap.
  3. Connect a 220 ohm resistor to digital pin 2. Connect the resistor to the long leg of the first LED.
  4. Connect each of the digital pins 3 through 6 to a corresponding LED following the same method.
  5. Connect the short leg of the LEDs to an Arduino GND pin using black jumper wires.
  6. Connect the Tinkerkit buttons to D9 and D10.
  7. Connect the Arduino assembly to the computer, upload example Pong and try the game.

Code

Find the code in File>Examples>EducationShield>Block2-Sports>Projects>Pong

How it works

  • The EducationShield library is included.
  • The VU-Meter variables are declared, ledPins[], pinCount and vuMeter.
  • The button objects are declared, button1 and button2.
  • Variables having to do with time and pressing the buttons are declared, ledTime, pressTime and buttonNotPressed.
  • In setup(), the VU-meter and buttons are configured and initialized.
  • The game starts with the last line of code in setup(); the VU-meter scrolls left.
  • In loop(), button1 is checked to be released within the preset time frame, pressTime. The reason you want to check for the button being released and not pressed is that this way you can not cheat by keeping the button pressed at all times.
  • If button1 is released in time, the VU-meter scrolls right.
  • button2 is checked to be released within the preset time frame.
  • If button2 is released in time, the VU-meter scrolls left again.
  • Whenever a button is not released in time the number of the button is stored in buttonNotPressed, then the program jumps to the function gameOver().
  • In gameOver(), all LEDs in the VU-meter blinks.
  • If button1 was not released in time the VU-meter scrolls right. If button2 was not released in time, the VU-meter scrolls left.
  • After gameOver() the program jumps back to loop(), at the same place where it was left, and keeps running.
 

Troubleshooting

  • Refer to the illustrations and double check your connections. Make sure the shield and wires are firmly connected. Check the LED polarities to make sure they are consistent and properly wired.
  • If you can not hit the pong, try swapping your buttons. If it still doesn’t work, see reference for debugging buttons.
  • If the VU-meter does not work correctly, see reference for debugging the VU-meter.

Learn by doing

  • Change the way the LED blinks when it’s game over. See the VU-meter reference for the commands you can use.
  • Design and build a box for the game. Poke the LEDs through the top of the box or cover with a translucent material. Mount the buttons on the box.