Introduction: Total Recall- Arduino Simon Says on Steroids!

About: I'm a jack of all trades and a master of none. I like to tweak, mod and improvise whenever possible!
Total Recall is a Simon based game for the Arduino. Your basic Arduino Simon Game consists of 4 buttons, 4 LEDs, an Arduino, some code and maybe a speaker and sound effects. Total Recall takes things a step further by adding an LCD display and offering  1 and 2 player game modes. 

The 1 Player game is your basic Simon Game. I added 4 levels of difficulty to it by giving you options for how many rounds you want to play. Your then given a sequence of flashing lights to mimic. Each round adding an extra flash to the sequence. If you can complete the number of turns you win. 

The 2 Player game puts you head to head against a friend. The game starts with player one entering the first button into the sequence. The Sequence is then played back for player 2 to see. Then player 2 gets to mimic the sequence. If he gets it correct he gets the opportunity to add a button to the sequence. Then Player 1 gets the play back and tries to mimic the sequence. The game goes back and forth like this with the sequence getting increasingly longer till one player gets it wrong or you reach 99 turns in which case you've hit the max length of the sequence and the game is a draw. 

Materials Needed:
  • Arduino (I used a Nove)
  • Bread Board
  • Jumper Wires/Ribbon cables
  • Red, Yellow, Green and Blue LEDs
  • 4x 100 Ohm Resistors
  • 4x Push Buttons
  • Small Speaker
  • Hitachi HD44780 based 16x2 LCD
  • 5K Potentiometer

Step 1: Background

After posting Arduino Simon Says i started getting ideas for ways to improve the game. Whenever possible i like to simplify my code to reduce the amount of memory it uses. One of the things that really eats up memory in an Arduino sketch is using the Serial interface. Since the eventual goal is to get this game onto its own PCB and make it an actual portable game linking it to a computer to track your progress wasn't going to be an option. Removing the Serial commands from the game dropped 2004 bytes from the compiled code. So to help me towards my goal of portability i decided to add an LCD display to the game. 

Then i started thinking how i could make the game play even better. And what better way to make a game more fun then to play it with a friend. So i would need to come up with a menu system allowing you to choose between the 2 game types. I also wanted to make the original Simon game more interesting as well. So i decided it would be good to give people the option of how many turns they could shoot for. Nothing like giving people a goal to shoot for to make things more interesting. 

Now a checklist of ideas was starting to come together.
  1. Add LCD display
  2. Create difficulty options for single player game
  3. create 2 player game
All these ideas raised many questions. 

Step 2: Adding the LCD

Before i did anything i wanted to clean up the mess of wires a bit on the project. So i created three ribbon cables about 12 inch long from an old 40 wire IDE cable. I split the cable into 3 six wire strips and soldered header pins to each end. On one end of each i clipped the headers down to individual pins and split the wires about 2 or 3 inches up. The joined ends sit neatly into the Arduino while the split ends give me the freedom to reach any components i need. When all connected only Digital I/O pins 0 and 1 are left with nothing connected. I've been meaning to this this for quite some time. 

Now i was ready to figure out where to connect my LCD. Considering most of my Digital I/O pins where taken i had to figure out where i was going to connect the LCD. I wanted to keep as many of the LCD wires as close together as possible. This makes it a bit easier to troubleshoot any problem. I also didn't want to move the LEDs and Buttons around. This would just add to the headach of updating the code. So i decided to place the LCD on the Analog In pins. The Analog Pins can be used as Digital I/O pins. The are designated as follows:
  • Analog 0 = Digital 14
  • Analog 1 = Digital 15
  • Analog 2 = Digital 16 
  • Analog 3 = Digital 17
  • Analog 4 = Digital 18
  • Analog 5 = Digital 19
I couldn't put all the LCD pins across the Analog pot cause i still need Analog pin 0 for randomization of the single player game. So the LCD is connected as follows:
  • RS pin to D13
  • E pin to D12
  • D4 pin to D16
  • D5 pin to D17
  • D6 pin to D18
  • D7 pin to D19
  • V0 tied to a pot to control brightness
  • Grd and R/W tied to ground
  • Vcc to +5V
You can refer back to the Arduino Simon Says Instructable concerning the setup of the rest of the hardware. The only change made was moving the speaker over to pin 6. I hope the Fritzing image helps to clear up any confusion there might be in the wiring. I went ahead and attached the Fritzing file if you need a better look at the diagram. You can also refer back to my Custom Large Font For 16x2 LCDs for more information about setting up a Hitachi HD44780 based 16x2 LCD on the Arduino. 


Step 3: Menu Coding

I spent a good 6 to 8 hours just trying to work out a menu system. I wanted the opening screen to show the name of the game. It then needed to tell you to select a game and show you the game options. When the 1 Player Game is selected it wil then go to another menu where you can choose the difficulty level.

Many ideas where tried and failed. Ultimately this is what i came up with. below is just a snippet of the code showing you how the menu works. 

void Menu() //menu display
{
  lcd.clear();
  lcd.print(" Sellect number");
  lcd.setCursor(3,1);
  lcd.print("of players");
  delay(1500); 
}


void loop()
{ //menu system for selecting 1 or 2 player game
  if (game == 0) //no game selected
  {
    lcd.clear();

    for(int y=0; y<4; y++)
    {
      lcd.setCursor(0,0);
      lcd.print("1 Player: RED");
      lcd.setCursor(0,1);
      lcd.print("2 Player: YELLOW");

      buttonstate = digitalRead(button[y]);

      if (buttonstate == LOW && button[y] == 2)
      {
        button1(); //calls for the RED button's LED to light and tone to play
        game = 1; //1 player game
      }

      if (buttonstate == LOW && button[y] == 3)
      {
        button2(); //calls for the Yellow button's LED to light and tone to play
        game = 2; //2 player game
      }
    }
  }

  if (game == 1 && turnlimit > 1) //as long as 1 turn has been taken in the 1 player game
  {
    play1(); //calls to the 1 player game
  }

  if (game ==1 && turnlimit < 1)  //no turns have been taken in the 1 player game
  {
    play1menu(); //calls to the menu to select number of turns for 1 player game
  }

  if (game == 2)
  {
    play2(); //calls to 2 player game
  }  
}


void play1menu() //menu for selecting 1 player difficulty
{
  lcd.clear();
  lcd.print("Red=10 Yellow=20");
  lcd.setCursor(0,1);
  lcd.print("Green=30 Blue=40");
  delay(100);

  for(int y=0; y<4; y++)
  {
    buttonstate = digitalRead(button[y]);
    if (buttonstate == LOW && button[y] == 2)
    {
      button1();  ////calls for the RED button's LED to light and tone to play
      turnlimit = 10; //sets number of turns
    }

    if (buttonstate == LOW && button[y] == 3)
    {
      button2();
      turnlimit = 20;
    }

    if (buttonstate == LOW && button[y] == 4)
    {
      button3();
      turnlimit = 30;
    }

    if (buttonstate == LOW && button[y] == 5)
    {
      button4();
      turnlimit = 40;
    }
  }
}

The void Menu() is the start of the menu system. The game's Name display happens in the void setup(). When the setup is complete it calls directly to void Menu() then starts in on the void loop(). In the void loop() i used several variables to keep track of the state of the code. Like which game is being played. So that everything else can be bypassed and the current game can continue. If the single player game is selected it jumps down to void play1menu() where your given the difficulty option. The difficulty is set by the number of turns you want to play and stored as turnlimit

Step 4: Coding the 2 Player Game.

To code the 2 Player game i decided it would be best to build it as its own sketch then integrate it into the main sketch. This allowed me to more easily troubleshoot any problems i came across. I had been bouncing around ideas for the code in my head for the past couple of days. So i knew what the general flow of the code would be. But it took 4 straight hours of coding to finally get it to work. It always amazes me how the littlest mistakes can cause so many problem. Even the misplacement of a single line at the beginning of a function when it would work better at the end can cause such problems. 

I decided to go ahead and include the code from the 2 player standalone game. The code isn't as refined and may be a bit buggy. But it was only meant as a template for getting the 2 player option up and running. There was no point in refining it when it was going to be copied over to the main code anyway. I started out with the Simon Says game from my other Instructable and built the 2 player game up from there after removing unneeded code like most of the audio side of the code and serial calls.

#include <Tone.h> //call to tone library
#include <LiquidCrystal.h> //call to LiquidCrustal library

Tone speakerpin; //enables the speaker

LiquidCrystal lcd(13, 12, 16, 17, 18, 19); //pins for LCD display

boolean button[] = {2, 3, 4, 5}; //The four button input pins
boolean ledpin[] = {8, 9, 10, 11};  // LED pins

int turn = 0;

int buttonstate = 0;  // button state checker
int randomArray[100]; //Intentionally long to store up to 100 inputs (doubtful anyone will get this far)
int inputArray[100];  //used for checking button pushes against the array
int player = 1;
int progress = 0;

void setup()
{
  lcd.begin(16, 2);
  speakerpin.begin(6); // speaker is on pin 13

  for(int x=0; x<4; x++)  // LED pins are outputs
  {
    pinMode(ledpin[x], OUTPUT);
  }

  for(int x=0; x<4; x++)
  {
    pinMode(button[x], INPUT);  // button pins are inputs
    digitalWrite(button[x], HIGH);  // enable internal pullup; buttons start in high position; logic reversed
  }
lcd.clear();
}


void loop()
{
  lcd.clear();
  if (player == 1)
    {
      lcd.setCursor(0,0);          
      lcd.print("Player 1!");    
    }
    if (player == 2)
    {
      lcd.setCursor(0,0);     
      lcd.print("Player 2!");    
    }   
    lcd.setCursor(0,1);
    lcd.print("Choose a Button");
    delay(150);
  for(int v=0; v<4; v++)
    {

    buttonstate = digitalRead(button[v]);



    if (buttonstate == LOW && button[v] == 2)
    { //Checking for button push
      digitalWrite(ledpin[0], HIGH);
      speakerpin.play(NOTE_G3, 100);
      delay(500);
      digitalWrite(ledpin[0], LOW);
      randomArray[progress] = 1;

      if (player == 1)
      {
        player++;
        progress++;
        delay(500);
        output();
        return;       
      }
      if (player == 2)
      {
        player--;
        progress++;
        delay(500);
        output();       
        return;       
      }

    }
    if (buttonstate == LOW && button[v] == 3)
    {
      digitalWrite(ledpin[1], HIGH);
      speakerpin.play(NOTE_A3, 100);
      delay(200);
      digitalWrite(ledpin[1], LOW);
      randomArray[progress] = 2;

      if (player == 1)
      {
        player++;
        progress++;
        delay(500);
        output();       
        return;       
      }
      if (player == 2)
      {
        player--;
        progress++;
        delay(500);
        output();       
        return;       
      }

    }

    if (buttonstate == LOW && button[v] == 4)
    {
      digitalWrite(ledpin[2], HIGH);
      speakerpin.play(NOTE_B3, 100);
      delay(200);
      digitalWrite(ledpin[2], LOW);
      randomArray[progress] = 3;

      if (player == 1)
      {
        player++;
        progress++;
        delay(500);
        output();
        return;       
      }
      if (player == 2)
      {
        player--;
        progress++;
        delay(500);
        output();
        return;       
      }

    }

    if (buttonstate == LOW && button[v] == 5)
    {
      digitalWrite(ledpin[3], HIGH);
      speakerpin.play(NOTE_C4, 100);
      delay(200);
      digitalWrite(ledpin[3], LOW);
      randomArray[progress] = 4;

      if (player == 1)
      {
        player++;
        progress++;
        delay(500);
        output();
        return;       
      }
      if (player == 2)
      {
        player--;
        progress++;
        delay(500);
        output();
        return;       
      }

    }


}


void output()
{       
  lcd.clear();
  if (player == 1)
    {
      lcd.setCursor(0,0);          
      lcd.print("Player 1");
    }
    if (player == 2)
    {
      lcd.setCursor(0,0);     
      lcd.print("Player 2");
    }   
  lcd.setCursor(0,1);
  lcd.print("Watch the lights");
  delay(1000);
    for (int x=0; x <= turn; x++)
    {


      for(int y=0; y<4; y++)
      {

        if (randomArray[x] == 1 && ledpin[y] == 8)
        {  //if statements to display the stored values in the array
          digitalWrite(ledpin[y], HIGH);
          speakerpin.play(NOTE_G3, 100);
          delay(400);
          digitalWrite(ledpin[y], LOW);
          delay(100);
         }
        if (randomArray[x] == 2 && ledpin[y] == 9)
        {
          digitalWrite(ledpin[y], HIGH);
          speakerpin.play(NOTE_A3, 100);
          delay(400);
          digitalWrite(ledpin[y], LOW);
          delay(100);
        }

        if (randomArray[x] == 3 && ledpin[y] == 10)
        {
          digitalWrite(ledpin[y], HIGH);
          speakerpin.play(NOTE_B3, 100);
          delay(400);
          digitalWrite(ledpin[y], LOW);
          delay(100);
        }

        if (randomArray[x] == 4 && ledpin[y] == 11)
        {
          digitalWrite(ledpin[y], HIGH);
          speakerpin.play(NOTE_C4, 100);
          delay(400);
          digitalWrite(ledpin[y], LOW);
          delay(100);
        }
      }
    }
  input2();
}



void input2() { //Function for allowing user input and checking input against the generated array
  lcd.clear();
    if (player == 1)
    {
      lcd.setCursor(0,0);          
      lcd.print("Player 1");
    }
    if (player == 2)
    {
      lcd.setCursor(0,0);     
      lcd.print("Player 2");
    }   
    lcd.setCursor(0,1);
    lcd.print("Repeat sequence");
    delay(150);

  for (int x=0; x <= turn;)
  { //Statement controlled by turn count

    for(int y=0; y<4; y++)
    {

      buttonstate = digitalRead(button[y]);

      if (buttonstate == LOW && button[y] == 2)
      { //Checking for button push
        digitalWrite(ledpin[0], HIGH);
        speakerpin.play(NOTE_G3, 100);
        delay(200);
        digitalWrite(ledpin[0], LOW);
        inputArray[x] = 1;
        delay(250);

        if (inputArray[x] != randomArray[x]) { //Checks value input by user and checks it against
          fail();                              //the value in the same spot on the generated array
        }                                      //The fail function is called if it does not match
        x++;

      }
       if (buttonstate == LOW && button[y] == 3)
      {
        digitalWrite(ledpin[1], HIGH);
        speakerpin.play(NOTE_A3, 100);
        delay(200);
        digitalWrite(ledpin[1], LOW);
        inputArray[x] = 2;
        delay(250);

        if (inputArray[x] != randomArray[x]) {
          fail();
        }
        x++;

      }

      if (buttonstate == LOW && button[y] == 4)
      {
        digitalWrite(ledpin[2], HIGH);
        speakerpin.play(NOTE_B3, 100);
        delay(200);
        digitalWrite(ledpin[2], LOW);
        inputArray[x] = 3;
        delay(250);

        if (inputArray[x] != randomArray[x]) {
          fail();
        }
        x++;

      }

      if (buttonstate == LOW && button[y] == 5)
      {
        digitalWrite(ledpin[3], HIGH);
        speakerpin.play(NOTE_C4, 100);
        delay(200);
        digitalWrite(ledpin[3], LOW);
        inputArray[x] = 4;
        delay(250);

        if (inputArray[x] != randomArray[x])
        {
          fail();
        }
        x++;

      }
    }
  }
  delay(500);
  turn++; //Increments the turn count, also the last action before starting the output function over again
loop();
}

void fail() { //Function used if the player fails to match the sequence
  lcd.clear();
  if (game == 2 && player == 1)
    {
      lcd.setCursor(0,0);          
      lcd.print("Player 1!");    
    }
    if (game == 2 && player == 2)
    {
      lcd.setCursor(0,0);     
      lcd.print("Player 2!");    
    }   
    lcd.setCursor(0,1);
    lcd.print("YOU LOOSE!");
    delay(150);

  for (int y=0; y<=2; y++)
  { //Flashes lights for failure

    digitalWrite(ledpin[0], HIGH);
    digitalWrite(ledpin[1], HIGH);
    digitalWrite(ledpin[2], HIGH);
    digitalWrite(ledpin[3], HIGH);
    speakerpin.play(NOTE_G3, 300);
    delay(200);
    digitalWrite(ledpin[0], LOW);
    digitalWrite(ledpin[1], LOW);
    digitalWrite(ledpin[2], LOW);
    digitalWrite(ledpin[3], LOW);
    speakerpin.play(NOTE_C3, 300);
    delay(200);
  }
  delay(500);
  for (int x=0; x<100; x++) //clears array
  {
    randomArray[x] = 0;
  }
  progress = 0;
  player = 1;
  turn = -1; //Resets turn value so the game starts over without need for a reset button
}

If you decide to try this code out be aware there may be some bugs. The tones for the Buttons/LEDs are there but all other music and tones are not. 

Step 5: Tightening the Code

While integrating the 2 player option into the game i realized the input() function of both options where the same. All i needed to do was add a couple of lines of code at the end of the function so the game can handle the turn counting based on which game is being played. For the 1 Player game it would compare the number of turns taken to the turn limit and announce the player as a winner if the turn counter is greater. For the 2 player option it checks to make sure the players are not going to exceed the 100 turn limit of the game. 

Next i went through the code to find other ways to tighten it. I realized that any time a button was hit or an LED needed to be light the same basic lines of code where used. So i created 4 functions to call to whenever a button was pressed or the game was lighting the LEDs for you to memorize. 

void button1()  //LED displayed and tone sounded for RED LED and Button 1
{
  digitalWrite(ledpin[0], HIGH);
  speakerpin.play(NOTE_G3, 100);
  delay(200);
  digitalWrite(ledpin[0], LOW);
  delay(300);
}


void button2()
{
  digitalWrite(ledpin[1], HIGH);
  speakerpin.play(NOTE_A3, 100);
  delay(200);
  digitalWrite(ledpin[1], LOW);
  delay(300);
}


void button3()
{
  digitalWrite(ledpin[2], HIGH);
  speakerpin.play(NOTE_B3, 100);
  delay(200);
  digitalWrite(ledpin[2], LOW);
  delay(300);
}


void button4()
{
  digitalWrite(ledpin[3], HIGH);
  speakerpin.play(NOTE_C4, 100);
  delay(200);
  digitalWrite(ledpin[3], LOW);
  delay(300);
}

All that was need is one function per Button/LED color. Any time an LED lights or the button is pressed the LED lights up, the same note is played, and the LED is turned off. These functions are used 22 time throughout the code. Reducing the code by about 1000 bytes. 

Many other little tweaks and changes where made throughout the coding process that help to tighten the code and make it more efficient. 

Step 6: Final Code

So here it is the final prototype!. I have attached the file with the full sketch. I won't copy it here since its a pretty long code. In total it takes up 9862 bytes on the Arduino. I wouldn't be surprised if the code could be tightened up further. My programming skills are still pretty limited. So if anyone wants to improve upon it i'd love to see what you come up with. I'm sure i'll be improving the code further in the days and weeks to come. I always do.

Important Note: Just like the Arduino Simon Says game this one uses the Tone library. As mentioned in the other Instructable the library hasn't been updated to be compatible with Arduino 1.0. So i'm including the library here. Alternately you can Download it from the creator and update the code yourself. All you need to do is open the Tone.cpp file and change #include <wiring.h> to #include <arduino.h>


The Next step may take some time and money. The idea is to put this into a permanent enclosure. Well not everything.  I'd like to move the Atmega328 chip to its own PCB. I want to turn this into a nice electronic game my kids can play with. So while i'm trying to pull the money together i can at least start designing the PCB for it. Weather i actually have any produced, or etch one myself is still up in the air. Though it will likely end up on a perf board. I'll still design the PCB anyway.  Gotta have something to do with my time. ;)


Parts list for final build:
  • 4x back lighted arcade style buttons.
  • new Hitachi HD44780 based 16x2 LCD
  • 6 AA or 6AAA Battery pack
  • Smallest enclosure possible to fit everything into

I look forward to any comments and suggestions you might have. 

UPDATE:

I've just added an update to the game. Total Recall 1.04 has had a few changes made.
  • Added sounds to indicate player 1 and player 2 in 2 player game.
  • New option to select speed of game
  • Changed single player winning tune
  • Menu spelling fixes
Also its important to note that the 1.04 update has changed the pins for the LCD in preparation for a PCB to be made. Pins 12 and 13 on the arduino need to swapped if you want to run the 1.04 code.

I've included previews of the final game layout in both breadboard and PCB views. Better images and the full Fritzing file will be made available at a later date.
Arduino Challenge

Participated in the
Arduino Challenge