Game Controller Using Arduino Uno

by 23montemayor6032 in Circuits > Arduino

156 Views, 0 Favorites, 0 Comments

Game Controller Using Arduino Uno

FInal Product.jpg

Creating a game controller using an Arduino Uno is actually quite simple. It doesn't take a lot of steps to get it done. Some really great programs like the Unity Engine can be used to create games that work with the controller. There are many great tutorials on how to use Arduino with these other amazing programs however I am going to focus more on creating the controller and getting Serial communication with a basic CPP program.

Supplies

- 4 Buttons (You can create a controller with more or fewer buttons but I strongly believe that 4 buttons are enough to create an interesting game that uses the controller.)

- 4 Resistors (You will want to have the same number of resistors as buttons)

- At Least 2 Dozen Wires (Should probably get more just in case)

- LEDs (As many as you think is necessary, I only used 2)

Follow the Diagrams Shown Above.

Circuit Diagram.png
TinkerCad.png

As far as circuits go, this one is quite simple. All you have to do is follow the diagrams above. However, getting serial communication with a program will be the hard part.

The Code

The code will be split into two parts. The first will run in the Arduino and will be the simpler of the two. All that is needed to do in the Arduino's code to communicate with any program is to create a new Serial port and read/write data from/to it. To create the Serial Port we can use the function Serial.begin(9600); where the first and only parameter indicates the number of bytes that should be allocated for the serial buffer. To write to the Serial Buffer we can use Serial.write and to read from the buffer we can use Serial.read. Of course, we will only want to read from the buffer when there are bytes to be read so we can use the function Serial.available() (which will return the number of bytes that still need to be read) to check for any bytes that we can read. If you need any help on how to program the communication, just reference the code which I used which I have left below.

int buttons[4] = {2, 3, 4, 5};

int Light1 = 7; int Light2 = 8;

int mode = '1';

void setup() { // put your setup code here, to run once:

for(int i = 0; i < 4; i ++) { pinMode(buttons[i], INPUT); }

pinMode(Light1, OUTPUT); pinMode(Light2, OUTPUT);

Serial.begin(9600);

}

void loop() {

// put your main code here, to run repeatedly:

char package = 0;

for(int i = 0; i < 4; i ++) {

if(digitalRead(buttons[i]) == HIGH) { package |= 1 << i; }

}

Serial.write(package);

if(Serial.available() > 0) {

char input = Serial.read();

if(input != 10) {

mode = input;

} }

if(mode == '1') {

digitalWrite(Light1, HIGH);

digitalWrite(Light2, HIGH);

}

if (mode == '2')

{

digitalWrite(Light1, HIGH);

if(millis() % 1000 < 500)

digitalWrite(Light2, HIGH);

else

digitalWrite(Light2, LOW);

}

if(mode == '3') { digitalWrite(Light1, HIGH); digitalWrite(Light2, LOW); }

if(mode == '4') { digitalWrite(Light1, LOW); digitalWrite(Light2, LOW); }

delay(10);

}

Creating a Program

The final step is to create a program that will be able to see the controller. As I already mentioned before, there are many libraries and engines to help you out. However, if you are anything like me, then you would like to create your program from scratch. I like to program in CPP and have therefore used it to communicate with the Arduino. The main problem that I came up with was the fact that Serial Communication is different on different platforms. I have gone ahead and created my own (flawed) class for Serial Communication which works on Unix. The Source Code can be found here. I have also gone ahead and used another Serial Communication class that I found on GitHub which only works on windows, and have abstracted it to also work with my class. The source code can be found here. I then took what I learned and tried to create a game engine using it called the Arcade Game Engine. It was a total failure. I created an event system that used Serial Communication but very quickly gave up on trying to create graphics since the way I was structuring everything favored a 3d environment more than a 2d environment. The Source Code can be found here. I then tried to create a different game engine which I am still working on to this day. This engine works a lot better for 2d and is a lot simpler for me to follow. The Arcade Engine might have been a lot better structured but I was over-exerting myself trying to keep up with the same professionally standard structure when I am still just a beginner at programing. This Engine is still a work in progress but can already create a sprite, render quads with textures, and communicate with the Arduino. There is still a lot that needs to be done to improve performance and make abstract it more but what I have so far can be found here. Finally, I will soon be uploading a demo game using the engine in here.