Memory game

For the first project, I created a game that would play a pattern of LEDs, which the user would then have to replicate by pressing the corresponding buttons.  If the user successfully completed the task, the LEDs would flash to show the user has won.  I tried to implement a randomizer to create a new pattern each time, but it didn’t end up working so for now there is only one pattern of lights for the user to play.

Code:

int red = 11;                 // red LED connected to digital pin 11
int blue = 12;                // blue LED connected to digital pin 12
int green = 13;              // green LED connected to digital pin 13
int redButton = 2;          // button for red LED connected to pin 2
int blueButton = 3;          // button for blue  LED connected to pin 3
int greenButton = 4;        // button for green LED connected to pin 4
int val1 = 0;
int val2 = 0;
int val3 = 0;
int prev1 = LOW;
int prev2 = LOW;
int prev3 = LOW;
long time = 0;
long debounce = 200;

boolean playing = false;
boolean won;

long num1;
long num2;
long num3;
long num4;
long num5;

void setup()
{
pinMode(red, OUTPUT);      // sets the LED pins as output
pinMode(blue, OUTPUT);
pinMode(green, OUTPUT);
pinMode(redButton, INPUT);  //sets the button pins as input
pinMode(greenButton, INPUT);
pinMode(blueButton, INPUT);
}

//set up play pattern array
int pattern[]={
red,blue,blue,green,red};
int results[]={
};

void loop()
{
if (playing==false){
playPattern();
playing=true;
}
else {
playerTurn();
}

/* TEST LEDS CODE
if(val1==LOW) {
digitalWrite(red, HIGH);
} else {
digitalWrite(red, LOW);
}

if(val2==LOW) {
digitalWrite(blue, HIGH);
} else {
digitalWrite(blue, LOW);
}

if(val3==LOW) {
digitalWrite(green, HIGH);
} else {
digitalWrite(green, LOW);
}
*/
}

void playPattern()
{
digitalWrite(pattern[0], HIGH);   // sets the LED on
delay(1000);                  // waits for a second
digitalWrite(pattern[0], LOW);    // sets the LED off
delay(500);
digitalWrite(pattern[1], HIGH);
delay(1000);                  // waits for a second
digitalWrite(pattern[1], LOW);
delay(500);
digitalWrite(pattern[2], HIGH);
delay(1000);
digitalWrite(pattern[2], LOW);
delay(500);
digitalWrite(pattern[3], HIGH);
delay(1000);
digitalWrite(pattern[3], LOW);
delay(500);
digitalWrite(pattern[4], HIGH);
delay(1000);
digitalWrite(pattern[4], LOW);
delay(500);
digitalWrite(pattern[5], HIGH);
delay(1000);
digitalWrite(pattern[5], LOW);
delay(1000);
}

void playerTurn()
{
for(int i=0; i<5; i++) {

val1 = digitalRead(redButton);
val2 = digitalRead(greenButton);
val3 = digitalRead(blueButton);

if(val1==HIGH && prev1==LOW && millis() – time > debounce) {
//red was pressed
digitalWrite(red, HIGH);   // sets the LED on
delay(500);                  // waits for a second
digitalWrite(red, LOW);    // sets the LED off
results[i]=red;
time = millis();
}
if(val2==HIGH && prev2==LOW && millis() – time > debounce) {
//green was pressed
digitalWrite(green, HIGH);   // sets the LED on
delay(500);                  // waits for a second
digitalWrite(green, LOW);    // sets the LED off
results[i]=green;
time = millis();
}
if(val3==HIGH && prev3==LOW && millis() – time > debounce) {
//blue was pressed
digitalWrite(blue, HIGH);   // sets the LED on
delay(500);                  // waits for a second
digitalWrite(blue, LOW);    // sets the LED off
results[i]=blue;
time = millis();
}
prev1=val1;
prev2=val2;
prev3=val3;
}

for(int j=0; j<5; j++) {
if (pattern[j] != results[j]) {
won=false;
}
}

if (won==false) {
//play pattern again
}
else {
//flash lights
for(int k=0; k<5; k++) {
digitalWrite(red, HIGH);   // sets the LED on
digitalWrite(green, HIGH);    // sets the LED on
digitalWrite(blue, HIGH);    // sets the LED on
delay(100);                  // waits for a second
digitalWrite(red, LOW);    // sets the LED off
digitalWrite(green, LOW);    // sets the LED off
digitalWrite(blue, LOW);      // sets the LED off
delay(100);                  // waits for a second
}
}

}

Leave a comment