Whack a mole

Overview

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. 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

3 hours

1-3 Clients


Hardware Components

Picture

Name

Quantity

Link

Picture

Name

Quantity

Link

I2C LCD Display

1

Included in Component Kit

Or you can purchase it here

Simon Says Project or DIY Kit

1

Link to Simon Says Project Page and Kit Page

Male to Male and Male to Female jumper wires

~20

Included in Component Kit

Or you can purchase it here

Q-Client Builder Base

3

Q-Client

Tools Used

Picture

Name

Quantity

Link

Picture

Name

Quantity

Link

Small slotted-head screwdriver

1

Included in Component Kit

Or you can pick from one on our Recommended Tools List


Assemble the Circuit

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.

Yo should attach the LCD to the Builder Base a 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.


Pair the Builder Base

Now we want to pair the Builder Base with our Q-Server. In order to do so, go to the Homescreen of your Q-Server.

Next click on the lift side symbol labeled “Clients”. Switch to the “Unpaired” tab at the top middle of the screen.

You should see your unpaired Builder Base. If not, check if you have plugged in the power supply for the Builder Base. Now move to the three dots below “Actions” and click “Pair”.

Once your Client is paired, click the “Setup” button.

Now you can edit your Client. Give him a Name you want and also a location where you are going to use it. Hit “Save” when you finished.


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.


Build the App

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 library on your Q-Server, 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:

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!


Map the Hardware

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.

You are now done mapping your application!


Run the App!

Next, hit “Save + Run”.

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

Schematics

 

 


Resources

Application

 

Firmware

 

Schematic

Diagram