Beginner's Guide To Make A Game Controller

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

INTRODUCTION ..................................................................................................................

2
ARDUINO IDE BASICS ......................................................................................................... 3
ARDUINO LIBRARIES .......................................................................................................... 9
EXAMPLES ......................................................................................................................... 11
SINGLE BUTTON KEYBOARD........................................................................................ 12
MULTIPLE BUTTON KEYBOARD ................................................................................... 13
MULTIPLE BUTTON MOUSE ......................................................................................... 14
JOYSTICK WSAD MOVEMENT ....................................................................................... 15
JOYSTICK MOUSE MOVEMENT..................................................................................... 16
NUNCHUK WSAD MOVEMENT AND BUTTONS .......................................................... 17
NUNCHUK MOUSE MOVEMENT AND BUTTONS ....................................................... 18
REFRENCE ......................................................................................................................... 19

BASIC FUNCTIONS ..................................................................................................... 19


KEYBOARD FUNCTIONS ............................................................................................ 19
MOUSE FUNCTIONS .................................................................................................. 20
VARIABLES .................................................................................................................. 20
SKETCH ....................................................................................................................... 20
FURTHER SYNTAX ...................................................................................................... 21
CONTROL STRUCTURE .............................................................................................. 21
ARITHMETIC OPERATORS ......................................................................................... 21
COMPARISON OPERATORS ...................................................................................... 22
BOOLEAN OPERATORS ............................................................................................. 22

1
How to get the most out of this guide.
The Beginner’s Guide to Arduino Game Controllers is a summary of all the material I’ve
learned through researching my own projects. This guide is intended to teach you the basic
fundamentals of the Arduino IDE software and introduce you to the free resources
available on my website insertcontrollerhere.com. There is a lot of information to cover
for a beginner, so take your time with each section. This is an interactive guide, all red text
contain links to sources, downloads, and recommended parts. Any materials purchased
through these links helps support the channel at no additional cost to you and allows me
to produce more free content. Please consider clicking these links before making any
online purchases.

If you have never touched an Arduino before, the introductory tutorial will walk you
through writing, uploading, and executing for first program. These are the fundamentals
required to utilize the example sketches I have provided for making game controllers.

For those that already have Arduino experience, check out the recommended components
and example code to get started right away!

Recommended Arduino board for beginners.


For making controllers, it is essential to have an Atmega32u4 based microcontroller with
built in USB support such as the Arduino Leonardo or Arduino Micro. In my experience,
the best Arduino board available for beginners learning to create controllers is the Arduino
Leonardo. There are also knockoff boards available for a more budget friendly option
that are functionally identical. A note to those already familiar with Arduino, the popular
Arduino Uno board does not have the native USB capabilities found on the recommended
boards.

You will need to have an Arduino board to complete the next section of the guide.

2
Download the Arduino IDE software.
The Arduino IDE is the open-source software that you will use to write code and upload it
to your Arduino board. The software is free to download by clicking the button below.
Downloads are available for Windows, Mac, and Linux.

Download the Arduino IDE software.


The Arduino IDE is the open-source software that you will use to write code and upload it
to your Arduino board. The software is free to download by clicking the button below.
Downloads are available for Windows, Mac, and Linux.

Run the IDE software.


Now is the time to connect your Arduino board to your computer via the USB cable and run
the Arduino IDE software. You will see a blank sketch with the basic setup and main loop.

3
Write a basic program.
Let’s begin with a simple sketch that prints the phrase “Hello World.” In the loop section of
the sketch, write the following lines of code (lines 8 and 9):
Serial.println("Hello World");
delay(1000);

Save your sketch.


The IDE requires you to save your sketch before you can compile or upload it. Click the
save button and select a location you will remember. You will know the sketch was
successfully saved if you see “Done saving” above the black console window.

4
Select your board type.
You must select your board type before you can communicate with your connected
Arduino board. For the board type, go to Tools → Board → Select your board type
(Leonardo or Micro).

Select your COM port.


In addition to board type, you must also select the proper COM port to communicate with
your Arduino board. The COM port may change each time you reconnect via USB; however,
the correct port will always have the board name next to it. For the COM port, go to
Tools → Port → Select your COM port.

5
Verify the sketch.
Your code must be compiled/verified before it can be sent to the Arduino. Compiling code
will check for logical errors. Any found errors will be listed in the console window below
your code. Click the verify button. You will know the sketch was successfully compiled if you
see “Done compiling” in above the console window. If you run into any errors, check your
code again to ensure you copied it correctly.

Upload the sketch to your Arduino microcontroller.


You can now upload the sketch to the connected Arduino board. Click the upload button. If
the software has any issues uploading to your Arduino board, you will see an error
message in the console window. You will know the program was successfully uploaded if
you see “Done uploading” above the console window. If you run into any errors, check that
you have the correct board type and COM port selected as well as the USB connection to
the Arduino board.

6
Check the serial monitor.
The Arduino can be programmed to send useful information to the serial monitor using the
Serial.println() function. To open the serial monitor, go to Tools → Serial Monitor. For
this example, you will know that the program is successfully running if you see the phrase
“Hello World” appear once every second.

7
Congratulations!
You have successfully written, uploaded, and executed your first Arduino sketch! Once you
feel comfortable with the basics of Arduino and have browsed some of the resources
available on my website, it’s time to start playing around with example controller sketches!
This is where the real fun begins.

Practice with example controller sketches.


Now it is time to start working on the example projects. The projects are labeled based on
the main component used and the controller’s basic function. Each project download
includes the source code and a reference page. The project reference page contains a brief
description of what the project does, a parts list, required libraries, and a wiring diagram.
There are also comments available within the code to explain the main functions. You can
download these examples for free by clicking the button below or visiting my website.

Join the community Discord server.


Finally, come join the rest of us on our community Discord server! Discord offers us a
central location to share ideas and get help on projects. Our server is full of people just like
you that love video games and want to learn more about controller design. It’s also a great
place to just chat with gamers and find groups to play with! Click the Discord button below
to receive an invitation to the server. For those that don’t already have it, you can sign up
for an account and download the software for free on discordapp.com.

Welcome to the controller creating community!

8
How to install an external library.
Libraries are extensions that add additional functionality to your sketch. All of my example
programs contain libraries that are required to function. Several libraries are included with
the IDE, but externally developed libraries can be installed via .ZIP files. External libraries
can be installed under Sketch → Include Library → Add .ZIP Library.

Default Libraries
These libraries are built into the IDE and do not require installation.

KEYBOARD
This library enables you to send keystrokes to a connected computer. The library is
essential to making controllers and is used in every program I make. This includes the
Keyboard.press() and Keyboard.release() functions.

MOUSE
This library enables you to send mouse commands to a connected computer. This includes
the Mouse.move(), Mouse.press(), and Mouse.release() functions.

WIRE
This library enables you to communicate with I2C devices over the SDA (data) and SCL
(clock) pins. Wire.h is included with IDEA and does not require installation. I2C devices that I
commonly used for game controllers include the MPU-6050 and the Wii Nunchuk.

9
External Libraries
These libraries must be downloaded and installed.

MPU-6050
Author: Jeff Rowburg - GitHub Link

This library is essential for using the MPU-6050 sensor. The library enables easy readings
for the MEMS accelerometer and MEMS gyroscope to find the position and acceleration
along the XYZ axes. Communication uses the I2C bus through the SCL and SDA pins.

NINTENDO EXTENSION CTRL


Author: David Madison - GitHub Link

This library enables simplified input readings from a wide selection of Nintendo extension
controllers; The most important of which is the Wii Nunchuk. The library provides data for
the joystick, C and Z buttons, and the built-in accelerometer. Communication uses the I2C
bus through the SCL and SDA pins.

HID BUTTONS
Author: David Madison - GitHub Link

This library greatly simplifies the code required to track the use of buttons and joysticks on
your controller. This removes the need to have statues variables and if statements to avoid
button spamming for every loop of the program cycle.

10
All of my example projects are free to download on my website. These are the
essential building blocks to my controller designs. To make these as user friendly as
possible, I broken the code down into sections and provided comments on as many
important lines as possible. This should make these examples easier to understand,
modify, and combine into your own designs.

Each example includes:

1. A brief description of the example code’s function.


2. A download link for the project source code.
3. A parts list with links to Amazon for exact components used.
4. A list of required libraries including download links for external sources.
5. A color-coded wiring diagram.

If you are new to Arduino, take your time with these examples and be patient. This
is a collection of concepts that took me months of researching and practice to
understand. The projects progressively increase in difficulty, so make sure to
understand all the components and functions before moving on. Utilize the
reference section at the end of this guide to look up any syntax or functions you do
not understand. If you run into any issues, do not hesitate to ask for help on our
community Discord chat. We have members that are more than willing to assist
with people of varying skill levels.

11
Project Description:
This example shows how to emulate a key press with a single button.

Source Code:
Example Code Downloads – Single Button Keyboard

Parts List:
1. Arduino Leonardo
2. Micro USB Cable
3. Breadboard
4. Jumper Wire
5. Push Buttons

Required Libraries:
1. Keyboard.h (Included)
2. HID_Buttons.h (Download)

Wiring Diagram:

12
Project Description:
This example shows how to emulate key presses with multiple buttons. You do not
need all 10 buttons for this example, but the code does show you how 10 buttons
would funtion at once.

Source Code:
Example Code Downloads – Multiple Button Keyboard

Parts List:
1. Arduino Leonardo
2. Micro USB Cable
3. Breadboard
4. Jumper Wire
5. Push Buttons

Required Libraries:
1. Keyboard.h (Included)
2. HID_Buttons.h (Download)

Wiring Diagram:

13
Project Description:
This example shows how to move the mouse and emulate the left, right, and center
mouse click with buttons.

Source Code:
Example Code Downloads – Multiple Button Mouse

Parts List:
1. Arduino Leonardo
2. Micro USB Cable
3. Breadboard
4. Jumper Wire
5. Push Buttons

Required Libraries:
1. Mouse.h (Included)
2. HID_Buttons.h (Download)

Wiring Diagram:

14
Project Description:
This example shows how to emulate the WSAD movement keys with a joystick.

Source Code:
Example Code Downloads – Joystick Keyboard Movement

Parts List (Amazon Links):


1. Arduino Leonardo
2. Micro USB Cable
3. Breadboard
4. Jumper Wire
5. Joystick

Required Libraries:
1. Keyboard.h (Included)
2. HID_Buttons.h (Download)

Wiring Diagram:

15
Project Description:
This example shows how to emulate mouse movement with a joystick.

Source Code:
Example Code Downloads – Joystick Keyboard Movement

Parts List (Amazon Links):


1. Arduino Leonardo
2. Micro USB Cable
3. Breadboard
4. Jumper Wire
5. Joystick

Required Libraries:
1. Mouse.h (Included)
2. HID_Buttons.h (Download)

Wiring Diagram:

16
Project Description:
This example shows how to use the Wii Nunchuk joystick for WSAD movement and
the C and Z buttons for key presses.

Source Code:
Example Code Downloads – Nunchuk WSAD Movement and Buttons

Parts List (Amazon Links):


1. Arduino Leonardo
2. Micro USB Cable
3. Breadboard
4. Jumper Wire
5. Wii Nunchuk
6. WiiChuk Adapter

Required Libraries:
1. Keyboard.h (Included)
2. HID_Buttons.h (Download)
3. NintendoExtensionCtrl.h (Download)

Wiring Diagram:

17
Project Description:
This example shows how to use the Wii Nunchuk joystick for mouse movement and
the C and Z buttons for key presses.

Source Code:
Example Code Downloads – Nunchuk Mouse Movement and Buttons

Parts List (Amazon Links):


1. Arduino Leonardo
2. Micro USB Cable
3. Breadboard
4. Jumper Wire
5. Wii Nunchuk
6. WiiChuk Adapter

Required Libraries:
1. Keyboard.h (Included)
2. Mouse.h (Included)
3. HID_Buttons.h (Download)
4. NintendoExtensionCtrl.h (Download)

Wiring Diagram:

18
This is a curated reference list to the main parts of the Arduino programming
language. While the full reference list can be found here, I shortened the list to
those components that I find most important to controller design for beginners to
understand. This is a great resource for when you read example code and do not
understand the function of certain parts.

BASIC FUNCTIONS

pinMode() – Used to set pins as input or output.

digitalRead() – Reads the value of a digital pin, then returns HIGH or LOW.

analogRead() – Reads the value of an analog pin, then returns a value between 0
and 1023. This corresponds with the voltage reading between 0 and max voltage
(3.3v or 5v).

delay() – Causes the program to pause for the given time in milliseconds (1/1,000 of
a second).

KEYBOARD FUNCTIONS

Keyboard.write() – Emulates pressing a key by sending a keystroke to the


connected device.

Keyboard.press() – Emulates pressing and holding a key down.

Keyboard.release() – Emulates releasing a key.

*ASCII Table – A list of decimal values for ASCII characters.

*Keyboard Modifiers – A list of decimal values for special modifier keys.

19
MOUSE FUNCTIONS

Mouse.move() – Moves the mouse cursor relative to its current position.

Mouse.click() – Emulates a single mouse left click. This can also be modified to
send right and middle clicks.

Mouse.press() – Emulates pressing and holding the left click.

Mouse.release() – Emulates releasing the mouse click.

VARIABLES

bool – Variable type that stores either TRUE or FALSE. Useful for comparisons.

char – Variable type that stores a single character such as ‘a’ or ‘z’. Useful for
assigning keystrokes to buttons.

int – Variable type that stores whole numbers.

float – Variable type that can store numbers with decimal points.

void – Void is a keyword used to declare functions.

SKETCH

setup() – The first function that runs when the Arduino turns on. Used to initialize
variables, pin modes, and libraries.

loop() – The loop function is the main function that repeats continuously after the
setup function and will run indefinitely while the Arduino is powered on.

20
FURTHER SYNTAX

#define – Used to assign a name to a constant value.

#include – Used to include external libraries in the program.

// (comment) – Used to write comments within the program. Starting a line with
“//” will cause the Arduino to ignore any text written on that line.

; (semicolon) – Used to end a statement. Similar to using a period at the end of a


sentence.

CONTROL STRUCTURE

if – Checks a condition then executes if that statement is TRUE or skips if that


statement is FALSE.

else – Provides greater controller over an “if” statement. When an “if” condition is
FALSE, the program will move on to the subsequent “else if” and “else” statements.

for – Repeats the loop for a defined number of iterations.

while – Loops continuously as long as the provided statement is true.

ARITHMETIC OPERATORS

= (assignment operator) – Used to assign a value or expression to a variable.

* (multiplication) – Used to multiply.

+ (addition) – Used to add.

- (subtraction) – Used to subtract.

/ (division) – Used to divide.

21
COMPARISON OPERATORS

== (equal to) – Used to compare one variable or value to another. Returns TRUE if
equal.

!= (not equal to) – Used to compare one variable or value to another. Returns TRUE
if not equal.

> (greater than) – Used to compare if one variable or value is greater than the
other.

< (less than) – Used to compare if one variable or value is less than the other.

>= (greater than or equal to) – Used to compare if one variable or value is greater
than or equal to the other.

<= (less than or equal to) – Used to compare if one variable or value is less than or
equal to the other.

BOOLEAN OPERATORS

! (logical not) – TRUE if the operand is FALSE and FALSE when the operand is TRUE.

&& (logical and) – TRUE only if both operands are TRUE.

|| (logical or) – TRUE if either or both operands are TRUE.

22

You might also like