En garde! Test your reaction time against an opponent!
In this game, two players will hold tilt switch swords. When the green LED randomly lights up, the first person to swing their sword wins.
Materials
- 1 Arduino Uno board
- 1 Education Shield
- 2 TinkerKit tilt switches
- 1 big red LED
- 1 big green LED
- 2 big yellow LEDs
- 4 220 ohm resistors
- 2 TinkerKit long wires
- 4 black jumper wires
- 4 colored jumper wires
Instructions
Code
Find the code in File>Examples>EducationShield>Block2-Sports>Projects>Fencing
How it works
- The EducationShield library is included.
- 4 constants are defined to hold the index numbers of the LEDs in the VU-meter.
- The VU-meter variables are declared,
ledPins[]
,pinCount
andlights
. - The TiltSwitch objects are declared,
player_1
andplayer_2
. - In
setup()
, the VU-meter and tilt switches are configured and initialized. - The random number generator is initialized.
- In
loop()
, all LEDs are turned off withlights.clear()
. - The red LED is turned on. This means that both players should hold their swords upright.
- The program pauses for a random period of time, somewhere between 3000 and 6000 milliseconds.
- The red LED is turned off.
- The green LED is turned on. This means that it is time for the players to swing their swords.
- The program jumps to the function
getWinner()
. - In
getWinner()
, a while loop loops until one of the sword is swung. If player 1 tilts her tilt switch, the function returns a 1. If player 2 is first, the function returns a 2. - The program jumps back to
loop()
where the number returned bygetWinner()
is stored in the variablewinner
. - If
winner
is equal to 1, the first yellow LED is turned on. - If
winner
is equal to 2, the second yellow LED is turned on. - The program pauses for 5 seconds.
loop()
continues to loop.
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 hit first but your friend wins, try swapping your tilt switch swords.
- If nothing happens when you are swinging the sword, make sure to hold the tilt switch upright on red and wait for the green light, and see the reference for debugging tilt switches.
- If the VU-meter does not work correctly, see reference for debugging VU-meter.
Learn by doing
- Work on the looks: construct a couple of swords to house the tilt switches inside.
- Change the winning conditions: make it so that the person who is first to swing the sword up and down five times is the winner.