Showing posts with label java. Show all posts
Showing posts with label java. 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)

Making Humanistic Mouse Clicks with Java Robot Class

Preface: Java's Robot class allows you to send mouse and keyboard signals to the operating system through APIs designed for indirect interaction with the PC. By using this Java class, we can perform actions like scrolling, clicking, and typing without actually doing so.

Problem: While experimenting with the Robot class for an Arduino gamepad I was making, I discovered a problem with clicking the virtual mouse. Let's look at the code.

This is how it is explained in Java examples, and how other users have taken to using the mouse click:
Robot robot = new Robot();
    
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
The button is pressed and immediately released. Even adding a delay by way of a Thread.Sleep will still result in the same problem. What if I have a function that detects an event and presses the mouse button? This certainly works for that case, but what if I am holding the mouse button down? Instead, this code will simulate a set of rapid clicks for as long as the button is held. That's not the behavior I wanted. Furthermore, the right click code was verbatim with the mask changed to account for the different button. Right clicking did not work at all since it opened and close any context menus far too fast to be useable.

Solution: Using some extra state information, we can track whether or not the mouse button should be held down, or if it should be released. Assuming the event triggers occur often enough, the user should not notice whether the mouse is only clicked or held for any period of time.

int currentLeftClickState = 0;
...
if(trigger == 0 && currentLeftClickState == 1){
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
this.currentLeftClickState = 0;
}
else if(trigger == 1 && currentLeftClickState == 0){
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
this.currentLeftClickState = 1;
}
In my example, I declare a simple flag using an int that will represent the current state.  When I receive the literal 'trigger' my function code will validate the trigger against the current state. Only if there is a difference between them, do we need to change the mouse behavior.

This code allows you to now select windows, drag objects, open context menus correctly, and even select multiple lines. Cool stuff right?

To see the project this code was written for, please check out my GITHUB.

Saturday, December 10, 2016

Keeping Track of Skill Practice Hours (A JSON, JAVA Project)


I've been wanting to write program that allowed me to keep track of my practice hours for various computer related skills. Some years ago, I read a book by Malcom Gladwell that examined experts of various fields from athletics to music. Each of these experts had practiced their craft for at least an estimated10,000 hours. Eventually, I will surely reach this number with crafts like programming, but it would be hard to say when if I wasn't actually keeping track. So I wrote an interactive program to do just that.

I called it 'Keeper', and it's freely available to download on my GitHub if you want to check it out. 

How it Works: Keeper uses what I called 'trackers' to record events for specified skills. In the source, you'll notice that I used mostly computer languages as the skills you could track. This is a limitation of the example code, but in practice, you would not be limited to the number of trackable skills currently available. Once you've entered data, you could perform analysis and get the following data:
  1. What year did I practice X the most/least
  2. What time of day did I practice X the most/least
  3. Which skill did I practice the most/least
  4. etc
I really enjoyed this project, but when I showed it to a friend, they asked why I wouldn't just use Excel to do the same thing. I didn't have a good answer, so I have since switched over to using Excel. The project still serves as a couple good examples of using JSON to serialize, deserialize data.