Simon Says

Test your memory with this game!
LEDs will blink in a pattern that you have to remember and repeat. If you get it right, the game gets more and more challenging.

Materials

  • 1 Arduino Uno board
  • 1 Education Shield
  • 1 breadboard
  • 1 TinkerKit button
  • 1 potentiometer
  • 1 piezo speaker
  • 5 LEDs
  • 5 220 ohm resistors
  • 1 TinkerKit wire
  • 7 black jumper wires
  • 8 colored jumper wires

 

Note: The potentiometer 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 separate 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 GND pin using black jumper wires.
  6. Connect the potentiometer to the on-shield breadboard. Connect the middle pin of the potentiometer to A0, left pin to GND and right pin to 5V.
  7. Connect the piezo speaker across the breadboard gap and connect one leg to digital pin 8 and the other to GND.
  8. Connect the TinkerKit button to D9.
  9. Connect the Arduino assembly to the computer, upload example SimonSays and try the game.

Code

Find the code in File>Examples>EducationShield>Block2-Sports>Projects>SimonSays

How it works

  • The EducationShield library is included.
  • The VU-meter variables are declared, ledPins[], pinCount and vuMeter.
  • The Knob, Button and Melody objects are declared, pot, button and piezo.
  • The game variables are declared to hold some game parameters, turns_begin, turns_max, game, turns[] and blinkTime.
  • In setup(), the VU-meter is configured and initialized.
  • The potentiometer is configured to have 5 levels.
  • The button is initialized.
  • The random number generator is initialized.
  • In loop(), the program jumps to the function newGame().
  • In newGame(), all LEDs are blinked.
  • All LEDs are turned off.
  • The program pauses for 500 milliseconds.
  • A for loop loops for as many times as the value of turns. The first time newGame() is run, turns is equal to 2.
  • In every loop of the for loop, a random number is generated between 0 and the value of pinCount, and stored in game[].
  • The program jumps back to loop().
  • The program jumps to the function simonSays().
  • In simonSays(), a for loop loops for as many times as the value of turns.
  • In every loop of the for loop, an LED is turned on, on the position of the generated number stored in game[].
  • The program pauses for as many milliseconds as the value of blinkTime.
  • The same LED is turned off.
  • The program pauses again.
  • The program jumps back to loop().
  • The program pauses for 1000 milliseconds.
  • The program jumps to the function getInputs().
  • In getInputs(), a for loop loops as many times as the value turns. Each time the for loop runs, the following happens:
    • The variable input is declared.
    • A while loop loops as long as the button is not released in time. This means that the button is checked all the time but in short pulses, 10 milliseconds. This is to not completely stop the program and prevent it from checking the potentiometer.
    • In the while loop, all LEDs are turned of.
    • The LED on the position of the value read from the potentiometer, is turned on,
    • When the button finally is released, the program leaves the while loop.
    • The read value from the potentiometer is stored in input.
    • If input is equal to the current game[] value (if it is the first time the for loop runs, this means if input is equal to game[0]), a scoring sound is played with the piezo.
    • The for loop continues and the next turn is checked.
  • When the for loop has looped through all the turns the program continues and pauses for 500 milliseconds.
  • The program jumps to the function levelUp().
  • In levelUp(), if turns is less than turns_max, turns is increased with 1, making the game a little bit more difficult for the next round.
  • A winning sound is played with the piezo.
  • The program jumps back to getInputs().
  • The program jumps back to loop().
  • loop() starts over and loops through the functions again.
  • In getInputs(), if input is not equal to the current game[] value, the program jumps to gameOver().
  • In gameOver(), turns is set to be equal to turns_begin again.
  • A game over sound is played with the piezo.
  • The program jumps back to getInputs().
  • The program leaves the getInput() function with the command return, and jumps back to loop().
  • loop() starts over and loops through the functions again.

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 button does not work, see reference for debugging buttons.
  • If the VU-meter does not work correctly, see reference for debugging the VU-meter.

Learn by doing

  • Make the game more challenging by making it go faster.
  • Make it a two player game. Record the first players patterns and let the second player repeat it.