Sunday, March 7, 2010

LED Project - Simon Says

Simon Says

Overview:
A simple memory game were players a shown a sequence of colours (red, green, yellow, and blue) that they must remember and repeat in the same order using four related buttons. Each time a sequence is correctly matched, the level in increased, and the sequence extended by one. When the sequence is incorrectly matched, the game ends, and is reset.

Fritz:










Video:


Code:
A simple memory game were players a shown a sequence of colours (red, green, yellow, and blue) that they must remember and repeat in the same order using four related buttons. Each time a sequence is correctly matched, the level in increased, and the sequence extended by one. When the sequence is incorrectly matched, the game ends, and is reset.

// constants won't change. They're used here to
// -set pin numbers:
// the number of the LED pins:
const int blueLedPin = 12;
const int yellowLedPin = 11;
const int greenLedPin = 10;
const int redLedPin = 9;
const int correctLedPin = 8;
const int incorrectLedPin = 7;
const int buzzerPin = 6;

// the number of the pushbutton pins:
const int blueButtonPin = 2;
const int yellowButtonPin = 3;
const int greenButtonPin = 4;
const int redButtonPin = 5;

const int smallestLedPinValue = 9;
const int highestLedPinValue = 12;
const int sequenceDelay = 500; //half a second
const int finishDelay = 3000; //three seconds
const int defaultLevel = 3;
// global variables will change:

int level = defaultLevel;


// -variables for reading the pushbutton status:
int blueButtonState = 0;
int yellowButtonState = 0;
int greenButtonState = 0;
int redButtonState = 0;

//memory arrays
int computerMemory[20]; // holds the computers colour sequence.
int playerMemory[20]; // holds the players sequence.

//==================================================================

void setup()
{
// initialize the LED pins as an output:
pinMode(blueLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
pinMode(correctLedPin, OUTPUT);
pinMode(incorrectLedPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);

// initialize the pushbutton pins as an input:
pinMode(blueButtonPin, INPUT);
pinMode(yellowButtonPin, INPUT);
pinMode(greenButtonPin, INPUT);
pinMode(redButtonPin, INPUT);

Serial.begin(9600);
randomSeed(analogRead(0));
}

//==================================================================

void loop()
{
displaySequence();
playerSequence();
compareSequences();
}

//==================================================================

void displaySequence()
//displays and records colour sequence:
{
int randomLed;
for (int i = 0; i < level; i++)
{
randomLed = random(smallestLedPinValue,(highestLedPinValue + 1)); //creates a random number between the smallest and highest pin values.
computerMemory[i] = randomLed;
digitalWrite(randomLed, HIGH);//lights the random pin.
delay(sequenceDelay);
digitalWrite(randomLed, LOW);
delay(sequenceDelay);
}//end of for loop.
}//end of method.

//==================================================================

void playerSequence()
// waits for the player to push enough buttons to match the computer sequence. Each button pushed "lights up" its related LED and records
//the number of that LedPin in the "playerMemory" array.
//note to self*
{
int playerPushCount = 0;
int arrayCounter = 0;
while(playerPushCount < level)
{
blueButtonState = digitalRead(blueButtonPin);
yellowButtonState = digitalRead(yellowButtonPin);
greenButtonState = digitalRead(greenButtonPin);
redButtonState = digitalRead(redButtonPin);

if (blueButtonState == HIGH)
{
digitalWrite(blueLedPin, HIGH);
digitalWrite(buzzerPin, HIGH);
while(blueButtonState == HIGH)
{
blueButtonState = digitalRead(blueButtonPin);
}
playerMemory[arrayCounter] = blueLedPin;
arrayCounter++;
playerPushCount++;
digitalWrite(blueLedPin, LOW);
digitalWrite(buzzerPin, LOW);
}

if (redButtonState == HIGH)
{
digitalWrite(redLedPin, HIGH);
while(redButtonState == HIGH)
{
redButtonState = digitalRead(redButtonPin);
}
playerMemory[arrayCounter] = redLedPin;
arrayCounter++;
playerPushCount++;
digitalWrite(redLedPin, LOW);
}

if (yellowButtonState == HIGH)
{
digitalWrite(yellowLedPin, HIGH);
while(yellowButtonState == HIGH)
{
yellowButtonState = digitalRead(yellowButtonPin);
}
playerMemory[arrayCounter] = yellowLedPin;
arrayCounter++;
playerPushCount++;
digitalWrite(yellowLedPin, LOW);
}


if (greenButtonState == HIGH)
{
digitalWrite(greenLedPin, HIGH);
while(greenButtonState == HIGH)
{
greenButtonState = digitalRead(greenButtonPin);
}
playerMemory[arrayCounter] = greenLedPin;
arrayCounter++;
playerPushCount++;
digitalWrite(greenLedPin, LOW);
}

}
}

//==================================================================

void compareSequences()
//compares each element in arrays computerMemory and PlayerMemory. each match found adds "1" to "correct". If the number of correct choices equals
//the level (the level is also the amount of elements in a sequence), then the next level begins, otherwise, the player loses, their final score is displayed and the game is reset.
{
int correct = 0;
for (int i = 0; i < level; i++)
{
if (computerMemory[i] == playerMemory[i])
{
correct++;
}
}

if (correct == level)
{
level++;
digitalWrite(correctLedPin, HIGH);
delay(finishDelay);
digitalWrite(correctLedPin, LOW);
Serial.println("Well Done!");
}
else
{
Serial.print("Game Over!, you reached level ");
Serial.println(level);
level = defaultLevel;
digitalWrite(incorrectLedPin, HIGH);
delay(finishDelay);
digitalWrite(incorrectLedPin, LOW);
}
}


/* *i could have used an array of buttonstates and pins to stop duplicate code e.g.
for (i=0; i < numberOfButtons;i++)
{
while(buttonaarray[i] = HIGH)
LedPin[i] = HIGH
each ledpin in the array relates to a button in the button array. it wont work if they dont relate*/

No comments:

Post a Comment