Introduction: Simon Game

Picture of Simon Game

Simon is an electronic game of memory skill. The device creates a series of tones and lights and requires a user to repeat the series. If the user succeeds the series becomes progressively longer and more complex. Once the user fails, the game is over.

Let's get started and see how can we make our Simon game
This project was made in Fablab Dhahran by Salam AlSaif.

Step 1: Parts Required

You will need the following parts to make the electronic circuit:

1. Arduino UNO

2. 4 Pushbuttons switch

3. 4 LEDs (Blue, Red, Yellow & Green)

4. 9V Battery

5. Connector 9V Battery

6. [Optional] Switch

will need a laser cutter to make the external design of the game.

Step 2: Electronic Circuit

Picture of Electronic Circuit
  1. Connect all cathodes (short lead) of LEDs to the arduino GND.
  2. Connect the anode of Red LED to the arduino digital pin 9
  3. Repeat step 2 for LED ( green, blue, yellow) to the arduino digital pin ( 10, 11, 12) Respectively
  4. Mark each button with the with corresponding LED color
  5. Connect one side from each button to the arduino GND.
  6. Connect the other side of red button to the arduino digital pin 2.
  7. Repeat step 6 for button ( green, blue, yellow) to the arduino digital pin ( 3, 4, 5) Respectively
  8. [Optional] Cut the negative wire from the battery to the GND of the Arduino and connect a switch to it; so you can switch your game on or off.

Step 3: The Code

The CODE starts by declare the variable. I used variables with type "byte" because my numbers are in range (0-255) so I can decrease the code size.

#define max_level 20 //I set the max number of level to be 20

byte i;
byte x;
byte level;
byte button[4] = { 2, 3, 4, 5 } ; // Red, Green, Blue, Yellow  
byte led[4] = { 9, 10, 11, 12 } ; // Red, Green, Blue, Yellow
byte pattern[max_level] ; 

Setup function:

Arduino has internal pull-up resistors (resistors that connect to power internally).

I used them so I don't need to add external resistor on the buttons.

When the button is pressed, the signal is 0. And vice versa.

void setup() 
{
  pinMode( button[0], INPUT_PULLUP); // INPUT_PULLUP >> The button is 0 when is it pressed
  pinMode( button[1], INPUT_PULLUP);
  pinMode( button[2], INPUT_PULLUP);
  pinMode( button[3], INPUT_PULLUP);
  pinMode( led[0], OUTPUT);
  pinMode( led[1], OUTPUT);
  pinMode( led[2], OUTPUT);
  pinMode( led[3], OUTPUT);
}

Main function:

First it generate a random pattern. Then it start from the level 1 by showing the first color. Then it wait a user to enter the key. If he enter the right button, the game will continue to the next level. If not, the game will restart from the first level with new pattern. If use reach to the last level that you set at begin of the code, the game also will restart.

void loop() 
{


  create_pattern();
  for ( level = 1 ; level <= max_level ; level ++ ) 
  {
    Serial.print("Level:");
    Serial.println(level);
    show_pattern( level);
    if (valid (level))
      {
        Serial.println("Good");
        delay(2000);
      }
     else
     {
       Serial.println("You lose");
       lose();
       break ;
     }
    Serial.println();
  }
  if (level > max_level)
    {
      Serial.println("You Win");
    }

}

Functions:

1- get key

This function read the input pins and return the number of pressed button. It also turn on the corresponding LED and tone.

byte get_key () // This function check which button is pressed
{
  byte i = 0 ;

  do 
  {
    for (i = 0; i < 4 ; i++)
    {
      if ( digitalRead(button[i]) == 0)
      {
        Serial.print("Button :");
        Serial.println(i);
        digitalWrite(led[i], 1);
        tone(8, (400+i*400), 200);
           
        while (digitalRead(button[i])==0);
        delay(500);
        digitalWrite(led[i], 0);
        noTone(8);
        return (i); 
      }      
    }
  } while ( digitalRead(button[0]) && digitalRead(button[1]) && digitalRead(button[2]) && digitalRead(button[3]));
  get_key ();
}

2- Create pattern

This function generate a random pattern. The size of the pattern is equal to number of levels.

void create_pattern () //This function create a random pattern
{
  byte i ;
  for ( i = 0 ; i < max_level ; i++ )
  {
    pattern[i] = random(0, 4);
  }
}

3- Show pattern

This function show every level pattern

void show_pattern(byte level) //This function show the pattern
{
  byte i ;
  for ( i = 0 ; i < level ; i++ )
  {
    x = pattern[i] ;
    Serial.print(x);
    Serial.print(" ");
    digitalWrite(led[x], 1);
    tone(8, (400+x*400), 200);
    delay(300);
    digitalWrite(led[x], 0);
    noTone(8);
    delay(1000*pow(1.1,-level)); // This function in time delay makes the delay shorter each level
  }
  Serial.println();
}
4- Check valid

After the pattern has shown, the player have to press the right button. This function compare between the pressed button and the pattern.

byte valid (byte level) // This function check if your input is same as the pattern
{
  byte i ;
  for ( i = 0 ; i < level ; i++ )
  {
    if (get_key() != pattern[i] )
      return (0);
  }
  return (1);
}
5- Lose

If you make a mistake, the game will restart and turn the LEDs to indicate that. Then it starts from the first level.

void lose() // This function reset the game 
{
  int i ;
  
  for ( i = 0 ; i < 4 ; i++ )
  {
    digitalWrite(led[i], 1);
    tone(8, (400+i*400), 200);
    delay(300);
    digitalWrite(led[i], 0);
    noTone(8);
  }
  delay(1000);
  for ( i = 3 ; 0 <= i ; i-- )
  {
    Serial.println(i);
    digitalWrite(led[i], 1);
    tone(8, (400+i*400), 200);
    delay(300);
    digitalWrite(led[i], 0);
    noTone(8);
  }
  delay(2000);
}

Step 4: Box Design

Picture of  Box Design

I used a black 6 mm acrylic for my box with these dimensions 12cm x 12cm x 5cm.

note:

You can use www.makercase.com to design and laser cut your box.

There is a place to mount a power switch ( make sure that you adjust it to your switch size )

For the other part I used a 3 mm acrylic. First let's start by the square. We need 4 color of the acrylic ( Red, Green, Yellow, Blue ). Cut each color one square.

Each square has 2 holes. The small one is for the 5 mm LED. The second one is for the button ( make sure that you adjust it to your button size )

Secondly, the supports which helped me to fix the squares on their place. There are one support I put under the squares so it become stronger.

I used a super glue to bond all pieces. And glue gun for the electronic parts.

Comments

BrownDogGadgets (author)2017-09-07

Ha! Thats a load of fun! I'll have to give it a try.

bgel (author)2017-08-18

Can I use UCI SPEAKERS

Swansong (author)2017-08-17

I love these! I used to play them a lot as a kid :)

About This Instructable

1,077views

18favorites

License:

More by FABLAB Dhahran: Ladder Shelf Ludo Candle Holder
Add instructable to: