Wack-A-Mole

by 777765 in Circuits > Arduino

33 Views, 1 Favorites, 0 Comments

Wack-A-Mole

tinker_adrino.PNG

Adriano project replicating the game wake-a-mole. Using the Arduino or known as a microcontroller. It will simulate the game through its code to light up LEDs representing the moles and will only be turned off by the corresponding push buttons. The score will be displayed via a serial monitor by the Arduino code. The project itself uses the use of digital inputs and outputs, code of random numbers and the interaction of hardware. This project of software and hardware is a simple and entertaining game to introduce audio projects

Supplies

stuff_png.PNG

Arduino Uno R3(1)

Pushbuttons(4)

LED's(4)

10k ohm resistors(4) Color top to bottom (orange,black,brown)

330 ohm resistors(4) Color top to bottom (brown, orange, orange)

wire(any color) 8 (4 longer, 4 shortor strips of wire)

Red wire(6)

Black wire(2)

Breadboard(1)

Coding Part 1(creating Value, Assigning Pins and Naming LEDS and Buttons)

code base Creddit (Make A Whack-A-Mole Game! https://www.youtube.com/watch?v=DMTDYd6rwAo)

Will begin with the coding and software part of the project. Which will be the easier and first part of coding. Naming the variables and pins of the project.

Int score = 0;

The first line of code is Int score = 0;. Int is short for integer and its purpose is to carry number data. Its second word “score” is the name it will be given to call upon. And lastly the number “0” will be the beginning value

Const int blueLED = 3;

Const int greenLED = 4;

Const int yellowLED =5;

Const int redLED =2;

Const int redButton =9;

Const int blueButton =8;

Const int greenButton =7;

Const int yellowButton =6;

The code present has 2 sections. The first section being the LEDs or the moles. And the second the buttons. Const int means assigning an integer that cannot be changed, words after are names to call upon later on. Lastly the numbers are the pins in the Arduino that would carry that information. “Int score” was just creating our own value.

This finishes the first part meaning the begging should look like.

Int score = 0;

Const int blueLED = 3;

Const int greenLED = 4;

Const int yellowLED =5;

Const int redLED =2;

Const int redButton =9;

Const int blueButton =8;

Const int greenButton =7;

Const int yellowButton =6;

Coding Part 2(setup Code)

The second stage of coding will be set up. Meaning the start of this code would be “void setup()”

And below that would be “Serial.begin(9600)”

void setup()

Serial.begin(9600)

Serial in of itself is a built in library and handles serial communication. “Begin” means terminating communication on, in a sense activating the serial monitor. And the value 9600 is just how fast the information would be received and displaced.

pinMode(redLED, OUTPUT);

pinMode(blueLED, OUTPUT);

pinMode(greenLED, OUTPUT);

pinMode(yellowLED, OUTPUT);

pinMode(redButton, INPUT);

pinMode(blueButton, INPUT);

pinMode(greenButton, INPUT);

pinMode(yellowButton, INPUT);


Just like before there are 2 seconds of code to separate LEDs and buttons.” Pinmode” assigns the arduino pin function is either going to be input or output. Either giving information or displaying information. Next is the name, using the names of the functions we named earlier in part 1. And lastly. The inputs and outputs are connected to the pinmode function which means assigning the function. Buttons are inputs while LEDs are output.

This section of code would look like this.

void setup()

{

Serial.begin(9600);


//Set LED pins as outputs (only displays light)

pinMode(redLED, OUTPUT);

pinMode(blueLED, OUTPUT);

pinMode(greenLED, OUTPUT);

pinMode(yellowLED, OUTPUT);


//Set Buttons as inputs (controls if you whacked the "mole")

pinMode(redButton, INPUT);

pinMode(blueButton, INPUT);

pinMode(greenButton, INPUT);

pinMode(yellowButton, INPUT);


Coding Part 3(loop Code)

This would be the last and most important and complex part of the code. Starting the code would be “void loop()”

for(int i = 0; i < 10; i++)

I will be assigned the value of 0. The code below this code, when triggered 10 times will then activate and loop this code again. For how the code works. “I” will start with a value of 0m and every time “i” isnt greater than 10 it will add until it is and then set back to 0 to restart. This code will act as the “rounds” of the game.

digitalWrite(random(2, 6), HIGH);

This will make the LED’s power at random. “DigitalWrite” will recall the Arduino to either give the specific pin power or ground. Meaning turning it on or off. “Random will call upon the range of PINs given which is (2,6) or pins between 2 to 6. And lastly the value high will tell the Arduino to give pins 2 to 6 power on Random.



while(digitalRead(redLED) == HIGH || digitalRead(blueLED) == HIGH || digitalRead(greenLED) == HIGH ||digitalRead(yellowLED) == HIGH)

{


if (digitalRead(redLED) == HIGH)

{


if (digitalRead(redButton) == HIGH) {


digitalWrite(redLED, LOW);

This is the boolean expression. The top part of the code will check if any LED’s are on, which are mostly are. After checking if any LED’s are on. The code then arrows it down to check each individual LED. This code will check if red is on. And if it's on it will then check if the Button of red is been pushed. And if all conditions are met. The led will turn off. Meaning these 3 boolean expressions will check all 4 LED's on which they will be due to the random power output from pins 2 to 6. And then the next check will be which button and then lastly which button will be pressed. Which will then close the corresponding LED. As digital write will assign the voltage for the LED to be low. Turning it off.





Note:

if (digitalRead(redLED) == HIGH)

{


if (digitalRead(redButton) == HIGH) {


digitalWrite(redLED, LOW);

This will be coded the same except for the change to check the individual LED’s and their buttons like blue,green, and yellow.



Lastly the code indicates that a round is completed.

for (int i = 0; i < 1; i++)

This code will be the score value and will add every time a round has been completed.

digitalWrite(redLED, HIGH);

digitalWrite(blueLED, HIGH);

digitalWrite(greenLED, HIGH);

digitalWrite(yellowLED, HIGH);

delay(250); // Wait for 250 millisecond(s)

digitalWrite(redLED, LOW);

digitalWrite(blueLED, LOW);

digitalWrite(greenLED, LOW);

digitalWrite(yellowLED, LOW);

delay(250); // Wait for 250 millisecond(s)


score++;

Serial.print("Score: ");

Serial.println(score);

The first 2 seconds of code are just the visual representation of winning. Meaning when the round is one all LED’s will flicker indicating that a round is won. And the last print of code. “Score++;” will add a point. Serical.print(“score;”); will be the value displayed. And “ Serial.PrintIn(score);” will carry the value of the score itself. Keeping track of the amount and update after each round.

the Loop code should look like this in full


void loop()

{

delay(500);

for(int i = 0; i < 10; i++)

{

digitalWrite(random(2, 6), HIGH);

while(digitalRead(redLED) == HIGH || digitalRead(blueLED) == HIGH || digitalRead(greenLED) == HIGH ||digitalRead(yellowLED) == HIGH)

{

if (digitalRead(redLED) == HIGH)

{

if (digitalRead(redButton) == HIGH)

{

digitalWrite(redLED, LOW);

}

}


if (digitalRead(blueLED) == HIGH)

{

if(digitalRead(blueButton) == HIGH)

{

digitalWrite(blueLED, LOW);

}

}


if (digitalRead(greenLED) == HIGH)

{

if(digitalRead(greenButton) == HIGH)

{

digitalWrite(greenLED, LOW);

}

}


if (digitalRead(yellowLED) == HIGH)

{

if(digitalRead(yellowButton) == HIGH)

{

digitalWrite(yellowLED, LOW);

}

}

delay(100);

}

}

for (int i = 0; i < 1; i++)

{

digitalWrite(redLED, HIGH);

digitalWrite(blueLED, HIGH);

digitalWrite(greenLED, HIGH);

digitalWrite(yellowLED, HIGH);


delay(250); // Wait for 250 millisecond(s)


digitalWrite(redLED, LOW);

digitalWrite(blueLED, LOW);

digitalWrite(greenLED, LOW);

digitalWrite(yellowLED, LOW);


delay(250); // Wait for 250 millisecond(s)

score++;

Serial.print("Score: ");

Serial.println(score);

//the lights will flicker when its 10+

}

}

Buttons

adrino_2.PNG
tinker_7.PNG

Place buttons within the area where the pins have a gap, vertically upward. The buttons must be able to reach securely into the pins above and below. The buttons must be spaced from each other for the future placement of LED’s and must be separated by a space of 3 pins. Enough so the led doesn't interfere with the connections to the buttons itself.


Button Resistors

adrino_3.PNG
tinker_6.PNG

Resistor recognition:

Buttons require the 10k resistors. You can recognize the value or amount of resistance a resistor holds by the 3 strips of color. A 10k resistor has the colors of Orange,Black,Brown in this exact combination.

Placement:

Before placing, the breadboard has a horizontal rail of pins that represent either ground or power. Place the resistor so that the metal ends enter the blue outlined rails. Before placing the ending metal end inline with one of the buttons legs, being the buttons bottom right. Make sure it lines up and connects securely to ground. Do this with all buttons.


Buttons Power

adrino_4.PNG
tinker_5.PNG

For the other leg of the buttons. The respective bottom left. Do the same method of adding a resistor but instead with a very short red wire. Instead of connecting to ground, you will connect the end of the wire to the horizontal area with a red strip that would indicate the rail that is powered by power. Make sure you connect your wire to align vertically upward towards the right leg of the button.


LED

adrino_5.PNG
tinker_4.PNG

Now for the LEDs of the project. LEDs can be color coded if desired and it won't affect the code or circuit. As for the placement. The LEDs should be placed above the buttons and in between the spaces of 3 or more between the buttons. Before placing the LEDs, know that the longer side is the positive for the LED. Make sure all LEDs positive prongs are facing right. Making the negative face left.


LED Resistor

adrino_6.PNG
tinker_3.PNG

Resistor recognition:

Placing the next set of resistors. The resistors now are 330 ohm resistors. Meaning they have a different set of 3 stripes. The set being Brown followed by 2 more stripes of orange in that order.

Placing the resistors follows the same rule as the 10k resistors. Meaning they connect to the ground power rail on the top of the breadboard, where blue marks the ground. But make sure the resistor leads into the negative terminal of the LED, meaning the most left as we stated before we needed to place all the positive terminals facing right.


LED Power

adrino_7.PNG
tinker_2.PNG

Take your shorter strips of wire, and place them in the right terminal that the LED occupies. From here the wire should go straight back into the arduino. Follow the pins you coded in the Adorno code. Starting from the most left LED it connects to pin 2 of the Arduino, for the second LED pin 3. So on and so forth until the last LED at pin 5. It's preferable to align your arduino horizontal to the top of the breadboard.

Also had the 2 power and ground wires. Meaning connect the red wire to the bottom power rail to the top power rail of the arduino. Meaning when we powered the board, it will carry the connection over to the top of the arduino. Do this repeatedly with the black wire for ground.


Note:Step 5 and 6, make sure the wires of step 5 and 6 that connect buttons and LEDs to the Arduino are connected correctly, and also are securely connected to avoid jumper wires where the wires could jump up either from the Arduino or breadboard.


Input Buttons

adrino_8.PNG
tinker_1.PNG

Lastly, take the longer strips of wires. The placement should be above the resistor connecting to the button. From the left most button. Place the wire above the resistor and then connect the ending wire tip to pin number 9 of the arduino. Do this for every button from left to right making the pin order 9 to 6. Covering about 4 pins meaning a pin for each button.

Lastly use the last red and black wire which should be the longer wires of red and black. Follow the tinkercad diagram and place the black wire to ground and lead it to the top ground pin, do the same for the red wire. Connect to the red outlined power rail and lead it back to the power in the top power pin in the Arduino.

Note:power pins are the pins marked with 5V (5 volts).


LDC(12c) Add On

Screenshot 2026-06-16 151818.png

Adding a LCD (12c) would provide a visual score update attached to the circuit instead of the serial monitor, think of it as a screen. If you'd like to add an LCD(12).

Attachment:

How you would attach an LCD would be like any component, the component needs power, and needs to be connected to ground. Following the image you can see that. What's special about an LCD is its direct connection to the arduino board, Its other 2 ports, SDA and SCL which acts as the information passing through the arduino into the display. Being serial data and clocks.

Code:

Before coding, variables need to be established and the LCD library needs to be known to the arduino board in order to make use of the LCD.

#include <Adafruit_LiquidCrystal.h>

int point_clock = 0;

Adafruit_LiquidCrystal lcd_1(0);

The first line of code calls a curtain library in order for the use and display information on the lCD display. "int point_clock=0;" is the starting value for the point system for the wack-a-mole game and a function to be called upon. "Adafruit_LiquidCrystal lcd_1(0); " this assigns the library under the code.

Setup code:

lcd_1.begin(16,1);//This code gives the placement on what will be written as the LCD is in 2 rows

lcd_1.print("score:"); //the word "Score" will be written in the position that Lcd_1.begin(16,1) stated

Loop code:

lcd_1.setCursor(6,2);//This code acts the same as lcd_1.begin as it sets the placement for what will be written

lcd_1.print(point_clock);Point_clock is a function that will be printed and updated, and will be placed (6,2) as stated before

delay(500);//delay by 0.5 for smooth transition.




Full Code(+LDC)

#include <Adafruit_LiquidCrystal.h>

int point_clock = 0;

int button = 13;


int score = 0;


Adafruit_LiquidCrystal lcd_1(0);


const int blueLED = 3;

const int greenLED = 4;

const int yellowLED = 5;

const int redLED = 2;


const int redButton = 9;

const int blueButton = 8;

const int greenButton = 7;

const int yellowButton = 6;



void setup()

{

lcd_1.begin(16,1);

lcd_1.print("score:"); //dispaly screen

Serial.begin(9600);

//Set LED pins as outputs (only displays light)

pinMode(redLED, OUTPUT);

pinMode(blueLED, OUTPUT);

pinMode(greenLED, OUTPUT);

pinMode(yellowLED, OUTPUT);

//Set Buttons as inputs (controls if you whacked the "mole")

pinMode(redButton, INPUT);

pinMode(blueButton, INPUT);

pinMode(greenButton, INPUT);

pinMode(yellowButton, INPUT);


}


void loop()

{

lcd_1.setCursor(6,2);

lcd_1.print(point_clock);

delay(500);

for(int i = 0; i < 10; i++)//code will loop if the boolean is triggered 9 times)

{

digitalWrite(random(2, 6), HIGH);

while(digitalRead(redLED) == HIGH || digitalRead(blueLED) == HIGH || digitalRead(greenLED) == HIGH ||digitalRead(yellowLED) == HIGH)

{

if (digitalRead(redLED) == HIGH)

{

if (digitalRead(redButton) == HIGH)//the top boolean is trigger which it always is, will reach the botton boolean, and then the next if will check the button. and if all condsitons are met, it turns off the light.

{

digitalWrite(redLED, LOW);

}

}


if (digitalRead(blueLED) == HIGH)

{

if(digitalRead(blueButton) == HIGH)

{

digitalWrite(blueLED, LOW);

}

}


if (digitalRead(greenLED) == HIGH)

{

if(digitalRead(greenButton) == HIGH)

{

digitalWrite(greenLED, LOW);

}

}


if (digitalRead(yellowLED) == HIGH)

{

if(digitalRead(yellowButton) == HIGH)

{

digitalWrite(yellowLED, LOW);

}

}

delay(100);

}

}

for (int i = 0; i < 1; i++)

{

digitalWrite(redLED, HIGH);

digitalWrite(blueLED, HIGH);

digitalWrite(greenLED, HIGH);

digitalWrite(yellowLED, HIGH);


delay(250); // Wait for 250 millisecond(s)


digitalWrite(redLED, LOW);

digitalWrite(blueLED, LOW);

digitalWrite(greenLED, LOW);

digitalWrite(yellowLED, LOW);


delay(250); // Wait for 250 millisecond(s)

point_clock += 1;//will add one point to the counter

score++;

Serial.print("Score: ");

Serial.println(score);

//the lights will flicker when its 10+

}

}