Fencing

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

  1. Attach the shield onto the top of the Arduino board.
  2. Connect four LEDs across breadboard gap in the following order – yellow, red, green, yellow.
  3. Connect a 220 ohm resistor to digital pin 2. Connect the resistor to the long leg of first LED.
  4. Connect each of the digital pins 3 through 5 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 Tinkerkit tilt switches to D9 and D10.
  7. Connect the Arduino assembly to the computer, upload example “Fencing” and try the game.

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 and lights.
  • The TiltSwitch objects are declared, player_1 and player_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 with lights.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 by getWinner() is stored in the variable winner.
  • 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.