Arduino Memory Game
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?
The Circuit
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.
Parts List
Buzzer
Active/Internally Driven
D1
Red LED
D2
Yellow LED
D3
Green LED
D4
Blue LED
R1
100 Ω
R2
100 Ω
R3
100 Ω
R4
100 Ω
R5
10 kΩ
R6
10 kΩ
R7
10 kΩ
R8
10 kΩ
SW1
Pushbutton
SW2
Pushbutton
SW3
Pushbutton
SW4
Pushbutton
U1
Arduino UNO
The Layout
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;
}
}