Arduino Uno as a game controller

I was wondering how I could go about using a potentiometer and an Arduino Uno as an in-game controller, say for a throttle or something. I wouldn't be the only controller, just an add on that did one thing.

I would also like to eventually do switches as well.

Thanks, Viper360

it's pretty simple. connect the wiper to one of the pins available for a/d conversions.
function analogRead() is your friend.

Trim pots have 3 pins. Connect GND to one of the outer pins and 5V to the other outer pin. Connect the middle pin to an analog input. Then, in your code, call "value = analogRead(potentiometerPin);" and then use value to control speed, position, or whatever else you want to do.

Switches are similar, except you use digitalRead() and need to debounce the button press.

I have looked at that and it looks fairly simple to get hooked up, but was I was more concerned about was getting the computer and the game to recognize it as a controller.

Viper360:
I have looked at that and it looks fairly simple to get hooked up, but was I was more concerned about was getting the computer and the game to recognize it as a controller.

Ahhh, I see now.

Ok, so the deal is actually how to send serial data to the PC (or whatever console you're using). Speaking of which, what platform/console do you want to plug the Arduino into? Is it a PC? XBox? Play Station?

Power_Broker:
Ahhh, I see now.

Ok, so the deal is actually how to send serial data to the PC (or whatever console you’re using). Speaking of which, what platform/console do you want to plug the Arduino into? Is it a PC? XBox? Play Station?

It is a PC, and I have been able to connect it with the serial port and processing, although fairly rudimentary stuff. I think my problem will be getting the game to recognise and do something with the input.

Viper360:
It is a PC, and I have been able to connect it with the serial port and processing, although fairly rudimentary stuff. I think my problem will be getting the game to recognise and do something with the input.

You should've mentioned all of this in your original post.

You need to give as much detail about your project as possible (and any problems you're running into). For instance, you need to describe what game you're trying to connect to, what did you do in your Processing sketches, and if you've actually attempted to connect the Arduino to the game (and the results of the attempt - i.e. any errors thrown?).

Power_Broker:
You should've mentioned all of this in your original post.

You need to give as much detail about your project as possible (and any problems you're running into). For instance, you need to describe what game you're trying to connect to, what did you do in your Processing sketches, and if you've actually attempted to connect the Arduino to the game (and the results of the attempt - i.e. any errors thrown?).

Ah, sorry I'm new to this forum.

I haven't attempted to connect it to the game yet, I really don't know how. I would be star citizen.
I am currently having a problem with processing not exporting any applications however.

Viper360:
Ah, sorry I'm new to this forum.

I haven't attempted to connect it to the game yet, I really don't know how. I would be star citizen.
I am currently having a problem with processing not exporting any applications however.

Don't worry about it, everyone has to start somewhere.

Check out these neat links I found on the Star Citizen forum:
Link1
Link2

I didn't look into them in much detail, but it looks like they might help.

Try using python instead of processing. It is relatively simple and easy to learn.
For the graphics in your games, I would suggest pySDL or pygame.
To control the arduino, try using pyfirmata. Look at the following code for how to initialize it.

#getting the library
from pyfirmata import Arduino, util
#initializing your board
board = Arduino('/dev/tty.usbserial-A6008rIF')

Then, you can use analog and digital read functions for reading your potentiometer and switch.

If pyfirmata doesn't support your operating system, try using pyserial.

Below are some links that may help you:

https://pysdl2.readthedocs.io/en/rel_0_9_5/
http://www.pygame.org/hifi.html
https://pythonhosted.org/pyserial/

Thanks for the advice guys, it looks very useful. Just one more question for now haha, I found this http://howtomechatronics.com/projects/arduino-game-controller/

Which basically uses the Arduino to press keys on the keyboard. Now my question is, is this more of a work around? Is there a way to not use the keyboard at all, and just have the switch do something in the game, and in game assign that button to a certain action which it looks like is what this guy does

Thanks again

In the video, the person appears to be using a component from sparkfun, therefore, the library is specific to that component.

You could also do this by using pyfirmata. If you upload the standard firmata code onto the arduino, and connect the arduino to the computer via usb cable, pyfirmata can read any pin.

So if you have a switch connected to 5v and pin 13, pyfirmata can read the digital value of pin 13, to see if the button is pressed or not. Since python runs on the computer, you can also use it for your graphics.

Here is some sample code if you using a switch with pin 13, where val is the state of pin 13, where the Com Port is : /dev/tty.usbserial-A6008rIF

PYTHON CODE:

from pyfirmata import Arduino, util
board = Arduino('/dev/tty.usbserial-A6008rIF')
val = board.digital[13].read()

ARDUINO CODE:

#STANDARD FIRMATA SKETCH FROM ARDUINO EXAMPLES

nehalda:
In the video, the person appears to be using a component from sparkfun, therefore, the library is specific to that component.

You could also do this by using pyfirmata. If you upload the standard firmata code onto the arduino, and connect the arduino to the computer via usb cable, pyfirmata can read any pin.

So if you have a switch connected to 5v and pin 13, pyfirmata can read the digital value of pin 13, to see if the button is pressed or not. Since python runs on the computer, you can also use it for your graphics.

Here is some sample code if you using a switch with pin 13, where val is the state of pin 13, where the Com Port is : /dev/tty.usbserial-A6008rIF

PYTHON CODE:

from pyfirmata import Arduino, util

board = Arduino('/dev/tty.usbserial-A6008rIF')
val = board.digital[13].read()




ARDUINO CODE:


#STANDARD FIRMATA SKETCH FROM ARDUINO EXAMPLES

I am defiantly going to try and learn some phython.

The game isn't one I am making though, its an existing one. I just a switch to be used as button for controller. So like button 3 on a joystick or something, i want to get a switch to do that.

Oh... Please specify what controller you are trying to replace.

If the switch is going to act like a key on a keyboard, try using the Mouse and Keyboard library.
(Please refer to this link): Keyboard - Arduino Reference

By reading the digital value of a pin, you could use the library to trick the computer into thinking that you typed a character, to manipulate the game.

nehalda:
Oh... Please specify what controller you are trying to replace.

If the switch is going to act like a key on a keyboard, try using the Mouse and Keyboard library.
(Please refer to this link): Keyboard - Arduino Reference

By reading the digital value of a pin, you could use the library to trick the computer into thinking that you typed a character, to manipulate the game.

I have thought about that way, and it seems the easiest for now, but I would also like to do it another way. Which is to have the computer recognize it as a controller, and use the buttons


I want an input to be one of these buttons.

It would probably be easier for now just to use it as a keyboard output, but I have an uno and it looks like it isn't directly supported on the uno.

It looks like this tutorial could help with that though?

having to short out too pins and change the firmware scares me though.

I just found this instructable. Please let me know if this helps you.

Sorry, just realized it is not for Arduino Uno R3. :frowning: :frowning: :frowning:

Have you checked out unojoy?