Arduino Memory Game
This circuit is live in Circuit Canvas. Open it to explore every part and wire, or make your own copy to change values, swap parts and build your own version. Free, right in your browser.
This is a simple Memory Game game built with Arduino. The point of the game is to repeat the sequence of colors that the Arduino comes up with. The sequence gets longer for each level. How many levels can you do?
Arduino Memory Game Schematic
Pin 3 controls the buzzer. You need an active buzzer for this, but you can tweak the code to work with a passive buzzer if you prefer. Pins 4-7 controls the LEDs. Pins 8-11 is for the input buttons that you use to "push" the colors in the correct sequence.
Arduino Memory Game Parts List
How to Build the Arduino Memory Game
Here's the code for the game:
// Button Definitions
int redButton = 3;
int yellowButton = 4;
int greenButton = 5;
int blueButton = 6;
// LED Definitions
int redLED = 8;
int yellowLED = 9;
int greenLED = 10;
int blueLED = 11;
// Buzzer Definition
int buzzer = 12;
// Global Variables
int pressedButton[4];
int starterPinLed = 8;
int starterPinButton = 3;
// Function to play sound on buzzer
void play() {
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(200);
}
// Function to turn on an LED
void writeHigh(int led) {
switch (led) {
case 0: digitalWrite(redLED, HIGH); break;
case 1: digitalWrite(yellowLED, HIGH); break;
case 2: digitalWrite(greenLED, HIGH); break;
case 3: digitalWrite(blueLED, HIGH); break;
default: break;
}
}
// Function to turn off an LED
void writeLow(int led) {
switch (led) {
case 0: digitalWrite(redLED, LOW); break;
case 1: digitalWrite(yellowLED, LOW); break;
case 2: digitalWrite(greenLED, LOW); break;
case 3: digitalWrite(blueLED, LOW); break;
default: break;
}
}
void setup() {
pinMode(redButton, INPUT);
pinMode(yellowButton, INPUT);
pinMode(greenButton, INPUT);
pinMode(blueButton, INPUT);
pinMode(redLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(blueLED, OUTPUT);
pinMode(buzzer, OUTPUT);
}
int continueGame = 0;
int showSequence = 1;
int currentLevel = 1;
const int levels = 20;
int win = 0;
int randomStates[levels];
int guessedStates[levels];
void loop() {
int i;
int random_number;
if (continueGame == 0) {
for(i = 0; i < levels; i++) {
guessedStates[i] = 0;
random_number = random(0, 4);
randomStates[i] = random_number;
}
continueGame = 1;
}
if (showSequence == 1) {
delay(200);
for (i = 0; i < currentLevel; i++) {
digitalWrite(randomStates[i] + starterPinLed, HIGH);
play();
digitalWrite(randomStates[i] + starterPinLed, LOW);
delay(200);
}
showSequence = 0;
}
i = 0;
int change = 0;
int currentPosition = 0;
while (currentPosition < currentLevel) {
while (change == 0) {
for (i = 0; i < 4; i++) {
pressedButton[i] = digitalRead(i + starterPinButton);
change += pressedButton[i];
}
}
for (i = 0; i < 4; i++) {
if (pressedButton[i] == HIGH) {
digitalWrite(i + starterPinLed, HIGH);
play();
digitalWrite(i + starterPinLed, LOW);
showSequence = 1;
guessedStates[currentPosition] = i;
pressedButton[i] = LOW;
change = 0;
}
}
if (guessedStates[currentPosition] == randomStates[currentPosition]) {
currentPosition++;
win = 1;
} else {
win = 0;
showSequence = 1;
break;
}
}
if (win == 0) {
delay(300);
continueGame = 0;
currentLevel = 1;
digitalWrite(buzzer, HIGH);
delay(300);
for(int i = 0; i < 4; i++) {
writeHigh(i);
play();
delay(15);
}
for(int i = 3; i >= 0; i--) {
writeLow(i);
play();
delay(15);
}
digitalWrite(buzzer, HIGH);
delay(500);
digitalWrite(buzzer, LOW);
delay(500);
}
if (win == 1) {
currentLevel++;
showSequence = 1;
}
}
Make it your own
Every circuit here is a live project in Circuit Canvas, not just a picture. Copy this one and change a value, swap a part or move it to a stripboard: the schematic and the breadboard layout are linked, so a part added to one appears in the other.
Or start from a blank canvas and draw your own circuit, with thousands of ready-made parts.