Basketball

She shoots! She scores!
In this game, players will try to land a ping pong ball into a cup. Make five points to win. The score is tracked using a light dependent resistor (LDR).

Materials

  • 1 Arduino Uno board
  • 1 Education shield
  • 1 TinkerKit LDR
  • 1 piezo speaker
  • 5 LEDs
  • 5 220 ohm resistors
  • 1 TinkerKit wire
  • 6 black jumper wires
  • 6 colored jumper wires
  • 1 plastic cup
  • 1 ping-pong ball
  • tape
  • scissor/knife
Note: The LDR is introduced in Block 3. If you want to know more about how it works, read about it here.

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 wires.
  6. Connect the piezo speaker across the breadboard gap and connect one leg to digital pin 8 and the other to GND.
  7. Connect the TinkerKit LDR to the analog TinkerKit connector, A1.
  8. Cut a hole in the bottom of the plastic cup so the LDR fits.
  9. Place the LDR in the hole, and tape it (do not cover the LDR).
  10. Connect the Arduino assembly to the computer. Upload example LDRtest and test lighting conditions. Open the Serial Monitor to check the uncovered value. Write it down.
  11. Place a ping-pong ball in the cup, over the LDR. Write down covered value. Average both numbers for threshold value.
  12. Open example ‘Basketball’.
  13. Find the syntax line ldr.config(###, ###), changing the ### parameters to the uncovered value and threshold value respectively.
  14. Upload ‘Basketball’ and try out the game.

Code

Find LDRTest in File>Examples>EducationShield>Help>LDRTest and the game code in File>Examples>EducationShield>Sports>Basketball


How it works

  • The EducationShield library is included.
  • pitches.h is included, the file containing the definitions of the notes.
  • The VU-meter variables are declared, ledPins[], pinCount and vuMeter.
  • The Melody object is declared.
  • The LDR object is declared.
  • The variable score is declared.
  • In setup(), the VU-meter and ldr are configured and initialized.
  • In loop(), the program pauses until the ldr is pressed, that is when it is covered.
  • score increases with 1.
  • As many LEDs are turned on as the value of score.
  • A short melody is defined by declaring the variables melody[], noteDurations[] and numberOfNotes.
  • The melody is played.
  • The program pauses for 50 milliseconds.
  • If score is more or equal to pinCount, the number of LEDs in the VU-meter, the program jumps to the function startOver().
  • In startOver(), the score is reset by assigning the value 0 to score.
  • A melody is defined by declaring the variables melody[], noteDurations[] and numberOfNotes.
  • The melody is played.
  • All LEDs in the VU-meter blink.
  • The program jumps back to loop() and keeps looping.
 

Troubleshooting

  • Refer to the illustration and double check your connections. Make sure the shield and jumper wires are firmly connected. Check the LED polarities to make sure they are consistent and properly wired.
  • If the goal does not count, try changing the threshold value closer to the baseValue value. See reference LDR sensor for debugging LDR sensor.
  • If the VU-meter does not work correctly, see reference VU-meter for debugging.

Learn by doing

  • Try adding more LEDs and increasing the possible score. Do not forget to add them both onto the board and into the code.
  • If you are not happy about the sounds the piezo plays, make up your own melodies.
  • Change the way the LEDs blink when it is game over. See code example VUMeterTest for the commands you can use.