Memory Game With Arduino

by JK792515 in Circuits > Arduino

17 Views, 0 Favorites, 0 Comments

Memory Game With Arduino

download.png

This project will testing your memory.


This Memory Game tests your ability to recall randomly generated sequences of four LEDs. The user will enter their answer using the buttons.


If you answer incorrectly or run out of time, the RGB led will turn red and the game will restart. The RGB LED will become blue after the user has entered the correct answer for all rounds.


7-Segment Display will be used to display each round number.

Supplies

partsneeded.jpg
  • Arduino Uno
  • Bread Board
  • RDG LED (The one used in this project is Common Anode (CA))
  • LED (Green, Blue, Red, Yellow)
  • Piezo buzzer
  • 7 Segment Display (The one used in this project is Common Anode (CA))
  • 4 Buttons (one for each LED Green, Blue, Red, Yellow)
  • 4x 10k resistors (used for the buttons)
  • 4x 330 ohm resistors (used for the LEDs)
  • 3x 560 ohm resistors (used for the RGB LED and 7 Segment Display)

Assemble

IMG-3334.jpg
IMG-3332.jpg

Here are some picture of how the wiring is suppose to look.

If you can't find where some wires go check out the code to see what goes where

The RGB LED only uses red and blue pin. The Green RGB pin is given power as the component is a Common Anode.

Code

arduino-bg.jpg

This is the code you will need for the memory game to function.


int button[] = {13, 8, 10, 4}; //yellow is button[0], green is button[1], red is button[2], blue is button[3]

int led[] = {12, 9, 11, 7}; //yellow is led[0], green is led[1], red is led[2], blue is led[3]


const int blue_RGB = 5; //blue will be used to indicate that they won the game

//const int green_RGB = 5; //use       ` `d to indicate if user is correct

const int red_RGB = 6; //used to indicate if user is wrong


int roundsToWin = 5; //number of rounds the player has to play to win.

int buttonSequence[6]; //makes an array of numbers that will be sequences that the user will need to remember


int pressedButton = 4; //a variable to remember which button is pressed. 4 is no button pressed.

int roundCounter = 1; //keeps track of the round the player is on


long startTime = 0; //time variable for time limit on button press

long timeLimit = 2000; //time limit to hit a button


boolean gameStarted = false;


const int a = A3; //makes 1 part of the 7 Segment Display Light up

const int b = A4; //makes 1 part of the 7 Segment Display Light up

const int c = A5; //makes 1 part of the 7 Segment Display Light up

const int d = A0; //makes 1 part of the 7 Segment Display Light up

const int e = 2; //makes 1 part of the 7 Segment Display Light up

const int f = A2; //makes 1 part of the 7 Segment Display Light up

const int g = A1; //makes 1 part of the 7 Segment Display Light up


void setup() {

 pinMode(button[0], INPUT);

 pinMode(button[1], INPUT);

 pinMode(button[2], INPUT);

 pinMode(button[3], INPUT);


 pinMode(led[0], OUTPUT);

 pinMode(led[1], OUTPUT);

 pinMode(led[2], OUTPUT);

 pinMode(led[3], OUTPUT);


 pinMode(blue_RGB, OUTPUT);

 //pinMode (green_RGB, OUTPUT);

 pinMode(red_RGB, OUTPUT);


 pinMode(a, OUTPUT);

 pinMode(b, OUTPUT);

 pinMode(c, OUTPUT);

 pinMode(d, OUTPUT);

 pinMode(e, OUTPUT);

 pinMode(f, OUTPUT);

 pinMode(g, OUTPUT);

}


void smiley() {

 digitalWrite(a, LOW);

 digitalWrite(b, HIGH);

 digitalWrite(c, LOW);

 digitalWrite(d, HIGH);

 digitalWrite(e, LOW);

 digitalWrite(f, HIGH);

 digitalWrite(g, HIGH);

}


void one() {

 digitalWrite(a, HIGH);

 digitalWrite(b, HIGH);

 digitalWrite(c, HIGH);

 digitalWrite(d, HIGH);

 digitalWrite(e, LOW);

 digitalWrite(f, LOW);

 digitalWrite(g, HIGH);

}


void two() {

 digitalWrite(a, LOW);

 digitalWrite(b, LOW);

 digitalWrite(c, HIGH);

 digitalWrite(d, LOW);

 digitalWrite(e, LOW);

 digitalWrite(f, HIGH);

 digitalWrite(g, LOW);

}


void three() {

 digitalWrite(a, LOW);

 digitalWrite(b, HIGH);

 digitalWrite(c, HIGH);

 digitalWrite(d, LOW);

 digitalWrite(e, LOW);

 digitalWrite(f, LOW);

 digitalWrite(g, LOW);

}


void four() {

 digitalWrite(a, HIGH);

 digitalWrite(b, HIGH);

 digitalWrite(c, LOW);

 digitalWrite(d, HIGH);

 digitalWrite(e, LOW);

 digitalWrite(f, LOW);

 digitalWrite(g, LOW);

}


void displayNumber(int number) {

 switch (number) {

  case 1:

   one();

   break;

  case 2:

   two();

   break;

  case 3:

   three();

   break;

  case 4:

   four();

   break;

  default:

   smiley();

   break;

 }

}


void winGame() {

 digitalWrite(blue_RGB, HIGH);

 delay(2000);

 digitalWrite(blue_RGB, LOW);

}


void loseGame() {

 digitalWrite(red_RGB, HIGH);

 delay(2000);

 digitalWrite(red_RGB, LOW);

}


void generateSequence() {

 for (int i = 0; i < roundsToWin; i++) {

  buttonSequence[i] = random(0, 4);

 }

}


void playSequence(int round) {

 for (int i = 0; i < round; i++) {

  digitalWrite(led[buttonSequence[i]], HIGH);

  delay(1000);

  digitalWrite(led[buttonSequence[i]], LOW);

  delay(500);

 }

}


void waitForButtonPress() {

 startTime = millis();

 while (true) {

  if (millis() - startTime > timeLimit) {

   pressedButton = -1;

   return;

  }

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

   if (digitalRead(button[i]) == HIGH) {

    pressedButton = i;

    return;

   }

  }

 }

}


boolean isButtonSequenceCorrect(int round) {

 for (int i = 0; i < round; i++) {

  waitForButtonPress();

  if (pressedButton != buttonSequence[i]) {

   return false;

  }

 }

 return true;

}


void loop() {

 if (!gameStarted) {

  generateSequence();

  playSequence(roundCounter);

  gameStarted = true;

 }


 if (isButtonSequenceCorrect(roundCounter)) {

  roundCounter++;

  if (roundCounter > roundsToWin) {

   winGame();

   gameStarted = false;

   roundCounter = 1;

  } else {

   playSequence(roundCounter);

  }

 } else {

  loseGame();

  gameStarted = false;

  roundCounter = 1;

 }


 displayNumber(roundCounter);

 delay(1000);

}