Memory Game
The following project is a Memory Game to see just how well your memory works.
The 4 LEDs will light up in a randomized order and gets more difficult to do each round.
You can turn on the game using any button, this will light up the LEDs and then shut them off. One LED will blink once, when that happens hit the button in front of it. This will go on 9 times with 9 different flashes. Once complete a victory sequence will start. If you mess up a loss sequence will start. Be fast, the is a time interval of for seconds between each press of the button. Run out of time and you'll lose.
Supplies
- Arduino Uno
- Bread Board
- 4 LEDs of any colour
- RBG LED
- Piezo buzzer
- Wires (try to include black and red for power connections)
- 7 Segment Display
- 4 Buttons
- 4x 10k resistors
- 4x 330 ohm resistors
- 3x 560 ohm resistors
Components on the Bread Board
Start by evenly spacing your LEDs on the Bread Board (put the anode (longer side) of the LED on the positive line). Place the Buttons in front of each LED over the indent, make sure the button pins do not vertically line up with the LED pin. Then horizontally place the 330 ohm resistors with one pin lied up with the LED and the other past the button pin. Place the 10k resistors on the negative line and Red wires one the positive, both lined up with the button pins. Place the Piezo buzzer and connect it to the negative using a Black wire. Place the RBG LED and connect the middle 2 pin with a Wire one negative and a 560 ohm resistor on positive (make sure the resistor is connected to the anode). Place down the 7 Segment Display connect the middle pins on either side to the negative with 560 ohm resistors. Finally, add a red and a black wire connecting each positive and negative line so the power can cross over.
The Wiring
For the Wiring make the following connections. For the LEDs connect then from the resistors to pins 7, 9, 11, and 13 in the order shown in the image. The Buttons should connect to pins 4, 8, 10, and 12 right beside their corresponding LED connections (besides pin 4). The remaining RBG LED pins should connect to pins 5 and 6 as per the image. The Piezo buzzer should connect to pin 3 (make sure the positive end is what connects). The 7 Segment Display should make the following connections (use the image above as reference). Connect pin a to pin A3, pin b to pin A4, pin c to pin A5, pin d to pin A0, pin e to pin 2, pin f to pin A2, and pin g to pin A1. Don't forget to connect pin 5V to the positive and GND to the negative.
The Code
The following code should help start up the memory game. If it doesn't start try to look for any possible errors whether in the build, hardware, components, etc.
The Code:
int button[] = {12, 10, 8, 4}; //yellow is button[0], green is button[1], red is button[2], blue is button[3]
int led[] = {13, 11, 9, 7}; //yellow is led[0], green is led[1], red is led[2], blue is led[3]
int tones[] = {265, 330, 392, 494}; //tones to play with eavh button (c, e, g, b)
const int buzzerPin = 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 = 9; //number of round 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 variavle to remeber which vutton is pressed. 4 is no button pressed.
int roundCounter = 1; //keeps track of round the player is on
long startTime = 0; //time variale 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);
pinMode(buzzerPin, 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 five() {
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}
void six() {
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}
void seven() {
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
}
void eight() {
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}
void nine() {
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}
//Flashes LED in generated sequence
void flashLED (int ledNumber) {
digitalWrite(led[ledNumber], HIGH);
tone(buzzerPin, tones[ledNumber]);
}
void setColour(int r, int b) {
analogWrite(red_RGB, r);
//analogWrite(green_RGB, g);
analogWrite(blue_RGB, b);
}
//Turns off all the leds
void allLEDoff() {
digitalWrite(led[0], LOW);
digitalWrite(led[1], LOW);
digitalWrite(led[2], LOW);
digitalWrite(led[3], LOW);
noTone(buzzerPin);
}
//Checks which button is pressed
int buttonCheck() {
if (digitalRead(button[0]) == HIGH){
tone(buzzerPin, tones[0]);
return 0;}
else if (digitalRead(button[1]) == HIGH){
tone(buzzerPin, tones[1]);
return 1;}
else if (digitalRead(button[2]) == HIGH){
tone(buzzerPin, tones[2]);
return 2;}
else if (digitalRead(button[3]) == HIGH){
tone(buzzerPin, tones[3]);
return 3;}
else
return 4; //this will be the value for no button being pressed
}
//Checks if any button is pressed to restart the game
int restartCheck() {
if (digitalRead(button[0]) == HIGH){
return 0;}
else if (digitalRead(button[1]) == HIGH){
return 1;}
else if (digitalRead(button[2]) == HIGH){
return 2;}
else if (digitalRead(button[3]) == HIGH){
return 3;}
else
return 4; //this will be the value for no button being pressed
}
//Generated random sequence and starts the sequence
void startSequence() {
//wait until a button is pressed
do {
pressedButton = buttonCheck();
//RGB LED off
setColour(255,255);
} while (pressedButton > 3);
delay(100);
//randomSeed(analogRead(A0)); //makes sure the random numbers are generated
//takes these generated values and stores it in the buttonSequence array with random numbers from 0 to 3
for (int i = 0; i <= roundsToWin; i++) {
buttonSequence[i] = round(random(0, 4));
}
//flashes all the LEDs when game is starting
for (int i = 0; i < 4; i++){
tone(buzzerPin, tones[i], 200);
//turns them all on
digitalWrite(led[0], HIGH);
digitalWrite(led[1], HIGH);
digitalWrite(led[2], HIGH);
digitalWrite(led[3], HIGH);
delay(100);
//turns them all off after a moment
digitalWrite(led[0], LOW);
digitalWrite(led[1], LOW);
digitalWrite(led[2], LOW);
digitalWrite(led[3], LOW);
delay(100); }
smiley();
}//this will repeat 4 times before the game starts
//implemented if user answer is wrong or they crossed time limit
void loseSequence () {
delay(100);
setColour(0,255);
delay(250);
setColour(255,255);
smiley();
//lose Sequence plays
tone(buzzerPin, 130, 250); //E6
delay(275);
tone(buzzerPin, 73, 250); //G6
delay(275);
tone(buzzerPin, 65, 150); //E7
delay(175);
tone(buzzerPin, 98, 500); //C7
delay(500);
//wait until a button is pressed
do {
pressedButton = restartCheck();
} while (pressedButton > 3);
delay(100);
gameStarted = false;
}
//implemented if user wins all the round
void winSequence() {
delay(100);
setColour(255,0);
delay(1000);
setColour(255,255);
smiley();
//Victory Sequence plays
tone(buzzerPin, 1318, 150); //E6
delay(175);
tone(buzzerPin, 1567, 150); //G6
delay(175);
tone(buzzerPin, 2637, 150); //E7
delay(175);
tone(buzzerPin, 2093, 150); //C7
delay(175);
tone(buzzerPin, 2349, 150); //D7
delay(175);
tone(buzzerPin, 3135, 500); //G7
delay(500);
//wait until a button is pressed
do {
pressedButton = restartCheck();
} while (pressedButton > 3);
delay(100);
gameStarted = false; //resets the game so the start sequence will play again
}
void loop() {
typedef void (*num_func) ();
num_func roundDisplay[] = {smiley, one, two, three, four, five, six, seven, eight, nine};
if (gameStarted == false) {
startSequence(); //starts the start sequence
roundCounter = 0; //resets the round counter
delay(1500); //waits 1 1/2 seconds
gameStarted = true; //sets gameStarted to true so the game doesn't restart
}
roundDisplay[roundCounter+1]();
//Rounds start by flashing generated order
for (int i = 0; i <= roundCounter; i++) { //gotes through the array up to the current round
flashLED (buttonSequence[i]); //turns on the LED for that array
delay(200);
allLEDoff(); //turns all of the LEDS off
delay(200);
}
//then starts going through the sequence one at a time to see if the user pressess the correct button
for (int i = 0; i <= roundCounter; i++) {
startTime = millis(); //starts recoridng the start time
while (true) {
pressedButton = buttonCheck(); //every loop check to see which button is pressed
if (pressedButton < 4) { //if a button is pressed (4 means no button is pressed)
flashLED(pressedButton);
if (pressedButton == buttonSequence [i]) {
delay(250);
allLEDoff ();
break;
}
else {
delay(250);
allLEDoff();
loseSequence();
break;
}
}
else {
allLEDoff();
}
//check to see if the time limit is up
if (millis() - startTime > timeLimit) {
delay(250);
loseSequence();
break;
}
}
}
roundCounter++;
if (roundCounter >= roundsToWin) {
delay(250);
winSequence();
}
delay(2000); //waits for half a second between each round
}