Simon Game Challenge

! Challenge

The last two projects are intended to put into practice what we have learned so far.

!Meet the Genius

Genius was a very popular game on the 1980's in Brazil, using very simple mechanics. The goal of the game is to follow and repeat, without mistaking, the light sequence for as long as possible...

To simplify the game, we decided to use sounds.

!Do it Yourself!

Search about Genius and see how it works. A good starting point is the Wikipedia link.

Think how you could make this project by exploring the resources of your kit, and then create a program for that!

!Tips for the project:

For this project you will need:

  • Think about how to check if the pressed button corresponds to the correct sound
  • Indicate whether the player got it right or not
  • Create a different sequence each time the game starts
  • Create a reset button
  • Make the sequence so that, if the player gets it right, it is composed of the previous sounds plus a new one
  • You will need to do a debounce to avoid multiple clicks when pressing the button

Next you can see our code for this project, but we recommend that you do it yourself, because this way you will truly learn! If it gets too difficult, use our code to clarify your doubts or find out how to solve more specific problems.

Our Code

#define MAX 75

// Pin connected to pushbutton
int buttonPin = A0;

// Pin connected to buzzer
int buzzer = 5;

// Variable to check the button state
int buttonState;

// Variable to make it easier to identify which button was pressed
int pressed_button;

// Defining the pin output to be clearer (yellow LED)
int led1 = A1;

// Defining the output pin to be clearer (green LED)
int led2 = A2;

int count = 0;
int compareSound = 0;
int playSound;
int wait = 1;
int timer = 0;
int result;
int variable;
int sequence[MAX];
int typed_sequence[MAX];

int readButton(int buttonState)
{
  int last, now;
  last = buttonState;
  now = analogRead(buttonPin);

  if (now != last) {
    delay(25);
    last = now;
  }
  if (now == last) {
    if (now < 69) {
      return 1;
    } else if (now >= 69 && now <= 230) {
      return 2;
    } else if (now >= 231 && now <= 407) {
      return 3;
    } else if (now >= 408 && now <= 630) {
      return 4;
    } else if (now >= 631 && now <= 896) {
      return 5;
    }
  }
}

void error_display()
{
  digitalWrite(led1, HIGH);
  //If the button is pressed, the Buzzer plays a sound at a frequency relative to the error
  buzzer_sound(95, 700, 1);
  digitalWrite(led1, LOW);
  delay(100);
}

void buzzer_sound(int frequency, int time_delay, int times)
{
  tone(buzzer, frequency);
  delay(time_delay);
  noTone(buzzer);
}

void blink_led(int exit, int time_delay, int times)
{
  for (int how_many = 0; how_many < times; how_many++) {
    digitalWrite(exit, HIGH);
    delay(time_delay);
    digitalWrite(exit, LOW);
    delay(time_delay);
  }
}

void alternace_blink_led(int time_delay, int times)
{
  for (int how_many_2 = 0; how_many_2 < times; how_many_2++) {
    digitalWrite(led1, HIGH);
    delay(time_delay);
    digitalWrite(led1, LOW);
    delay(time_delay);
    digitalWrite(led2, HIGH);
    delay(time_delay);
    digitalWrite(led2, LOW);
    delay(time_delay);
  }
}

void setup()
{
  // Defines the button pin as input
  pinMode(buttonPin, INPUT);
  // Defines the yellow LED pin as output
  pinMode(led1, OUTPUT);
  // Defines the green LED pin as output
  pinMode(led2, OUTPUT);
  // Defines the buzzer pin as output
  pinMode(buzzer, OUTPUT);
  randomSeed(analogRead(A7));
}

void loop()
{
  boolean finish = false;
  // Checks if the button state has changed
  int buttonState = analogRead(buttonPin);

  memset(typed_sequence, 0, MAX);
  variable = random(1, 5);
  sequence[count] = variable;
  compareSound = 0;

  for (int i = 0; i <= count; i++) {
    playSound = sequence[i];

    switch (playSound) {
      case 1 :
        buzzer_sound(261, 200, 1);
        break;

      case 2 :
        buzzer_sound(293, 200, 1);
        break;

      case 3 :
        buzzer_sound(329, 200, 1);
        break;

      case 4 :
        buzzer_sound(349, 200, 1);
        break;
    }
  }

  buttonState = 1023;

  while (buttonState >= 897) {
    buttonState = analogRead(buttonPin);
  }

  if (buttonState < 897) {
    while (compareSound <= count) {
      pressed_button = readButton(buttonState);

      if (pressed_button == 1) {
        buzzer_sound(261, 200, 1);
        typed_sequence[compareSound++] = 1;
      }

      if (pressed_button == 2) {
        buzzer_sound(293, 200, 1);
        typed_sequence[compareSound] = 2;
        compareSound++;
      }

      if (pressed_button == 3) {
        buzzer_sound(329, 200, 1);
        typed_sequence[compareSound] = 3;
        compareSound++;
      }

      if (pressed_button == 4) {
        buzzer_sound(349, 200, 1);
        typed_sequence[compareSound] = 4;
        compareSound++;
      }

      if (pressed_button == 5) {
        alternace_blink_led(100, 2);
        count = 0;
        compareSound++;
        finish = true;
      }
    }
  }

  boolean isCorrect = true;

  if (!finish) {
    for (int k = 0; k <= count; k++) {
      if (typed_sequence[k] != sequence[k]) {
        isCorrect = false;
        break;
      }
    }

    if (isCorrect) {
      blink_led(led2, 100, 2);
      count++;
    } else {
      error_display();
      count = 0;
    }
  }
  delay(100);
}