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
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
andvuMeter
. - 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[]
andnumberOfNotes
. - The melody is played.
- The program pauses for 50 milliseconds.
- If
score
is more or equal topinCount
, the number of LEDs in the VU-meter, the program jumps to the functionstartOver()
. - In
startOver()
, the score is reset by assigning the value 0 toscore
. - A melody is defined by declaring the variables
melody[]
,noteDurations[]
andnumberOfNotes
. - 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 thebaseValue
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.