Project tutorial

# LED light "Whack-a-mole" © GPL3+

The classic whack-a-mole game using LED lights and a joystick.

• 9,357 views
• 28 respects

## Components and supplies

 Arduino UNO & Genuino UNO
×1
 LEDs
×7
 Resistor 221 ohm
×7
 Analog joystick (Generic)
×1
 Jumper wires (generic)
×8
 Male/Female Jumper Wires
×4

This was my first Arduino project, I built it based off what I learned from the early on tutorials on LED's and basic controls.

This "Whack-a-mole" game uses 7 LED's, and a joystick. There are 4 "moles", represented by the 2nd, 3rd, 4th, and 5th lights from the left on my board. One of the four lights will randomly light up and give a set amount of time to hit the corresponding direction on the joystick. I have arranged my lights so that, from left to right, the selections on the joystick are: left, up, down, right. This is just the convention that I chose, so creative and try other ones!

The two lights on the left are red and green, which indicate a incorrect or correct choice. The yellow light to the far right blinks the count for the current high score, and it indicates the score every time a run is over (whenever a wrong choice is made). Naturally, a new high score replaces the old one.

Perhaps the most complicated part of this project is the analog joystick. For both directions of the joystick, there are max values of 0 and 1024 (for mine, I had to use 1023, so if you aren't getting the selections correct, you can try that). I believe these values can be measured as well using a serial window. I simplified these input values using switch statements for both the x and y inputs. I am not sure if this is the best way to do it, but because I only needed the max values of each direction (i.e, only 4 options, 4 LED moles) I assigned integer values to each possibility: 1, 2, 3, 4 corresponding to Left, Right, Up, Down.

The difficulty levels that I have written into my code as constants, are the time in milliseconds that the person has to make a selection. You can mess around with these times to change the difficulty for your own preference. I thought about integrating a choice using buttons or maybe the joystick at the beginning of a turn, but it wasn't really worth the work as I feel it would be pretty complicated and not really user friendly.

By all means, let me know what modifications and improvements can be made! :)

## Code

##### WhackALEDArduino
```// Whack a LED

// Joystick variables
int sSX;         //state of x, reading from sX
int sSY;         //state of y, reading from sY
int sS;          //converted state (may not be most efficient)

// Game variables
int rNum;        //random int choosing the random light to turn on
int wins=0;      //counting consecutive wins
int highScore=0; //saving the highest score of consecutive wins

// Difficulty constants, time to react
const int easy=1000;
const int medium=500;
const int hard=325;
const int stupid=250;

int ledPins[]={5,2,4,3,6,7,8}; //initializing led's
int pinCount=7;                //number of led pins

void setup() {

Serial.begin(9600);
pinMode(sX, INPUT);
pinMode(sY, INPUT);

for (int thisPin = 0; thisPin < pinCount; thisPin++) { //assigning all the pins as outputs in a for loop
pinMode(ledPins[thisPin], OUTPUT);
}
}

void loop() {

rNum=random(4); //generating random choice
delay(1000);
digitalWrite(ledPins[rNum], HIGH); //lighting the randomly chosen bulb
delay(stupid); //difficulty

//stick stuff
delay(100);

//converting y and x inputs to 4 possibilities. 0 and 1023 are the maximum values on each axis of the joystick, as measured.
sS=0;
switch (sSX) {
case 0:
sS=1;      // Left
break;
case 1023:
sS=2;      // Right
break;
}
switch (sSY) {
case 0:
sS=3;      // Up
break;
case 1023:
sS=4;      // Down
break;
}

digitalWrite(ledPins[rNum], LOW); //turning off the randomly selected bulb, after the joystick choice was made
if (sS-1==rNum) //checking if the user input the correct direction of the lit bulb
{
wins++;
for (int k=0; k<=3; k++) {     //blinking green light indicating correct choice
digitalWrite(ledPins[4], HIGH);
delay(50);
digitalWrite(ledPins[4], LOW);
delay(50);
}
}
else
{
if (wins>highScore) { //if the consecutive wins are more than the previous highscore, the new highscore is set.
highScore=wins;
wins=0;
}
for (int i=0; i<=3; i++) {       //blinking red light indicating incorrect choice
digitalWrite(ledPins[5], HIGH);
delay(50);
digitalWrite(ledPins[5], LOW);
delay(50);
}
for (int w=0; w<highScore; w++) { //displaying via counting and blinking the high score on a yellow bulb.
digitalWrite(ledPins[6], HIGH);
delay(200);
digitalWrite(ledPins[6], LOW);
delay(200);
}
}
}
```

## Schematics

Everything should match the real picture of the project except for the joystick. My joystick has 5 pins, 4 or which I have used: named from top to bottom "GND", "+5V", "VRx", and "VRy". "GND" and "+5V" are connected to GND and 5V on the arduino, "VRx" and "VRy" are connected to A0 and A1.
Everything should match the real picture of the project except for the joystick. My joystick has 5 pins, 4 or which I have used: named from top to bottom "GND", "+5V", "VRx", and "VRy". "GND" and "+5V" are connected to GND and 5V on the arduino, "VRx" and "VRy" are connected to A0 and A1.
whack_a_mole_schematic_CBciL9GiCP.fzz

• 1 project
• 5 followers

April 8, 2018

#### Members who respect this project

and 23 others

See similar projects
you might like

#### Controlling LED light intensity using smartphone light senso

Project tutorial by Nikola Travis

• 2,839 views
• 1 respect

#### Ambient Light Sensor Using Photo Resistor and LED Lights!

Project tutorial by DCamino

• 9,279 views
• 22 respects

#### LED Dice

Project showcase by EvdS

• 28,713 views
• 85 respects

#### LED as a Light Sensor

Project tutorial by WolfxPac

• 12,022 views
• 25 respects

#### Flashing LED Light and Temperature Data Logger

Project tutorial by pipparichter

• 6,018 views
• 9 respects

#### Touch LED Table - Retrogaming And Ambiant Light

Project tutorial by Arbalet Project

• 11,188 views
• 1 comment
• 47 respects