Whack-A-Mole LED Game!

by scootjliu in Circuits > Arduino

27 Views, 0 Favorites, 0 Comments

Whack-A-Mole LED Game!

thumbnail_IMG_2115.jpg
thumbnail_IMG_2116.jpg

Today I will teach you how to make a Whack-A-Mole game using LEDs and pushbuttons with arduino. The purpose of this game is that one of four LEDs randomly light up briefly and the player quickly has to push a button associated with that LED before it turns off. If successful, the score will increase by one, if not the score will reset to zero. Lets begin!

Supplies

Screenshot 2025-06-13 140428.png

Arduino UNO (x1)

Solderless Breadboard (x1)

5mm LED (x4)

Push button (x4)

330 Ohm Resistor (x6)

10k Ohm Resistor (x4)

Seven Segment Display (x1)

Piezo Buzzer (x1)

Wires

Wiring

Screenshot 2025-06-13 142507.png
thumbnail_IMG_2116.jpg

Here is a picture and diagram on Tinkercad as to how the wiring should look.

Connecting the power source

Since each component requires power and ground make sure to connect the breadboard to the 5V and GND.

Wiring the LEDs and push buttons

Each LED and button are assigned to their own pin. You can use the Tinkercad diagram above for reference. Pins [13, 11, 9, 7] are wired to a 330 ohm resistor, which each leads to an LED. Make sure that the resistor is in the middle or near the top of the breadboard to prevent the bottom section (where the LEDs and buttons are located) from getting cluttered with resistors and wires.

Pins [12, 10, 8, 6] are each wired to a 10k ohm resistor, which then leads to the push button. Make sure to use another wire to connect the push button to the negative. Once again make sure the push button and wires are placed in the middle or near the top of the breadboard to avoid the bottom section from being cluttered. Please note that the pins are used in this order to help prevent wires from crossing and overlapping, which makes the circuit look neater and more visually appeasing.

Wiring the seven segment display and piezo buzzer

Unfortunately, since there are not enough pins for all components, the analog pins (A0 - A5) are used as outputs for the seven segment display. [Pins 5, 4, A0, A1, A2, A3, A4] are used for the 7 segment display in this order

Pin 4 - a

Pin 5 - b

Pin A2 - c

Pin A3 - d

Pin A4 - e

Pin A0 - f

Pin A1 - g


There are also two 330 ohm resistors which are wired to the common cathode.

Finally, pin A0 and the GND are wired to the piezo buzzer.

NOTE: In the case you're using a breadboard long (like I did) make sure to connect the positives and negatives of both the right and left side together as they are not connected. This is because there are 2 power rails that are separated in the middle meaning if you don't do this either the display & piezo won't work, or the LEDs won't work.

Next, we can move on to the best part: THE CODE!

Code - Setup

Screenshot 2025-06-13 175128.png

The game consists of 4 LEDs, 4 push buttons, a seven segment display, and a piezo buzzer. A random LED is meant to turn on and off quickly, where the player then needs to push a button to increase their score (which can be seen on the seven segment display). If pressed correctly, the piezo will make a ding and the seven segment display will increase the score by one. If no button is pressed, or the wrong one is pressed, then the score is reset to zero and a low pitched buzz is played in the piezo.

To achieve this, we need to assign each button, LED, piezo, seven segment display, and the score their own variables. After setting up the numbers 1-9 on the seven segment display like this:

void nine () {

digitalWrite (a, HIGH);

digitalWrite (b, HIGH);

digitalWrite (c, HIGH);

digitalWrite (d, LOW);

digitalWrite (e, LOW);

digitalWrite (f, HIGH);

digitalWrite (g, HIGH);

we can begin.

We can use the random function to generate us a number between 1-4 and assign it to a variable. In this case, the variable is seen as randLED. An if statement is then used to assign each number between 1-4 an LED and a pushbutton. After, we set the chosen LED to HIGH, and then use the millis() function to keep track of how much time has passed since the LED has turned on. We store the current time in a variable called startTime.

Then, we create two variables: correctPress and wrongPress, both set to false by default.

A while loop is used to give the player a short window to respond:

while (millis() - startTime < 700)

Since we already set the startTime as a variable right as the LED turned on (below) we can subtract the amount of time elapsed when the LED turned on from the total amount of elapsed time the circuit has been on for.

unsigned long startTime = millis()

This calculates how much time has passed since the LED turned on. For example, if startTime was 5000 and millis() is now 5600, then millis() - startTime is 600 milliseconds. In this case we made it once 700 milliseconds passes, the LED turns off, but by changing this number, you can change the amount of time the LED is on for.

Continuing, inside the loop there are two scenarios:

  1. If the correct button (the one that matches the lit LED) is pressed, correctPress becomes true
  2. If any of the wrong buttons or no buttons are pressed during this time, wrongPress becomes true

After the loop ends, the LED turns off and then we can check which condition was triggered using another if statement. There are 3 conditions:

  1. The correct button was pressed. In this scenario the buzzer plays a ding sound and increase the score variable by one (which in turn changes the number on the seven segment display)
  2. The wrong button was pressed. In this scenario the buzzer will play a wrong sound and reset the score to 0.
  3. No button was pressed. This has the same result as the scenario above.

Before the loop ends, the seven segment display is set to whatever number the score variable is at by using another if statement.

Attached below is the code for the game.

Video!

Here is a video demonstration of the circuit:

Thanks for reading my instructables!

Downloads