Introduction: Simon Game With Arduino

Reference Sources: Simon & Whack-a-Mole Game Using Arduino

Have you ever played an electronic game called Simon? The game involved your memory skill and provide a series of tones and colours that need the player to repeat the sequence by pressing the correct bottom orders. In this instructable, I will provide a series of instruction to create your own unique Simon game by Arduino and some recycled materials!

I've modified the game from the original game by adding the sound effects in the game and alter the change of the speed. (The original instruction is provided above)

Step 1: Parts

Electronic materials

  • Arduino Leonardo or equivalent *1
  • USB Cable *1
  • Bread Board*1
  • LED lights *4 (Different colours such as blue, yellow, red, and green)
  • Bottoms *4 (No specific)

  • 220-ohm resistors *8

  • Wires

  • Alligator clips with wires *8

  • Speaker *1 (with wires for Arduino)

  • Power source *1 (Power back or other)

Materials and tools for the container designed

  • Poster paper *3
  • Tape
  • Scissor *1
  • Shoebox *1
  • Cutting mat *1
  • Ruler *1
  • Pencil *1
  • Utility knife *1

Most of the materials for inner structure can buy from:

Step 2: Mount and Connect Arduino

The correct wires connection are the most important parts for this game to function properly. On this step, I'll introduce the function of each material and show the correct connections of the wires for each supply.

The different types of Arduino including Arduino Uno, Leonardo, or other equivalents will have slightly different wires for different functions. In order to make sure you got the correct order and connection, remember to follow the instruction below or do some Google search on the internet.

Make sure do not install the incorrect connection of the wires especially the positive and negative electrodes. (May causing danger)

Plus, I recommend to use the same colour of wires for the same bottom and LED lights. This can reduce the chance of connecting the wrong wire.


LED Lights

A LED light has two pins, one is for positive pole and one is for negative. The positive pole should directly connect to the Digital Pin on the Arduino board. The negative pole should connect to the negative electrode with a 220-ohm resistor in between.

Red LED --> D2

Blue LED --> D4

Yellow LED --> D6

Green LED --> D8

The negative pole should connect to the GND which can first connect to the negative part on the breadboard and use another wire that links all negative wires to GND on Arduino.

Bottoms

The bottom has a similar connection with the LED lights. The difference is that the wires of the bottom do not have the negative or positive pole. Therefore, you can randomly pick the wire to connect. The alligator clips are used in this section, it helps to strengthen the connections between the bottom's wire and the wires from positive and negative electrodes.

Red Bottom --> D3

Blue LED --> D5

Yellow LED --> D7

Green LED --> D9

The 200-ohm is also required between the negative connections.

Speaker

The two wires of the speaker should be connected to the D11 pin and GND.

Step 3: Writing Code

After the circuit is connected, you can start writing code.

The code of Arduino can be copied by here.

Before you upload your work to your Arduino, make sure to verify the code and test it before the final work is done. The pin can be modified by your own but remember to change the code on the Arduino as well. If you have changed the pin, then remember to test the board again.

<p>const int redLED = 2;<br>const int blueLED = 3;
const int yellowLED = 4;
const int greenLED = 5;</p><p>const int redButton = 6;
const int blueButton = 7;
const int yellowButton = 8;
const int greenButton = 9;</p><p>int waitTime = 3000; //loop cycle for button press before timeout</p><p>//setup an arrary
int sequence[256];</p><p>// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(redLED, OUTPUT);
  pinMode(blueLED, OUTPUT);
  pinMode(yellowLED, OUTPUT);
  pinMode(greenLED, OUTPUT);</p><p>  //use "PULLUP" to set open/unpressed to HIGH
  pinMode(redButton, INPUT_PULLUP);
  pinMode(blueButton, INPUT_PULLUP);
  pinMode(yellowButton, INPUT_PULLUP);
  pinMode(greenButton, INPUT_PULLUP);</p><p>  Serial.begin(9600);      // open the serial port at 9600 bps:
  randomSeed(analogRead(0));
}</p><p>// the loop function runs over and over again forever
void loop()
{</p><p>  int seqNo = 0;
  int newColor;
  //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  boolean correctSeq = true;
  //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</p><p>  Serial.println("===new sequence===");</p><p>  while ((seqNo <= 10) and (correctSeq)) {
    //pick a new color
    newColor = pickNewColor();</p><p>    //assign newcolor to an array:
    sequence[seqNo] = newColor;</p><p>    //debug manually set the sequence
    /*  sequence[0] = 2;
      sequence[1] = 3;
      sequence[2] = 4;
      sequence[3] = 5;
      seqNo = 3;
    */
    //show me the sequence being generated
    Serial.println("sequence has:");
    for (int i = 0; i <= seqNo; i++) {
      Serial.print(sequence[i]);  //debug
      blink(sequence[i], 500);
    }
    Serial.println(); //debug</p><p>    //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    //Serial.print("Check for button: ");</p><p>    //for (int i = 0; i <= seqNo; i++) {
    int i = 0;</p><p>    while ((i <= seqNo) and (correctSeq)) {
      if (isPressed(sequence[i])) {
        //Serial.println(sequence[i]);
        //Serial.println("You whacked the mole!");
        //ledOff(newColor);
        //delay(500); //pause briefly before showing a new mole
        //waitTime = 0.9*waitTime;
        i++;
      } else {
        Serial.print("** wrong button. game over**");
        correctSeq = false;
      }</p><p>    }</p><p>    Serial.println();</p><p>    //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</p><p>    seqNo++;
    delay(500);  //delay between next sequence
    // 改 delay時間減少 增加遊戲難度
  }</p><p>  gameover();
}</p><p>//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
boolean isPressed(int newColor) {
  int i = 0;
  int chkButton;</p><p>  boolean isCorrect = false;
  boolean buttonPressed = false;
  boolean buttonReleased = false;</p><p>  //start the counter and wait for botton to be pressed..or timeout
  Serial.print("Wait for button ");
  Serial.println(newColor);</p><p>  //wait until button is pressed AND released
  while ((i < waitTime) and (!buttonReleased)) {</p><p>    //TODO check down to up
    chkButton = isButtonPressed(); //0 if nothing is pressed</p><p>    if (!buttonPressed) { //check for button push
      if (newColor == chkButton) { //correct button is pressed
        isCorrect = true;
        buttonPressed = true;
        Serial.print(chkButton);
        Serial.print(" pressed.");</p><p>      } else if (chkButton > 0) { //incorrect button is pressed
        isCorrect = false;
        buttonPressed = true;
        Serial.print(chkButton);
        Serial.print(" pressed.  ");
      }
    } else { //button pressed so check for release
      //Serial.print("buttonpressed is true");
      if (chkButton == 0) {
        buttonReleased = true;
        Serial.print(chkButton);
        Serial.print(" released.");
      }
    }</p><p>    i++;
    //Serial.println(i);
    //Serial.print("  @isPressed: ");
    //Serial.println(chkButton); //Comment out later. This slows down the loop
    //Serial.println("."); //Comment out later. This slows down the loop</p><p>    delay(1); //make each loop about 1ms
  }</p><p>  if ((buttonPressed) and (!buttonReleased)) {
    isCorrect = false;  //is there a better place to reset this
    Serial.print(chkButton);
    Serial.println("  button pressed but release NOT detected");
  } else if ((buttonPressed) and (buttonReleased)) {
    Serial.print(chkButton);
    Serial.println("  button pressed and release detected.");
  } else {
    Serial.println("time out");
  }</p><p>  return isCorrect;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</p><p>boolean isWhacked(int newColor) {
  int i = 0;
  int chkButton;
  boolean whacked = false;
  boolean buttonPressed = false;</p><p>  //start the counter and wait for botton to be pressed..or timeout
  Serial.print("Wait for button....");
  while ((i < waitTime) and (!buttonPressed)) {</p><p>    chkButton = isButtonPressed2(); //0 if nothing is pressed
    if (newColor == chkButton) { //correct button is pressed
      whacked = true;
      buttonPressed = true;
    } else if (chkButton > 0) { //incorrect button is pressed
      whacked = false;
      buttonPressed = true;
    }
    i++;
    //Serial.println("."); //Comment out later. This slows down the loop
    delay(1); //make each loop about 1ms
  }
  if (buttonPressed) {
    Serial.println("button detected.");
  } else {
    Serial.println("time out");
  }</p><p>  return whacked;
}</p><p>//function that monitor all buttons and returns a integer
// 0 = nothing pressed
// 2-5 = button prssed
// variation #2 -
// do not turn off led if button is NOT pressed
int isButtonPressed2() {</p><p>  int buttonPressed = 0;</p><p>  //2 = red, 3 = blue, 4 = yellow, 5=green
  //Set to LOW when a button is pressed</p><p>  if (digitalRead(redButton) == LOW) {
    ledOn(redLED);
    buttonPressed = redLED;
  } /*else {
      ledOff(redLED);;
    }*/</p><p>  if (digitalRead(blueButton) == LOW) {
    ledOn(blueLED);
    buttonPressed = blueLED;
  } /*else {
      ledOff(blueLED);
    } */</p><p>  if (digitalRead(yellowButton) == LOW) {
    ledOn(yellowLED);
    buttonPressed = yellowLED;
  } /* else {
      ledOff(yellowLED);
    } */</p><p>  if (digitalRead(greenButton) == LOW) {
    ledOn(greenLED);
    buttonPressed = greenLED;
  } /*else {
      ledOff(greenLED);
    } */</p><p>  return buttonPressed;
}</p><p>void gameover() {</p><p>  for (int i = 0; i <= 2; i++) {</p><p>    delay(200);
    tone(11, 988, 300); //buzzer or speaker frequency
    delay( 1000 ); // waits a few milliseconds
    noTone(11);
    tone(11, 988, 300); //buzzer or speaker frequency
    delay( 1000 ); // waits a few milliseconds
    noTone(11);
    //改 加入音效
    ledOn(redLED);
    ledOn(blueLED);
    ledOn(yellowLED);
    ledOn(greenLED);
    delay(200);
    ledOff(redLED);
    ledOff(blueLED);
    ledOff(yellowLED);
    ledOff(greenLED);</p><p>  }
  delay(2000);
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</p><p>//function that monitor all buttons and returns a integer
// 0 = nothing pressed
// 2-5 = button prssed
int isButtonPressed() {</p><p>  int buttonPressed = 0;</p><p>  //2 = red, 3 = blue, 4 = yellow, 5=green
  //Set to LOW when a button is pressed</p><p>  if (digitalRead(redButton) == LOW) {
    ledOn(redLED);
    buttonPressed = redLED;
  } else {
    ledOff(redLED);;
  }</p><p>  if (digitalRead(blueButton) == LOW) {
    ledOn(blueLED);
    buttonPressed = blueLED;
  } else {
    ledOff(blueLED);
  }</p><p>  if (digitalRead(yellowButton) == LOW) {
    ledOn(yellowLED);
    buttonPressed = yellowLED;
  } else {
    ledOff(yellowLED);
  }</p><p>  if (digitalRead(greenButton) == LOW) {
    ledOn(greenLED);
    buttonPressed = greenLED;
  } else {
    ledOff(greenLED);
  }</p><p>  return buttonPressed;
}</p><p>//function to randomly pick a new color
int pickNewColor () {</p><p>  int randomColor;
  //random(min, max)
  //Parameters
  //min - lower bound of the random value, inclusive (optional)
  //max - upper bound of the random value, exclusive</p><p>  //generate random # from 2 to 5 that matches led Pins
  //2 = red, 3 = blue, 4 = yellow, 5=green
  tone(11, 524, 200);
  delay(250);
  noTone(11);
  tone(11, 660, 200);
  delay(250);
  noTone(11);
  tone(11, 784, 200);
  delay(250);
  noTone(11);
  delay(500);
  //改 加入音效
  randomColor = random(2, 6);
  return randomColor;</p><p>}</p><p>void boomerang(int speed) {
  chaseL2R(speed);
  chaseR2L(speed);
}</p><p>void chaseL2R(int speed) {
  blink(redLED, speed);
  blink(blueLED, speed);
  blink(yellowLED, speed);
  blink(greenLED, speed);
}</p><p>void chaseR2L(int speed) {
  blink(greenLED, speed);
  blink(yellowLED, speed);
  blink(blueLED, speed);
  blink(redLED, speed);
}</p><p>void blink(int color, int blinkTime) {
  ledOn(color);
  delay(blinkTime);
  ledOff(color);
  delay(blinkTime);
}</p><p>void ledOffAll() {</p><p>  digitalWrite(redLED, LOW);
  digitalWrite(blueLED, LOW);
  digitalWrite(yellowLED, LOW);
  digitalWrite(greenLED, LOW);
}</p><p>void ledOn(int colorON) {</p><p>  if (colorON == redLED) {
    digitalWrite(redLED, HIGH);
  }
  else if (colorON == blueLED) {
    digitalWrite(blueLED, HIGH);
  }
  else if (colorON == yellowLED) {
    digitalWrite(yellowLED, HIGH);
  }
  else if (colorON == greenLED) {
    digitalWrite(greenLED, HIGH);
  }</p><p>}</p><p>void ledOff(int colorOFF) {</p><p>  if (colorOFF == redLED) {
    digitalWrite(redLED, LOW);
  }
  else if (colorOFF == blueLED) {
    digitalWrite(blueLED, LOW);
  }
  else if (colorOFF == yellowLED) {
    digitalWrite(yellowLED, LOW);
  }
  else if (colorOFF == greenLED) {
    digitalWrite(greenLED, LOW);
  }</p><p>}</p>

Step 4: Design Your Container!

After finished connecting the wires in Arduino, in this step, we're going to design a unique and creative case for your game design. The materials we need include a shoebox, some pieces of poster paper (big enough to cover up the whole shoebox), some coloured markers, scissor, ruler, pencil, or other requirements. The steps are shown below in details:

  1. Measure the dimension of the entire shoebox. (each side)
  2. Before you start to design your work, make sure to measure the space that the bottoms and LED are going to come out.
  3. Use the scissor to cut off the poster paper in order to match the size of the shoebox.
  4. Each side of the shoebox should be covered by the poster paper.
  5. The case is done roughly. Use your creativity to come up with a design that is unique and awesome.
  6. Use markers, pencil, and other tools to help you create your design.
  7. After the outer layer of the box is done, carefully put your breadboard and Arduino into the box. Use tape or other tools to stick the bottom and LED lights on the top of the box.
  8. Close it up to make sure nothing (wires) is uncovered.
  9. Your design of the case is done!

Also, you can also add some additional decoration to your design in order to make it more interesting.

The case should be bigger than the Arduino and breadboard in order to cover up all of the wires to make the machine look professional and completed.

Step 5: Play Your Game!

After you finish all of the steps above, connect the power bank and Arduino with the USB cable. Turn on the power and enjoy the game that you made yourself! Share your own work below! Remember to connect the bottoms and LED light bulb properly in order to avoid bad connections.

If you have any problems, welcome to comment below.