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
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
andvuMeter
. - The button objects are declared,
button1
andbutton2
. - Variables having to do with time and pressing the buttons are declared,
ledTime
,pressTime
andbuttonNotPressed
. - 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 functiongameOver()
. - In
gameOver()
, all LEDs in the VU-meter blinks. - If
button1
was not released in time the VU-meter scrolls right. Ifbutton2
was not released in time, the VU-meter scrolls left. - After
gameOver()
the program jumps back toloop()
, 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.