Basketball

He shoots! He 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 board
  • 1 Basic Education shield
  • 1 Tinkerkit LDR
  • 1 Tinkerkit cable
  • 1 piezo speaker
  • 5 LEDs
  • 5 220 ohm resistors
  • 6 black jumper wires
  • 6 colored jumper wires
  • 1 plastic cup
  • 1 ping-pong ball
  • tape
  • scissor/knife

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 to the breadboard and connect one leg to digital pin 8 and the other to GND.
  7. Connect Tinkerkit LDR to A1 three pin connector.
  8. Connect the Arduino assembly to the computer. Upload example “LDRtest” and test lighting conditions. Open the Serial Monitor and hold the LDR vertically. Write down uncovered value.
  9. Place a ping-pong ball over the LDR. Write down covered value. Average both numbers for threshold value.
  10. Open example “Basketball”.
  11. Find the syntax line ldr.config(###, ###), changing the ### parameters to the uncovered value and threshold value respectively.
  12. Upload “Basketball” and try out the game.

  13. Cut hole on bottom of plastic cup so it fits LDR.
  14. Place the LDR in the hole, and tape it (don’t cover the LDR). Tape the plastic cup to your table, so it stands.

Code

You can find LDRTest in File -> Examples -> Castilla -> Sports -> LDRTest and the game code in File -> Examples -> Castilla -> Sports -> Basketball


How it works

The variable score will keep count on your score. Each time the LDR gets covered, it detects a value over the threshold and the score will increase with 1. The LEDs will light up on the VU-meter and a short melody will play. At the end of loop() the program checks to see if the score is equal to the amount of LEDs connected to the board (5 on this VU-meter). If it is, the program skips to startOver() where the score is reset to zero, a victory melody is played, and all the LEDs will blink. After that, the program starts over in loop().

 

Troubleshooting

  1. Refer to the illustration 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.
  2. Goal doesn’t count? Try changing the threshold value closer to the baseValue value. See reference LDR sensor for debugging LDR sensor.
  3. VU-meter does not work correctly? See reference VU-meter for debugging.

Experiment further

  • Can you add more LEDs and increase the possible score? Dont forget to add them both onto the board and into the code.
  • Not happy about the sounds the piezo plays? Make up your own melodies.
  • Change the way the LEDs blink when it’s game over. See code example VUMeterTest for the commands you can use.