Learn how to build a Game Controller and program the 14 buttons to mimic keyboard strokes or mouse movements for your favorite games.
Piperbot and Pip need to get back to the ship, but first, they need to find the controller that someone took off their rocket. As they stumble upon an arcade of games, they decide to play a few first and practice with the controllers. Help Piperbot and Pip have some fun before their journey!
Web and Digital Interface Designer: Salary $83,240/yr
Computer User Support Specialist: Salary $71,530/yr
Video Game Designer: Salary $83,240/yr
Hardware Diagram
Code Diagram
Tutorial Steps
Game Controller
Hmm, it looks like we need to connect these Controllers to work with these arcade games. We'll have to make the Controllers work as a keyboard and then as a mouse.
Like me?
Not you, silly! Like the keyboard and mouse on your computer.
Follow the build instructions for your Game Controller.
Not sure which version you have? Take a look at the image below:
Hmm, it seems like we need to connect that Controller PCBA to the Pico. To be safe, let's unplug our Pico while we build.
NOTE: The controller wires may not be the same color as the picture below. Be sure to match the pin labels to the GPIO shown.
Double-check all of your connections. Once you are sure that they are correct, plug your Pico into your computer. Then, click NEXT.
Ooh, I think this controller uses a shift register!
Shift registers read inputs all at the same time, and then shift them out to the Microcontroller:
Then, the LATCH pulse causes the Pico to read all the pressed buttons as 1s and 0s, which are each called a bit. 8 bits is a byte!
Hmm, and then that CLOCK pulse seems to send the bits to the Pico in order until the byte is complete.
Your Controller has 14 buttons, so there are two shift registers to capture all of them.
Click NEXT.
What code would tell the Pico that a Controller is connected? Hmm, maybe that setup controller block I see.
Create the following code in your workspace:
Click NEXT.
I like mouse mode best! How do we get the Controller to act like a mouse instead of a keyboard?
Hmm, I think we can create functions to get our Controller to act like keyboard arrows or like a mouse. Let's create a function for arrow mode!
Create a function called arrow mode. Let's start by getting the LED on your Pico to turn ON when you press the UP button on your Controller and OFF when you release it. Remember to CALL the function by putting it under the start block. Our code will also utilize the repeat while true block because it loops faster than the repeat forever block.
Create the following code:
It seems like the read controller buttons block is the pulse to the shift register! That's the one that gets the Controller to "shift in" all of the button values to the Pico!
Click NEXT.
Hmm, can we ensure that's working before we do anything else?
Click on the DIGITAL VIEW tab at the bottom of the screen to open it. Then, click CONNECT. Choose your Pico device from the pop-up menu, and then click START.
Whoa! GP2, GP3, and GP4 pins are flickering fast! That seems like the Pico asking the Controller for the button press information (GP3 and GP4), and the Controller sending it back (GP2).
When you press the UP button on your Controller, the LED on the Pico should turn on. When you release the button, the LED will turn off.
Is your Controller working? Great!
Mine isn't working. Maybe I should review the wiring and code. Let me click STOP. I think I will try unplugging my Pico, wait 5 seconds, and then plug it back in. Ooh! Or maybe I'll refresh the browser page!
If the Digital View is still open, click its tab to close it. Then, click NEXT.
Is it time for mouse mode yet? I wanna be able to tell when we switch to mouse mode. Do you think we can use that LED to tell us?
Ooh, good idea! Let's flash it once for arrow key mode and twice for mouse mode! We need to create a flash function first.
Use the turn pin 25 (LED) ON and OFF blocks to create a flash LED function like this:
And to make the LED flash once in arrow mode, let's call the flash LED function as the first step in our "arrow mode" function.
Go ahead and delete those two if blocks:
Click NEXT.
Okay, now when we press the UP button on the Controller, we want it to do the same thing as clicking the UP arrow on our keyboard, right?
Grab a check controller block from the Sensing menu and change the first variable to "up button" and the second variable to "keyboard" ⇧ (Up Arrow):
Yikes, now we have to do that for ALL of the buttons on this Controller??
Eep, I think you're right! For the up, down, right, and left arrows, that's FOUR check controller blocks!
Create the following code:
Once you've double-checked all of your code, click NEXT.
Our program is getting really big! The buttons along the right lower side of the workspace look like we can use them to zoom in or out:
Hmm, I usually use the Spacebar, Return/Enter, and Escape buttons to play the game. Maybe I should map those onto buttons 5, 6, and 14.
Add three more check controller blocks onto your arrow mode function:
Once you've double-checked all of your code so far, click NEXT.
Oh man, tell me that's all the buttons. Except for the one to switch to mouse mode, of course! Let's use button 11 for that.
So if 11 is pressed, it means we want to switch modes. Maybe we could create a variable called "mode pressed" that switches based on whether button 11 is pressed.
Let's set the variable as "false" in the main code initially, and then if we press button 11, it sets it to "true".
That's right! If it's true AND we hit button 11 again, it switches to false. Ooh, let's also add an exitloop block underneath that.
Build the code as shown below. Don't forget the blocks right under the start block:
Click NEXT.
Ooh ooh, can we please play now? I have a bunch of games we can play using the arrows before switching to mouse mode.
Flip the switch on the left side of DIGITAL VIEW to turn it off and improve the smoothnes of your Controller's operation.
Use the Controller's left and right buttons to move, and button5 to choose a game. Use the direction buttons (1-4) and buttons 5 and 6 to play the games. To go back and choose a different game, use button 14.
Click the link below to open Pip's Arcade in a new tab:
Whew! I'm ready to add in mouse mode. Let's do it!
Since Pip's Arcade is just a website, you can bookmark it in your browser to go back to it later. When you've finished testing your controller, close the tab with Pip's Arcade and click NEXT.
Hmm, maybe we can use all those if-do statements from arrow mode in our mouse mode. Let's duplicate it!
Right-click and duplicate, and then collapse the to arrow mode function block. Rename the new function "mouse mode".
A mouse can be used to move up, down, right, and left instead of keyboard arrows. We're trying to capture that with mouse mode.
Yep, moving the mouse doesn't have a "release" like the keyboard does, so we can remove those check controller blocks for the up, down, right, and left.
Oh and let's map the mouse left button (click) to button 5 and right button (click) to button 6!
Change the second variable on the check controller blocks for button 5 and button 6 to mouse left button and mouse right button.
Then, add another call to flash LED function to make the light flash twice when in mouse mode.
Finally, add a call to mouse mode right under the call to arrow mode. Create this code:
Click NEXT.
Hmm, so there are three things a mouse can control, correct? Up, down, and how fast we move around.
The Actions menu has a block named mouse move on. I wonder if that's what we need to use?
I bet it is! Let's add it right below the read controller buttons block.
Maybe we should add some blocks that check to see if a Controller button has been pressed? And that would make the mouse move?
Ooh, good call! Grab some was controller button pressed blocks from the Sensing menu and put them into the mouse move on block. Then, match up the mouse directions to the left, right, up, and down buttons on the Controller.
And, it looks like we can make the mouse go faster or slower with the speed menu on that block, too!
That's it! Double-check all of your work, and then click NEXT to try it out.
Click CONNECT and then click START.
Remember that your Controller will start in arrow key mode. Click button 11 to change it. When you do, the LED on the Pico should blink twice, which means you are now in mouse mode. Try it out! Use the direction buttons (left, right, up, down) on your Controller to move the cursor on your screen. Use button 5 to left-click and use button 6 to right-click.
To return to arrow key mode, just press button 11 again. The LED should blink once.
Sweet, we just built a super cool game controller! What are some ways to change it up? Could we add more buttons or add another mode? I want to try out the arcade again with this new mode.
Have an different version? Look for more information on the Support Page.
Troubleshooting Tips
Don’t forget the Duplicate Block feature!
When we code large programs, the Duplicate command is needed.
To access this feature, right-click on the block and select Duplicate.
Have you tested your wiring? (Step 12)
Click DIGITAL VIEW at the bottom of the screen, then click CONNECT. Choose your Pico device from the pop-up menu and click START.
The first thing you might notice when your Controller and Pico are running a program is that the GP2, GP3, and GP4 pins are flickering quite fast—this is the Pico asking the Controller for the button press information (GP3 and GP4) and the Controller sending it back (GP2).
When you press button 2 (UP) on your Controller, the LED on the Pico will turn on. When you let the button go, the LED will turn off.
If the Controller is working, that’s great! If not, this is a good time to review the steps to troubleshoot your wiring and code.
Is your Pico disconnecting after you test the Controller?
Check the program and wiring to make sure they are correct. The wires sent with your Game Controller might be different colors than depicted, which is fine. Follow the diagram to make the right connections.
Unplug your Pico, wait 5 seconds, and plug it back in.
Refresh your browser page.
Is your program not running smoothly? (Step 19)
Click DIGITAL VIEW at the bottom of the workspace. On the left side of the Digital View, there is a switch. Click the switch to turn off the Digital View.
How do I change to Mouse Mode?
First, click CONNECT and then click START. Your Controller will start in arrow key mode. Click button 11 to change it. When you do, the LED on the Pico should blink twice, indicating you are now in Mouse Mode. Try it out! Use the direction buttons (left, right, up, down) on your Controller to move the cursor on the screen. Use button 5 to left-click and use button 6 to right-click.
To return to arrow key mode, press button 11 again. The LED should blink once.
Our customer support specialists are on hand to ensure your implementation of Piper runs seamlessly. View Support Docs or Contact Support
EXPANDED RESOURCES
Term Glossary
Console Personal computers built for gaming (e.g., PlayStation, Xbox, Nintendo Switch).
Modding Making "modifications" to how a game sounds, plays, looks, or functions, usually by users.
Graphic User Interface (GUI) A way to display information on the computer screen where the user can use a mouse or touchscreen to click various buttons. (CAT)
Playtesting Playing through new inventions or builds to find bugs, test their ease of use, and get new ideas to build on.
Virtual Reality (VR) Computer technology that makes someone feel like they are elsewhere. It requires a player to wear a headset and use input devices to play games.