Kyle Thompson - Simon Says

Project Description:

To create a version of the popular kids game "Simon Says."  I want to use 4 LEDs to simulate the 4 buttons to press. The user input will be via a keypad. The user must follow the same pattern as the lights or they fail. A 5th LED will light up red when the user has failed. If the user is able to follow the lights all the way to till the end, then all 4 lights will illuminate white, indicating victory.

I interfaced with an LED Driver (MAX6969) and keypad. I found a keypad library and used it to interface with the keypad.

Setup:

In this setup, Keypad Button 1 = Red, Button 2 = Blue, Button 4 = Green, Button 5 = Yellow.

Arduino Code:

#include <Keypad.h>

/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'0','F','E','D'}
};
byte rowPins[ROWS] = {6, 7, 8, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {2, 3, 4, 5}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

int data = 10;
int clk = 11;
int le = 12;
int roundMax = 20;
byte Red = B00000001;
byte Green = B00010000;
byte Blue = B00100000;
byte Yellow = B00011000;
byte LOSER = B00000001;
long randNumber;
int i = 0;
int keyTally = 0;
boolean stopLoop = false;
boolean USUCK = false;
byte shift1 = B00000000;
byte shift2 = B00000000;

void setup(){
  Serial.begin(9600);
  customKeypad.setDebounceTime(250);
  pinMode(data,OUTPUT);
  pinMode(clk,OUTPUT);
  pinMode(le,OUTPUT);
}

int whatLED(int randNumber){
    byte result;
    if (randNumber == 0){
        shift2 = B00000000;
        shift1 = Red;
    }
    else if (randNumber == 1){
        shift2 = B00000000;
        shift1 = Green;
    }
    else if (randNumber == 2){
        shift1 = B00000000;
        shift2 = Blue;
    }
    else if (randNumber == 3){
        shift1 = B00000000;
        shift2 = Yellow;
    }
    return result;
}

 
void loop(){
    char key = customKeypad.getKey();  
    if (key != NO_KEY){
        Serial.println(key);
    }
    digitalWrite(le,LOW);
    shiftOut(data,clk,MSBFIRST,shift2);   // Right Side
    shiftOut(data,clk,MSBFIRST,shift1);   // Left Side
    digitalWrite(le,HIGH);
    delay(1);

    if (key != NO_KEY && stopLoop == false){  
    do{         
               
               if (randNumber == 0 && (key == '2' || key == '4' || key == '5')){   // RED
                            USUCK = true;
                            stopLoop = true;
               }
               else if (randNumber == 1 && (key == '1' || key == '2' || key == '5')){  // GREEN
                            USUCK = true;
                            stopLoop = true;
               }
               else if (randNumber == 2 && (key == '1' || key == '4' || key == '5')){  // BLUE
                            USUCK = true;
                            stopLoop = true;
               }
               else if (randNumber == 3 && (key == '1' || key == '2' || key == '4')){  // YELLOW
                            USUCK = true;
                            stopLoop = true;
               }
               randNumber = random(4);
               Serial.println(randNumber);
               whatLED(randNumber);
               if (USUCK == true){
                   shift2 = LOSER;
                   shift1 = B00000000;
               }
               keyTally++;
               i++;               
       }while (i < roundMax && keyTally == (i-1) && stopLoop == false);
    if (i == roundMax){
        stopLoop = true;
        shift1 = B11111111;
        shift2 = B11111110;
    }
  }
}

Future Additions:

If I had additional time I would make my program more like Simon Says, where the system remembers a sequence and repeats it, adding one move each time.

I would also build on my current version of the game and make the time required to respond faster and faster, which adds more risk of losing.

Demo:

http://www.youtube.com/watch?v=xEY4UXF-pvM