Arduino Memory Game

by 686717 in Circuits > Arduino

975 Views, 1 Favorites, 0 Comments

Arduino Memory Game

38970B11-1359-4C8B-A2E7-435917070504.jpeg

Today I will be demonstrating how to create a memory game using an Arduino Uno, and TinkerCad. In this game, the LED's flash in a certain order, adding one more flash at every level. To beat the levels, you have to click the pushbuttons in the exact order that the LED's flash in. Pressing the wrong button will reset your progress and take you back to stage 1. Since you are now aware of how this game works, let's cover the materials needed to make it.

Supplies

1) 4 different colored LED's (I only had 3 colors so I had to use the red one twice)

2) 4 mini pushbuttons

3) 1 Piezo buzzer

4) 4 10kΩ resistors

5) 4 330Ω resistors

6) Breadboard

7) Arduino Uno

8) Multicolored wires (different colors help organize the circuit and color code)

Wiring the Circuit

6B8F54CC-6E3E-43A5-9728-B8668B00086E.jpeg
AD1F0ABC-949B-442E-9018-91C971C931BA.jpeg
F7D281AE-170F-4FCE-B645-CBA67D44CC7D.jpeg
A9F56574-0F33-4C28-9966-EFB898A380DE.jpeg

Refer to both the TinkerCad image, and the actual circuit

1) Start by putting all the wires required onto the breadboard. Doing this step now is better because there are no components that interfere with the wires. Put the wires that connect to the power rail first, because the other wires are going to be overlapped.

2) Add your pushbuttons, and buzzer in their spots, connecting the top and bottom rails

3) Insert the 10kΩ and 330Ω resistors into their correct places. make sure that you don't mix up the resistors because having too much power going to a component can damage it, and your breadboard.

4) Lastly, add your LEDs onto the breadboard. You want to use 4 different colored LED's but I only had 3 colors so I had to use the red one twice

5) Double check your entire circuit with the TinkerCad one, to make sure everything is right, and tweak any errors. Note that if you wish to change the circuit, the code will also change.

Coding the CIrcuit

667C1E0E-A78A-428A-B65F-96282967ED9C.png

The code for this circuit makes it so the circuit will put out a pattern that you have to imitate with the pushbuttons. If you put in the wrong pattern, the code will tell the buzzer to make a "you lose" noise. If you are using different pins on the Arduino Uno, then you are going to have to specify those pins in the code. I have added a link to the Arduino sketch, but that link may not work for everyone so the entire code is pasted below

Arduino Code

const int button1 = 2;

const int button2 = 3; const int button3 = 4; const int button4 = 5; const int led1 = 7; const int led2 = 8; const int led3 = 9; const int led4 = 10; const int buzzer = 12;

int game_on = 0; int wait = 0; int currentlevel = 1; long rand_num = 0; int rando = 0; int butwait = 500; int ledtime = 500; int n_levels = 10; int pinandtone = 0; int right = 0; int speedfactor = 5; int leddelay = 200;

const int tones[] = {261, 349, 392, 440, 2700};

int buttonState[] = {0,0,0,0}; int lastButtonState[] = {0,0,0,0}; int buttonPushCounter[] = {0,0,0,0};

void playTone(int tone, int duration) { for (long i = 0; i < duration * 1000L; i += tone * 2) { digitalWrite(buzzer, HIGH); delayMicroseconds(tone); digitalWrite(buzzer, LOW); delayMicroseconds(tone); } }

void setup() { randomSeed(analogRead(0)); pinMode(button1, INPUT); pinMode(button2, INPUT); pinMode(button3, INPUT); pinMode(button4, INPUT); pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); pinMode(led3, OUTPUT); pinMode(led4, OUTPUT); pinMode(buzzer, OUTPUT);

}

void loop() { int n_array[n_levels]; int u_array[n_levels];

int i;

if (game_on == 0){ for(i=0; i50 && rand_num<=100) rando=1; else if (rand_num>100 && rand_num<=150) rando=2; else if (rand_num<=200) rando=3; n_array[i]=rando; } game_on = 1;

}

if (wait == 0){ delay (200); i = 0; for (i = 0; i < currentlevel; i= i + 1){ leddelay = ledtime/(1+(speedfactor/n_levels)*(currentlevel - 1)); pinandtone = n_array[i]; digitalWrite(pinandtone+7, HIGH); playTone(tones[pinandtone], leddelay); digitalWrite(pinandtone+7, LOW); delay(100/speedfactor); } wait = 1; } i = 0; int buttonchange = 0; int j = 0; while (j < currentlevel){ while (buttonchange == 0){ for (i = 0; i < 4; i = i + 1){ buttonState[i] = digitalRead(i+2); buttonchange = buttonchange + buttonState[i]; } } for (i = 0; i < 4; i = i + 1){ if (buttonState[i] == HIGH) { digitalWrite(i+7, HIGH); playTone(tones[i], ledtime); digitalWrite(i+7, LOW); wait = 0; u_array[j]=i; buttonState[i] = LOW; buttonchange = 0; } } if (u_array[j] == n_array[j]){ j++; right = 1; } else{ right = 0; i = 4; j = currentlevel; wait = 0; } }

if (right == 0){ delay(300); i = 0; game_on = 0; currentlevel = 1; for (i = 0; i < 4; i = i + 1){ digitalWrite(i+7, HIGH); } playTone(tones[4], ledtime); for (i = 0; i < 4; i = i + 1){ digitalWrite(i+7, LOW); } delay (200); for (i = 0; i < 4; i = i + 1){ digitalWrite(i+7, HIGH); } playTone(tones[4], ledtime); for (i = 0; i < 4; i = i + 1){ digitalWrite(i+7, LOW); } delay(500); game_on = 0; }

if (right == 1){ currentlevel++; wait = 0; } if (currentlevel == n_levels){ delay(500); int notes[] = {2, 2, 2, 2, 0, 1, 2, 1, 2}; int note = 0; int tempo[] = {200, 200, 200, 400, 400, 400, 200, 200, 600}; int breaks[] = {100, 100, 100, 200, 200, 200, 300, 100, 200}; for (i = 0; i < 9; i = i + 1){ note = notes[i]; digitalWrite(note+7, HIGH); playTone(tones[note], tempo[i]); digitalWrite(note+7, LOW); delay(breaks[i]); }

game_on = 0; currentlevel = 1; n_levels = n_levels + 2; speedfactor = speedfactor + 1; } }

Review

Arduino Memory Game Tutorial

Now that you've finished uploading the code, and have a finished circuit, it's time to play with it! To start the game, press one of the pushbuttons, and then just follow the pattern that is displayed. Although a basic circuit, this game is a challenge to make on your own and can be easily modified to make it harder or easier.