Showing posts with label serial. Show all posts
Showing posts with label serial. Show all posts

Saturday, December 31, 2016

Making a Custom PC Gamepad with Arduino



What: Making a simple gamepad from an Arduino that you can use on the computer

Why: I wrote another article earlier this year about using the RXTX java library involving basic communication with a computer and finally got a free weekend to make an even better project using that same tech. Furthermore, a Twitch streamer by the handle 'Rudeism' has become well known for using a 'Makey Makey' microcontroller in video games, and it should be noted that he inspired me to finally get working on my own fun game controller.

Required: 
- Arduino, Arduino IDE
- Switches, buttons, and/or joysticks
- Computer
- Java
- RXTX Java Library
- Code available on my Github

Notes: For this tutorial, I am using an old analog computer joystick. It used a gameport adapter, but I have since cut it off so I can get at the wires directly. The important take away is that I have wired the outputs from the potentiometers of the joystick directly into the analog input pins of the Arduino. Further, the buttons from the joystick are also wired directly into the digital input pins. The program files available for this tutorial assume only two axes and two buttons. If you want to make a more complicated gamepad, I encourage you to do so; the concept is the same. 

I am using an Arduino Uno R3. The Uno does not have native HID support which is why we need the Java program on the computer. The joystick I am using an old InterAct Raider Pro Digital that I found while thrifting. This tutorial does not require flashing the Uno’s core firmware.

Brief: The basic idea is that the Arduino will receive the inputs (either digital or analog) from a button or joystick you have and send it over the serial connection to your PC. The PC will then parse that input into something actionable. Using the Java ‘Robot’ class, your Java application will move the mouse, click a button, or enter a keystroke. Here is an abstract diagram for how it should work:



Steps:

1. Connect your switches, buttons, and potentiometers to the Arduino


[x-axis potentiometer signal] -> [analogPin0]
[y-axis potentiometer signal] -> [analogPin1]
[trigger button] -> [digitalPin3]
[thumb button] -> [digitalPin4]
[GND] -> [GND]
[5v] -> [5v]

2. Upload the Arduino sketch
     View my sketch example video HERE.

3. Run the Java application
     That's it! Your potentiometer inputs now drive your mouse, the trigger button is a left click, and the thumb button is a right click.

Troubleshooting:

-If you need more help with the RXTX library specifically, please refer to my previous article on using the library HERE.

-If you have questions about the process or this project in particular, post a comment and I'll try to help you out.

More process photos:
(Testing direct connections)

(Connections soldered and labeled)

(Ready for game testing)

Saturday, December 3, 2016

How to Use RXTX Library for Java & Arduino Communication


I have been thinking about doing this project for a while, and since I've been riding a tech/craft wave lately, I thought there was no better time. *Note to reader, this tutorial is crafted to a Mac OS X environment and its quirks, if you need a tutorial for another environment start here: Arduino Java Interfacing Tutorial
What: Programming an arduino and java application to perform serial communication.
Why: Why not?! Think about it. Once you have that neat arduino project, you add in 'duino-PC communication and now you're really taking it to the next level! Back during my computer science undergrad, I built an anemometer for an IEEE event when I was first getting started with arduinos and learning about EE stuff. When it was hooked up to the arduino IDE, I could read its output values, but that's it. Now, if I was to hook it back up, add in some serial communication, I could start logging that data! If logging data doesn't do it for you, there are tons of other things you can do with this. Let's get started.
Things you will need:
-Arduino : I am using an Arduino Uno v3
-Computing Environment : I am using a Macbook Air running OS X El Capitan (10.11.3) with Java 1.8, and coding my application using Eclipse.
-RXTX Libraries : Can be downloaded at: RXTX Libraries (Warning - If you need the 64 bit libraries, you will have to source them elsewhere. If you need the more rare mac os x 64 bit JNI library, you can get it here: RXTX 64 bit Library)
Getting Started: The first thing you want to do is download the correct RXTX libraries and install them. You can add them as extensions to the java runtime environment you have installed or load them into your project directly. What's the difference? Well the guys who built the binaries for RXTX say that adding them directly to your runtime environment can be dangerous if you are writing cross platform functionality. If you are not concerned, copy the libraries (.jar and .jnilib) to the path "/Library/Java/Extensions". You will likely need to provide your password in order to modify this directory. If you are concerned, copy the libraries into your project using an IDE like Eclipse, add them to your classpath in your makefile, etc.
Once that is done, copy the code example from the tutorial in the following link: Arduino Java Interfacing Tutorial. It is important that you understand each step of the code and what each piece accomplishes. This will allow you to improve upon the example code in whatever ways your project demands. I will be posting a heavily-commented version of this tutorial to my github at the following link: Basic Tutorial
Status check. You have the libraries installed, you have the source code, arduino is ready to go. Plug that arduino in and run your program. I bet you probably got an exception didn't you? Follow the error resolution steps and see if it can't get you unstuck.
Error: "java.lang.UnsatisfiedLinkError"
Solution: You are on a newer Mac and have a 64 bit architecture running with a 32 bit library. Download the 64 bit JNI library I linked to above.
Error: "gnu.io.PortInUseException"
Solution: Create a directory named 'lock' in the path '/var' with permissions 777. If you are not sure how to do this, open a utility application on your mac called Terminal. Next, type the following command and then hit enter: 'sudo mkdir /var/lock'. It will likely prompt you for your password. Next, enter the additional command and hit enter: 'sudo chmod 777 /var/lock'.
Good job! If you made it this far, you should be past all the frustrating parts, and your arduino is happily chirping "Hello World" over and over into your console! What's next? Experiment and add functionality for sending data TO the arduino. Good luck!