Arduino Game Controller With Lights Responding to Your Unity Game:

Published

Introduction: Arduino Game Controller With Lights Responding to Your Unity Game:

First off I wrote this thing out in word. This is the first time I use instructable so whenever i say: write code out like so know that I'm referring to the image at the top of that step.

In this project I use 2 arduino’s to run 2 separate bits of code since I had some trouble getting it working with just one

These are the Uno and the Leonardo

in total this is what you'll need to breadboard this project.

· 7 push buttons

· 23 jumper wires

· 1 arduino Leonardo + usb cable

· 4 resistors
· 4 led’s

· 1 arduino uno + usb cable

· 2 breadboards

Step 1: Hooking Up the Control Buttons to Arduino

What you’ll need to hook this part up:

· 7 push buttons

· 14 jumper wires

· 1 arduino Leonardo + usb cable

· breadboard

The buttons are pretty simple to hook up but can quickly
become a mess to display in image form.

· first of you place the push buttons in the positions I marked on the image above.

· Secondly you want to hook up a jumper from the ground slot on the arduino Leonardo into the minus row of the breadboard.

· Now you hook up jumpers from anywhere on the minus row to the row that is in line with the right pin of you push button . this will feed power to it.

· Next you want to hook up a jumper wire from a row that’s in line with the left pin of your push button to the Leonardo’s digital input slots. For the buttons here I used slot 2 - 3 – 4 – 5 - 6 - 7- 8

I only showed the wire going to input 8 on the image cause it becomes a very messy picture to show you all the wires crossing each other.

Step 2: Coding the Arduino Leonardo:

So to tell you the reason I’m using Leonardo for this. It’s because
it has a certain chip that the Uno does not that makes the computer recognize it as a usb input device. Much like It would a keyboard. Which allows us to do something that makes our lives as programmers for this project a hell of a lot easier. Keyboard emulation. We are gonna trick the computer into thinking that we are pressing certain keys on the keyboard when we are in fact pressing buttons on our controller.

Now I’m going to walk you thought the code step by step .

First off all the way at the top we include Keyboard.h . This will allow us to use the keyboard functions we are gonna use in our code.

Step 3:

Then we need to define the digital input slots we are going to use on the

Leonardo for each button.

I’ve also named every one of them after the key I am going to emulate.

Step 4:

From there we get into the setup function. This tells the

arduino what it does on start up.

first we write the Serial.begin which will open the serial port and sets data rate to 9600 bps

and we define all are named pins to be inputs.

Step 5:

And finally we write our loop function which will check if

buttons are being pressed and tell the arduino how to respond to that .

First we write Keyboard.begin which will tell arduino that he needs to look out for us using keyboard functions

Step 6:

Then for every button we write an if/else statement that goes like this

So what I told arduino here is: If my left button is being
pressed on the controller make the computer think we are pressing the “a” key and if that isn’t the case make it think that we have let go of the key.

You basically repeat this block of code for every button on your controller. with changes for every variable and the keys that variable needs to emulate

Step 7:

The keys we told the computer to hit here are W – A – S – D which

are pretty much the standard for movement in pc games and E – Q – and the spacebar which in this code is recognized as ‘ ‘ which are the buttons I use to pick-up – dismiss and fire in my game. Feel free to change these to whatever keys you feel work better for your game/controller.

Then lastly we tell the arduino to stop the check which Keyboard.end

Step 8: Coding Movement in Unity:

First off let me tell you I am making this for a 2d game so

the examples you’re going to see are built for that. Mainly meaning where I use vector2’s if you are working in 3d you will use vector3’s. because you would need to worry about am extra dimension of movement in 3d.

Now the reason I used Keyboard emulation on the arduino side is because Unity has something build in to recognize keys on the keyboard which we can use.

If you open up your project in unity go to Edit -> Project settings -> Input. You can see in your inspector it boots up the input manager. Where if you click on Axes you can see a whole bunch of input names open up which are normally used for pc controllers like the ones we are making now. The 2 inputs we are going to use are Horizontal and Vertical which check for you guessed it W-A-S-D.

First you want to make an object In your unity scenes hierarchy which will be your player. In my case being this fun little guy. This can be whatever you need it to be but for the purpose of my game I made this guy.

Step 9:

You want to give this player 2 things. A box collider 2d and

a Rigidbody 2d and edit them to be like the image above.

Step 10:

Then you want to go into your projects folder and create a

C# script. Which I named PlayerMovement.

In this script you’re going to define 2 variables. A public float which I call moveSpeed. Which will determine how fast you go. And a private Rigidbody2D which I call RigidPlayer. Which will look for your players rigidbody.

Don't forget to got to your unity editor click on your player add this new script by dragging it to the inspector and set a number value for moveSpeed.

Step 11:

now in your Start() you want to do one thing and that is to

make sure your RigidPlayer can find your payer rigidbody 2d. You do that with the following line of code:

Step 12:

Then we move on to the update(). Here is where we are going

to make the player move.

we do that with a pretty simple if statement that will look at the negative and positive keys from the Horizontal (A-D) and Vertical (S-W) and apply that as a number to force we are gonna put on our rigidbody 2d to push our player in a direction. That number will be multiplied by the moveSpeed variable.

Step 13:

Lastly you want to make sure your player stops moving when

you don’t press anything. This you do like so:

Now if you wanna use your 3 other buttons for anything you
do that with this if statement: if(Input.GetKeyDown(Keycode.Space))

for the spacebar and instead of space put the letter of the other keys you chosen to use for the other keys.

Step 14: Hooking Up Lights to Arduino

·

What you’ll need to hook this part up:

· 9 jumper wires

· 4 resistors

· 4 led’s

· 1 arduino uno + usb cable

· breadboard

The hookup for the lights will be done through the Uno.

It will end up looking like this image above:

You hook up the resistors to the long side of the LED’s. And
then you hook up a jumper from a digital pin number on the uno to the resistors . in this case I used digital pin slots 9 – 10 -11 -12 these numbers will be relevant when we get to the code for the lights.

From there you wanna hook up a jumper from the ground slot on your arduino Uno to the spot on your board as seen with the black wire on the image above.

Then you want to have 4 jumpers all lead into the short end of each LED

Step 15: Coding the Arduino Uno

Alright first of we are going to define our variables again

So first which digital inputs we are using for our lights. And we are going to make a char called myCol[20]

Step 16:

Then in the setup we want to do the Serial.begin again. Put all our LED’s on Output and turn all of them off on startup.

Step 17:

And finally you want to write your loop function.

This will cause the Arduino to read in any data coming in through serial until one of two events happen. Either a line feed character is detected; this is identified by lf which is set to 10 and is the first argument or a certain amount of bytes have been read in. This is the third argument and for this example is set to just one byte. The data is stored in the variable myCol, which is set to char and a limit of 20. In readBytesUntil this is the second argument. Or another way to explain it is Serial.readBytesUntil(terminatorCharacter, buffer, byteLimit);

And the if statements will make sure certain lights turn on when unity gives the signal. In this case I got 4 different colored lights so I make unity send g for the green LED to turn on, b for the blue LED to turn on, r for the red LED to turn on and y for the yellow LED to turn on.

Step 18: Coding Lights Unity Side:

Oke 2 things first before we get into this.

1.in your arduino coding program go to Tool -> Port -> and check for me which COM your uno is on. In my case its COM3 (this will be important in the code)

2.in unity Go to Edit -> Project settings ->Player then in the inspector click on other settings go to Api compatibility Level and Switch it from .NET 2.0 subnet to just .NET 2.0

Oke that’s done. Let’s get into it.

Right click on your hierarchy and click on Create empty.

This will be an empty game object that will just exist in your game to hold one script for us. I’ve called this object Init.

Step 19:

Then go into your projects tab and create a new C# script

and call it Sending.

First thing you do is write these to lines in the using sections of you code:

using System.IO.Ports;

using System.Threading;

this makes it so we can use the SerialPort variable

Step 20:

in this script we are going to make the following variables. Now do note I got COM3 there this might be different for you as we discussed at the beginning of this step. If it is replace the 3 with the number it said for you.

Step 21:

In the start we tell the script to use are OpenConnection

function which we are gonna write in a bit

the update is more a test then anything but if you want to include it its to test messages being sent to unity. You could ignore it honestly.

Step 22:

Oke now for this OpenConnection function. This is just a big

if/else statement that keeps the connection open just copy it like written and you should be fine.

Step 23:

Now Remember from the arduino code that I made it check for

signals that unity is gonna send to it.

well this is where that happens. In my case I got 4 lights hooked up so I wrote 4 functions that are extremely simple. All they do is when they are called they send a letter over to arduino. When arduino get that letter it lights up the light corresponding to said letter .

Step 24:

You might be wondering how do I get it to call this

function? Well you do it with one simple line in any of your other c# scripts you use for your game. Sending.NameFunctionhere();. So for example in my game the player is tasked to collect 4 colored orbs. So I wrote a little script that makes sure that when he gets close to one and presses the right button he picks it up and it sends the signal to the arduino that in this case the Yellow orb has been picked up. So that it knows ,oke I gotta turn the Yellow LED on:

You could put this one line of code anywhere you like. If
you want it to light up when the game starts put it in the start function. Want it to light up when he dies put it in your function for the players death. Experiment around a bit . I choose to make the lights a sort of ui element outside of the game to keep track of the orbs you got to collect without cluttering up the screen.

Share

    Recommendations

    • Water Contest

      Water Contest
    • Creative Misuse Contest

      Creative Misuse Contest
    • Oil Contest

      Oil Contest

    Discussions

    Very nice! Thanks for sharing!