Sezme: A Memory Game for the Arduino

Sezme: A Memory Game for the Arduino © Apache-2.0

Pitch your memory ability against the Arduino.

  • 1,201 views
  • 0 comments
  • 8 respects

Components and supplies

About this project

I have just started using the Arduino and I thought I would try my hand at a little project of my own.

Sezme is a game for the Arduino Uno that tests your memory. It is easy to build as I have only used LEDs, resistors, switches and a Piezo.

In the game, the Arduino generates a series of random LED flashes. The player must then press the buttons and repeat the sequence of flashes in the correct order. If the player guesses incorrectly a buzzer sounds, the correct sequence is shown and the game starts again. If the player guesses correctly, a series of tones are played and the difficulty level goes up. There are 10 levels of difficulty. I can manage level 7. How far can you get?

Code

Sezme Source CodeArduino
Create a sketch called sezme in the Arduino IDE and copy and paste the code below into it.
/*
 * Sezme.
 * 
 * A game for the Arduino Uno that tests your memory.
 * 
 * Sezme is a memory game in which the Arduino generates a series
 * of random LED flashes. The player must then press the buttons and
 * repeat the sequence of flashes in the correct order. If the player
 * guesses incorrectly a buzzer sounds, the correct sequence is shown 
 * and the game starts again. If the player guesses correctly, the 
 * difficulty level goes up.
 * 
 * The buttons are connected to digital pins 2 through 5 via
 * a 10K Ohm pull-down resistor.
 * 
 * The LEDs (yellow, blue, green & red) positive legs are connected to
 * pins 9 through 13. The negative legs are connected to GND via a 220 Ohm resistor.
 * 
 * Button connected to pin 2 controls the yellow LED
 * Button connected to pin 3 controls the blue LED
 * Button connected to pin 4 controls the green LED
 * Button connected to pin 5 controls the red LED
 * 
 * 
 * Author: Mario Gianota December 2019
 */
const int LED_YELLOW  = 10; // Yellow LED pin
const int LED_BLUE    = 11; // Blue LED pin
const int LED_GREEN   = 12; // Green LED pin
const int LED_RED     = 13; // Red LED pin

const int BUTTON_YELLOW = 2;  // Yellow button input pin
const int BUTTON_BLUE   = 3;  // Blue button input pin
const int BUTTON_GREEN  = 4;  // Green button input pin
const int BUTTON_RED    = 5;  // Red button input pin

const int PIEZO         = 6;  // The pin the piezo is connected to

const int STATE_START_GAME        = 0;  // Initial state
const int STATE_PICK_RND_SEQUENCE = 1;  // Pick a random sequence of LEDs
const int STATE_SHOW_RND_SEQUENCE = 2;  // Show the randomly selected sequence of LED flashes
const int STATE_READ_PLAYER_GUESS = 3;  // Read the player's guess
const int STATE_VERIFY_GUESS      = 4;  // Check the guess against the random sequence
const int STATE_GUESS_CORRECT     = 5;  // Player guessed correctly
const int STATE_GUESS_INCORRECT   = 6;  // Player guessed incorrectly

const int MAX_DIFFICULTY_LEVEL    = 9;  // Maximum difficulty level (LED flash sequence length)

// Array used to store the generated random sequence
int randomSequence[MAX_DIFFICULTY_LEVEL];

// Array used to store the player's guess
int playerGuess[MAX_DIFFICULTY_LEVEL];
// A counter to record the number of button presses made by the player
int numButtonPresses;

// The current state the game is in
int currentState;
int nextState;

// The difficulty level (1..MAX_DIFFICULTY_LEVEL)
// (Do not set to zero!)
int difficultyLevel;

void setup() {
  // Initialise input buttons
  for(int i=BUTTON_YELLOW; i<=BUTTON_RED; i++) {
    pinMode(i, INPUT);
  }
  // Initialise LEDs
  for(int i=LED_YELLOW; i<=LED_RED; i++) {
    pinMode(i, OUTPUT);
  }
  // Set initial difficulty level to 1 random flash && put game in start state
  difficultyLevel = 1;
  currentState = STATE_START_GAME;
  nextState = currentState;
  numButtonPresses = 0;
}

void loop() {

  // Run the state machine controlling the game's states
  switch( currentState ) {
    case STATE_START_GAME: 
      delay(5000); // Give player time to get ready
      nextState = STATE_PICK_RND_SEQUENCE; 
      break;
    case STATE_PICK_RND_SEQUENCE:
      generateRndSequence();
      nextState = STATE_SHOW_RND_SEQUENCE;
      break; 
    case STATE_SHOW_RND_SEQUENCE:
      showRndSequence();  // Display the rnd sequence to the player
      nextState = STATE_READ_PLAYER_GUESS;
      break;
    case STATE_READ_PLAYER_GUESS:
      readPlayerGuess();  // Poll buttons and record each button press
      // If all inputs have been made then verify the guess
      if( numButtonPresses >= difficultyLevel ) {
        numButtonPresses = 0;
        nextState = STATE_VERIFY_GUESS;
      }
      break;
    case STATE_VERIFY_GUESS:
      // Check player's guess against the generated random sequence
      if( verifyGuess() ) {
        nextState = STATE_GUESS_CORRECT;
      } else {
        nextState = STATE_GUESS_INCORRECT;
      }
      break;
    case STATE_GUESS_CORRECT:
      // Player was right. Increase difficulty level and goto start game
      soundCorrectGuess();
      difficultyLevel++;
      if( difficultyLevel > MAX_DIFFICULTY_LEVEL )
        difficultyLevel = MAX_DIFFICULTY_LEVEL;
      nextState = STATE_START_GAME;
      break;
    case STATE_GUESS_INCORRECT:
      // Player was wrong. Sound buzzer, show correct sequence, set difficulty level to 1 and re-start game
      soundBuzzer();
      showRndSequence();
      difficultyLevel = 1;
      nextState = STATE_START_GAME;
      break;
  }
  currentState = nextState;
}


// Generate a random sequence of LED pin numbers
void generateRndSequence() {
  for(int i=0; i<difficultyLevel; i++) {
    randomSequence[i] = rndLEDPin();
  }
}

// Show random sequence
void showRndSequence() {
  for(int i=0; i<difficultyLevel; i++) {
    flashLED(randomSequence[i], true);  
  }
}

// Read a button press representing a guess from player
void readPlayerGuess() {
  if( digitalRead(BUTTON_YELLOW) == HIGH ) {
    playerGuess[numButtonPresses] = LED_YELLOW;
    numButtonPresses++;
    flashLED(LED_YELLOW, true);
    blockUntilRelease(BUTTON_YELLOW);
  } else if( digitalRead(BUTTON_BLUE) == HIGH ) {
    playerGuess[numButtonPresses] = LED_BLUE;
    numButtonPresses++;
    flashLED(LED_BLUE, true);
    blockUntilRelease(BUTTON_BLUE);
  }else if( digitalRead(BUTTON_GREEN) == HIGH ) {
    playerGuess[numButtonPresses] = LED_GREEN;
    numButtonPresses++;
    flashLED(LED_GREEN, true);
    blockUntilRelease(BUTTON_GREEN);
  }else if( digitalRead(BUTTON_RED) == HIGH ) {
    playerGuess[numButtonPresses] = LED_RED;
    numButtonPresses++;
    flashLED(LED_RED, true);
    blockUntilRelease(BUTTON_RED);

  }
}

void blockUntilRelease(int buttonNumber) {
  while( digitalRead(buttonNumber) == HIGH )
    ;
}
// Compare the guess with the random sequence and return true if identical
bool verifyGuess() {
  bool identical = true;
  for(int i=0; i<difficultyLevel; i++) {
    if( playerGuess[i] != randomSequence[i] ) {
      identical = false;
      break;
    }
  }
  return identical;
}

// Sound the buzzer for incorrect guess
void soundBuzzer() {
  tone(PIEZO, 100, 2000);
  delay(2300);  
}

void soundCorrectGuess() {
  tone(PIEZO, 700, 100);
  delay(100);
  tone(PIEZO, 800, 100);
  delay(100);
  tone(PIEZO, 900, 100);
  delay(100);
  tone(PIEZO, 1000, 100);
  delay(100);
  tone(PIEZO, 1100, 100);
  delay(100);
  tone(PIEZO, 1200, 100);
  delay(100);
}
// Flash the LED on the given pin
void flashLED(int ledPinNum, bool playSound) {
  digitalWrite(ledPinNum, HIGH);
  if( playSound )
    playSoundForLED(ledPinNum);
  delay(1000);
  digitalWrite(ledPinNum, LOW);
  delay(500);
}

void playSoundForLED(int ledPinNum) {
  int pitch = 0;
  switch(ledPinNum) {
    case LED_YELLOW: pitch = 200; break;
    case LED_BLUE: pitch = 250; break;
    case LED_GREEN: pitch = 300; break;
    case LED_RED: pitch = 350; break;
  }
  tone(PIEZO, pitch, 800);
}
// Get a random LED pin number
int rndLEDPin() {
  return random(LED_YELLOW, LED_RED + 1);
}

Schematics

Sezme Breadboard Schematic
Sezme bb hxunaalnms

Comments

Similar projects you might like

Make an Arduino Memory Game

Project tutorial by Jeremie

  • 21,601 views
  • 39 comments
  • 46 respects

Memory game

Project showcase by Anton Nazarenko

  • 3,259 views
  • 2 comments
  • 9 respects

Play Dinosaur Game Using Arduino and Python3

Project tutorial by Jalal_Mansoori

  • 1,501 views
  • 1 comment
  • 7 respects

LCD Game

Project showcase by Team Iron_SalsaStudio

  • 14,139 views
  • 12 comments
  • 23 respects

Arduino Pocket Game Console + A-Maze - Maze Game

Project tutorial by Alojz Jakob

  • 11,060 views
  • 8 comments
  • 28 respects

Simon Style Memory Game with Agent Based Programming

Project tutorial by Embrio

  • 1,893 views
  • 4 comments
  • 8 respects
Add projectSign up / Login