Arduino Memory Game W/ LCD, Difficulties, and Fading Timer

by RJ - 763053 in Circuits > Arduino

19 Views, 0 Favorites, 0 Comments

Arduino Memory Game W/ LCD, Difficulties, and Fading Timer

foto.jpg

This instructable will teach you how to make an Arduino memory game that will have:

An LCD to help the player understand what is happening in the game without being told by the creator

A potentiometer to adjust for difficulties with a special one if the player chooses the maximum difficulty

An LED to fade and help the player gage the amount of time they have before said time runs out, resulting in a loss

Downloads

Supplies

Screenshot 2025-06-16 185810.png

Parts list:

  1. Arduino Uno + Breadboard
  2. 4 LEDs for the game, preferably different colours
  3. 1 extra LED for the timer
  4. 5 330 Ω Resistors
  5. 4 Push Buttons
  6. 4 10K Ω Resistors
  7. 1 Potentiometer
  8. 1 Buzzer + 100 Ω Resistor
  9. A lot of wires

Arduino & Breadboard Setup

Screenshot 2025-06-16 191839.png

Place the Arduino Uno on the left of the breadboard, connect a red wire to 5V and the power rail. Connect a black wire to GND and the ground rail. Finally, add connections at the end of the breadboard to connect the top and bottom power and ground rails.

Wiring Pushbuttons

Screenshot 2025-06-16 192955.png

Place the pushbuttons, 10K Ω resistors, and some power wires as the diagram shows.

Wiring LEDs

Screenshot 2025-06-16 194143.png

Wire the LEDs to connect to ground. Place the 330 Ω resistors to the right of each button so that they connect to each LEDs anode.

Arduino Pin Connections

Screenshot 2025-06-16 202252.png
Screenshot 2025-06-16 202523.png
Screenshot 2025-06-16 202804.png
Screenshot 2025-06-16 203033.png

Connect each button and LED combo to the Arduino's pins. Start at pin 2 and work your way up to pin 9. The diagrams will each show a different step in connecting everything to the Arduino.

Add the LCD

Screenshot 2025-06-16 204412.png

The LCD is quite simple to wire. The GND pin to ground, the VCC pin to power, the SDA to the dedicated SDA pin on the Arduino, and the SCL pin to the dedicated SCL pin on the Arduino.

Wire the Rest of the Components

Screenshot 2025-06-16 210942.png
IMG_1518.jpeg
IMG_1517.jpeg

The other components are quite simple. Wire the potentiometer to A0, take the left leg to power, and right leg to ground. Next, connect the fifth LED's cathode to a 330 Ω resistor, the anode, to pin 10. Finally, the buzzer's negative leg goes to a 100 Ω resistor, while the positive leg goes to pin 11.

Double check with the diagram or alternatively the schematic to make sure your components are all wired properly.

Setting Up the Code

Screenshot 2025-06-16 212240.png

Now that we're done the simple part it's time to get into the meat and potatoes of this project, that being, the code. To first set things up we just need to declare anything we'll be using in the code later. This includes libraries, mapping Arduino pins, and prototyping functions to make moving through your code easier. Alternatively you may download the code from the file given to skip this entire process.

If you do not have the LiquidCrystal_I2C library, you can download it by looking at the top left of the Arduino IDE and click:

  1. Sketch > Include Library > Manage Libraries

Then just search up "LiquidCrystal_I2C" and download the library with the same name.

Void Setup

Screenshot 2025-06-16 212609.png

Now for the void setup we're just initializing pinModes for all LEDs, buttons, and the buzzer. As well as initializing the LCD to be used.

Difficulty Select

Screenshot 2025-06-16 213312.png

This part of the code uses simple LCD countdowns followed by mapping the values of the potentiometer. These mapped values then give us our 20 base difficulties. The highest difficulty will eventually cause the start of extreme mode but we'll leave that function's creation for the end.

Initializing Match

Screenshot 2025-06-16 213801.png

Here three important initializations happen. The maximum additions to the LED pattern (maxRounds), the next LED to be lit in the pattern (ledNext), and the pattern of LEDs itself (pattern[30]). After this the random pattern the player will have to memorize through maxRounds rounds will be generated

Displaying the LED Pattern

Screenshot 2025-06-16 214321.png

This part of the code is used to generate each part of the pattern for the player to know what to memorize and then input back. The LEDs will follow the pattern of the pattern[30] array, only going as far the player is on their round.

Int ButtonChecker() Function

Screenshot 2025-06-16 214925.png

Before we get to the input we need to define our first function. This function will be used to simplify receiving button input to be used back in the void to confirm whether or not a button is actually pressed and whether or not that button was the right button.

Setting Up the Input

Screenshot 2025-06-16 215501.png

Before actually getting into the input we need to declare some variables. Timer and maxTime will be used to decrease the ledTimer's brightness to match the actual time give, which will be seen in the next step.

buttonPressed will be related to the function we just created and this int will be used in the input section to check if the player's inputs were actually correct. It's initialized to 4 as 4 means no input.

correctButton will be used if the timer runs out, later checking if the player's input was correct before the timer ran out (which it shouldn't be since the player didn't have time to input) to move the player to the youLose() function that will be defined later.

Getting Player Input

Screenshot 2025-06-16 220403.png

This loop will let the player actually have time to input, only exiting when either an input is made or the player runs out of time.

When it comes to the ledTimer logic:

  1. timePassed checks how much time has passed since entering the loop in the same way the loop condition does
  2. timerBrightness is mapped so that it is its highest value when the least time has passed and lower when more time has passed
  3. timerBrightness is then analog written to the ledTimer to slowly make the LED fade as the timePassed approaches maxTime

The following if statements first check if the player even has input something, if so, the nested if statement checks if the input is correct. If the input is correct, correctButton is changed to true and the LED will flash just as it does when showing the pattern.

Restart() Function

Screenshot 2025-06-16 221424.png

Before creating the win and lose functions, as well as completing the void loop we need a function that is relied on by the win and lose functions, that being restart(). This function uses similar structure to the buttonChecker() function except it is a Boolean that will return true when ANY button is pressed, so the player can easily restart the game when winning or losing.

Speaking of winning and losing those functions are up next.

YouLose() Function

Screenshot 2025-06-16 221803.png

This function is quite simple, using the LCD to display a message of your defeat, playing a little jingle to auditorily tell the player they lost and then flash the LEDs until the player restarts.

YouWin() Function

Screenshot 2025-06-16 222212.png

Does essentially the same thing as the youLose() function, but for winners instead!

Tells the player they won, plays a little jingle, and prompts the player to restart as the LEDs chase on another.

Finishing Void Loop()

Screenshot 2025-06-16 222958.png

Go back to the end of the void loop where the users input was being shown if it was correct. Add the following code that will call the win and lose function depending on whether the input is wrong, time runs out, or the user just wins.

ExtremeMode() Function

Screenshot 2025-06-16 224204.png
Screenshot 2025-06-16 224346.png
Screenshot 2025-06-16 224653.png
Screenshot 2025-06-16 224746.png

Copy and paste all of void loop past the part where the extremeMode() function is called upon (should be around line 66).

Differences: (Which are shown in the screenshots

  1. Menacing jingle: Meant to show the player auditorily that extreme mode has started
  2. Quicker Gameplay: LEDs are turned on for a shorter amount of time and you're given less time to input to make the mode, well, harder
  3. Higher Pitched LEDs: This works with the increased speed and also gives an increased sense of urgency to players, making them feel how this mode is harder than any other potentiometer difficulty