Whack-a-mole Project

 

Overview

Once the player presses the start button, a random LED illuminates and the player has limited time to press the correct colored button. Only if the correct colored button is pressed in time one point is added to the player’s score. The game stops when the player reaches the defined limit.

intermediate

$25 - $30

3 hours

1-3 Clients

 


Things used in this project

Hardware Components

Picture

Name

Quantity

Price (7/17/20)

Link

Picture

Name

Quantity

Price (7/17/20)

Link

I2C LCD Display

1

$4.00/Screen (when purchased in a pack of 2)

Included in Starter Kit

Or you can purchase it here

Simon Says Project or DIY Kit

1

~$20 Completed

Link to Simon Says Project Page and Kit Page

Male to Male and Male to Female jumper wires

~20

$.06/Cable (when purchased in a pack of 120)

Included in Starter Kit

Or you can purchase it here

Q-Client Builder Base

3

$49

Q-Client

Hand tools and fabrication machines

Picture

Name

Quantity

Link

Picture

Name

Quantity

Link

Small slotted-head screwdriver

1

Included in Starter Kit

Or you can pick from one on our Recommended Tools List


Story

We intended to build the Whack-A-Mole project to show not only that you can make a fun game with the Quantum platform, but also to show that many of our hardware projects can be reused and converted to a different use by writing new programs to be used with them.

Video

Process

Step 1: Building the Hardware

Gather all of the materials listed in the parts list above and we’ll start by connecting the LCD to the Builder Base.

On the back of your LCD you should have four pins labeled SDA,SCL,VCC, and GND.

You should use MF jumper wires to attach the LCD to the Builder base

You should attach the LCD to the Builder Base as follows:

LCD

Builder Base

LCD

Builder Base

SCL

SCL

SDA

SDA

VCC

5V

GND

GND

 

 

The other hardware component that needs to be assembled is the Simon Says Project or the DIY Kit.

If you don’t already have one of those circuits assembled, follow the steps listed on their pages.

This circuit will serve as your game board for the Whack-a-Mole project.

Once you have both circuits completed you can move onto building the Firmware.

Step 2: Build the Firmware

Remember: All Firmware and Application files are available in the resources section below!

First we will start by building the firmware file for the LCD screen.

Create a new firmware file and name it “Whack_a_mole_LCD”.

Now, by using the “+Add Hardware” button, add an LCD to the firmware file.

 

 

Now, select the “Generic LCD2004 Display (I2C) from the driver dropdown menu and configure the driver as follows:

 

 

The I2C address of your LCD might be different that ours, so when your LCD is connected to the Builder Base we suggest that you perform an I2C scan via the Debug menu on our clients page.

That’s it! Save your firmware file.

Next, depending on which controller circuit you use, you should build the firmware file for either the Simon Says Project or the DIY Kit.

Step 3: Build the Application

Remember: All Firmware and Application files are available in the resources section below!

We will use this section to walk you through the basic game logic involved in the Whack-a-mole game. You can either download the application from the resources section below, or you can follow along and build the game for yourself!

Here we have split the application up into for main components. 1. Main Game sequence, 2. Input, 3.Score/Win Calculation, 4. Output

These two Input Number objects set the time a player as to react, and the number of points the player needs before the game is won. These values are set through the Dashboard.

 

When the player hits the “Start” hardware button object a number of objects are triggered. First, the “Start” interface object is triggered. We have hardware and interface button objects here so that the game can be played from both the dashboard and the game controller.

From the Start button the Digital Toggle object is then triggered, the toggle object then triggers the “And” object, which consequently triggers the “Sequence” object.

The data sequence then triggers the RNG object, which will output a random number in the range [1,4]. The sequence will then trigger a 1s timeout. During this timeout the random number is sent to the “Color Enum Out” Custom snippet. This custom snipper takes in the range [0,4], and sends a trigger out of one port depending on the number received.

Number

Port

Number

Port

0

Reset

1

Blue

2

Red

3

Yellow

4

Green

Here’s the code for the object:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 switch(inPorts[0].value){ case 0:{ return [false,false,false,false, true]; }; break; case 1:{ return [true,false,false,false, false]; }; break; case 2:{ return [false,true,false,false, false]; }; break; case 3:{ return [false,false,true,false, false]; }; break; case 4:{ return [false,false,false,true, false]; }; break; default: break; }

Returning back to the original sequence, the RNG object also sends the number to the “Value 1” port of the Value Compare object.

At this point the timeout block has finished executing its 1000ms timeout and sends a trigger to the “send zero” custom snippet, and it also triggers the Value Compare block.

The Value compare Block evaluates the value that the RNG block produces against the color(number) that the player inputs.

When we send a zero to the custom “Color Enum Out” snippet a trigger is sent out of its Reset port. This resets the game sequence and causes another color to be selected. The Sequence will either be reset by the timeout, or if the player selects the right color before the timeout executes.

We will now look at the the Input portion of the program.

We have a hardware and an interface button object for each color, this allows us to play the game from both the controller and the dashboard. These button pairs each feed into their corresponding ports on the “Color Enum In” custom snippet. This snippet converts the the port the signal is received from the port's corresponding number.

In Port:

Out num

In Port:

Out num

Blue

1

Red

2

Yellow

3

Green

4

This value is then passed to the Value Compare object that we discussed earlier. If the value that the player inputs is equal to the value that the RNG object generates, the game sequence is then reset. If the colors are not equal then nothing is triggered.

We will now look at how the score is calculated and how a winner is determined.

When the Game starts, the Data counter is set to 0, and the Max of the Data Counter is by default set to 10, but that can be changed through the dashboard.

If the RNG value and the value that the player input are equal, the Increment port on the data counter is triggered. This Value is then passed to the String Merge Block. The String Merge Block merges the strings “Score:” and “(Value from Counter)”.

The value from the counter is also passed to the “Your Score” Text block which displays your score on the dashboard, and to the Any Data Sequence object, which sends the same string to the LCD.

The counter also passes this value to the value compare block. This compare block evaluates your current score against the set winning score. By default the winning score is set to 10, but it can be changed through the dashboard.

If the two values are equal, the “Win” static string block sends the message “!WIN! !WIN!” to the “Your Score” text block, and to the Any Data Sequence block, which sends the same string, “!WIN! !WIN!”, to the LCD.

When a winning score is met, the value compare block also resets the entire game so that it can be played again.

That’s it for programming the App, save it and return to your applications page!

Step 4: Mapping the Application

You should now be back on the Apps page.

Find your “Whack a mole” app and hit the play button.

A list containing all of the devices in your application will expand.

Next click on the “Red” device and the client dropdown menu will appear on the right.

Select the Red driver from the dropdown menu and hit “Done”.

Repeat the same steps for the remaining devices. Ensure that you map the buttons and LEDs to their appropriate colors. Meaning Red → Red and Blue → Blue etc.

Notice how the status symbols have changed to green checkmarks.

Note: When mapping the Start button, you will map it to the driver named reset.

You are now done mapping your application!

Step 5: Run the Application!

Next, hit “Save + Run”

 

Congratulations! Your Whack a Mole project is now complete, have fun!

Schematics

 

 


Code

App

Firmware


Gallery

 

 

 

 


Resources

App

 

Firmware

 

Schematic

 

Fritzing