PC Mouse Made With Arduino Uno and Joystick (2024)

Introduction: PC Mouse Made With Arduino Uno and Joystick

By Syntaxian

About: Research intern at UW-Madison. I have a passion for tinkering. More About Syntaxian »

Hi! Welcome to my first Instructable.

I recently began tinkering with my new Arduino Uno and decided to find an application for a PS2 joystick module. I thought it would be nifty to turn my Arduino into a joystick controlled mouse for my PC.

I must confess: I thought the task would be easy using the "mouse" library I found online, little did I realize that this class only works with Arduino Leonard and Micro (and perhaps a few others), but not the ubiquitous Uno. I was discouraged, but I decided to use the Uno's serial communication as well as my rudimentary Java abilities to "hack" the Uno into a functional mouse for Windows. It surprisingly worked! Here's how:

Step 1: Materials

This project does not require many materials:

1 Arduino Uno

5 male to male wires

5 female to female wires (to connect to joystick module and to add extension length for joystick.

1 Joystick (I used the SainSmart PS2 joystick module and would recommend it)

Step 2: Setting Up the Arduino Uno

The setup of the Uno can be seen in the materials picture, and here's the instructions:

Connect the five female to female wires to the pins of the joystick module. Now, connect five male to male wires into the ends of the female wires and connect them to the Arduino in this way:

1. The Ground on the joystick to Arduino Gnd

2. The +5V on the joystick to Arduino 5V

3. The UPx on the joystick to A0 on the Arduino

4. The UPy on the joystick to A1

5. The SW pin (the digital click switch) to digital pin 7 on the Arduino

Step 3: Upload the Joystick Program to Arduino

Connect the Uno to your PC and upload the joystick code seen here (please note I did not create this code originally):

int pushPin = 7; // potentiometer wiper (middle terminal) connected to analog pin 3int xPin = 0;int yPin = 1;int xMove = 0;int yMove = 0;// outside leads to ground and +5Vint valPush = HIGH; // variable to store the value readint valX = 0;int valY = 0;void setup(){pinMode(pushPin,INPUT);Serial.begin(9600); // setup serialdigitalWrite(pushPin,HIGH);}void loop(){valX = analogRead(xPin); // read the x input pinvalY = analogRead(yPin); // read the y input pinvalPush = digitalRead(pushPin); // read the push button input pinSerial.println(String(valX) + " " + String(valY) + " " + valPush); //output to Java program}

Step 4: Setting Up Java Program

Now that the Uno is set up, we need to connect it to my Java program which is capable of taking the Uno's serial output values with the special library RxTx and moving the mouse with the library collection JNA. Both of these libraries are included for download at the end of this step. Please note that the only part of the code I changed from the example RxTx was adding the method that moves the mouse in a way that I calibrated for my joystick. It's a bit crude, but it served my purposes.

I used BlueJ as my IDE, but whichever Java IDE you use, install RxTx and JNA libraries for this project, which I named "Mouse". Once that's done, created a project and include this code:

import java.awt.*;
import java.awt.event.InputEvent;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.OutputStream;import gnu.io.CommPortIdentifier; import gnu.io.SerialPort;import gnu.io.SerialPortEvent; import gnu.io.SerialPortEventListener; import java.util.Enumeration;public class Mouse implements SerialPortEventListener { SerialPort serialPort; /** The port we're normally going to use. */ private static final String PORT_NAMES[] = { "/dev/tty.usbserial-A9007UX1", // Mac OS X "/dev/ttyACM0", // Raspberry Pi "/dev/ttyUSB0", // Linux "COM4", // Windows**********(I changed) }; /** * A BufferedReader which will be fed by a InputStreamReader * converting the bytes into characters * making the displayed results codepage independent */ private BufferedReader input; /** The output stream to the port */ private OutputStream output; /** Milliseconds to block while waiting for port open */ private static final int TIME_OUT = 2000; /** Default bits per second for COM port. */ private static final int DATA_RATE = 9600; int buttonOld = 1; public void initialize() { // the next line is for Raspberry Pi and // gets us into the while loop and was suggested here was suggested http://www.raspberrypi.org/phpBB3/viewtopic.php?f... //System.setProperty("gnu.io.rxtx.SerialPorts", "/dev/ttyACM0"); I got rid of this CommPortIdentifier portId = null; Enumeration portEnum = CommPortIdentifier.getPortIdentifiers(); //First, Find an instance of serial port as set in PORT_NAMES. while (portEnum.hasMoreElements()) { CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement(); for (String portName : PORT_NAMES) { if (currPortId.getName().equals(portName)) { portId = currPortId; break; } } } if (portId == null) { System.out.println("Could not find COM port."); return; } try { // open serial port, and use class name for the appName. serialPort = (SerialPort) portId.open(this.getClass().getName(), TIME_OUT); // set port parameters serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); // open the streams input = new BufferedReader(new InputStreamReader(serialPort.getInputStream())); output = serialPort.getOutputStream(); // add event listeners serialPort.addEventListener(this); serialPort.notifyOnDataAvailable(true); } catch (Exception e) { System.err.println(e.toString()); } } /** * This should be called when you stop using the port. * This will prevent port locking on platforms like Linux. */ public synchronized void close() { if (serialPort != null) { serialPort.removeEventListener(); serialPort.close(); } } /** * Handle an event on the serial port. Read the data and print it. In this case, it calls the mouseMove method. */ public synchronized void serialEvent(SerialPortEvent oEvent) { if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { String inputLine=input.readLine(); mouseMove(inputLine); System.out.println("********************"); //System.out.println(inputLine); } catch (Exception e) { System.err.println(e.toString()); } } // Ignore all the other eventTypes, but you should consider the other ones. }public static void main(String[] args) throws Exception { Mouse main = new Mouse(); main.initialize(); Thread t=new Thread() { public void run() { //the following line will keep this app alive for 1000 seconds, //waiting for events to occur and responding to them (printing incoming messages to console). try {Thread.sleep(1000000);} catch (InterruptedException ie) {} } }; t.start(); System.out.println("Started"); } // My method mouseMove, takes in a string containing the three data points and operates the mouse in turn public void mouseMove(String data) throws AWTException { int index1 = data.indexOf(" ", 0); int index2 = data.indexOf(" ", index1+1); int yCord = Integer.valueOf(data.substring(0, index1)); int xCord = Integer.valueOf(data.substring(index1 + 1 , index2)); int button = Integer.valueOf(data.substring(index2 + 1)); Robot robot = new Robot(); int mouseY = MouseInfo.getPointerInfo().getLocation().y; int mouseX = MouseInfo.getPointerInfo().getLocation().x; if (button == 0) { if (buttonOld == 1) { robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); robot.delay(10); } } else { if (buttonOld == 0) robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); } if (Math.abs(xCord - 500) > 5) mouseX = mouseX + (int)((500 - xCord) * 0.02); if (Math.abs(yCord - 500) > 5) mouseY = mouseY - (int)((500 - yCord) * 0.02); robot.mouseMove(mouseX, mouseY); buttonOld = button; System.out.println(xCord + ":" + yCord + ":" + button + ":" + mouseX + ":" + mouseY); return; }}

Attachments

  • jna-3.2.7-sources.zip

    Download

  • mfz-rxtx-2.2-20081207-win-x86.zip

    Download

Step 5: Troubleshooting

Getting the Java program to work may be difficult. I've got some tips if you're stuck:

-Change the "Com4" string in the PORT_NAMES[] to the port your arduino Uno is connected to. (I changed to Com4 from the default Com3 in my Java program)

-Comment out the line relating to Raspberry Pi (if you copied my program, I already did this)

-Click "Rebuild Package" or your IDEs equivalent

-Reset the Java Virtual Machine in your IDE. Maybe even reset the program before using the mouse the first time.

Step 6: Conclusion

I hope this project works for you and that you can improve upon it. Ultimately, the easiest solution is to use an Arduino Leonard or Mini that can function as a system device for mouse inputs, but I found it fun to make the Uno function in a way it was not designed--a mouse--by using my limited Java knowledge.

I learned a lot alone the way and hope to add several features in the future:

-Right Click button. The joystick has one button which I reserved for the left click.

-Real device driver for this project. I'm not sure if this is possible, maybe someone can enlighten me on the subject!

Thanks for reading!

PC Mouse Made With Arduino Uno and Joystick (2024)

References

Top Articles
Molly Tarlov | Bio, Age, Net Worth, Relationship, Career
Unraveling The Dynamics: Brian McKenna, The Husband Of Jessica Tarlov
Star Wars Mongol Heleer
Ups Customer Center Locations
Skyward Sinton
Wordscapes Level 6030
Missed Connections Inland Empire
Gabriel Kuhn Y Daniel Perry Video
Driving Directions To Fedex
Is Sportsurge Safe and Legal in 2024? Any Alternatives?
Owatc Canvas
Ladyva Is She Married
Wnem Radar
Regal Stone Pokemon Gaia
Dumb Money
ocala cars & trucks - by owner - craigslist
Craigslist Edmond Oklahoma
N2O4 Lewis Structure & Characteristics (13 Complete Facts)
"Une héroïne" : les funérailles de Rebecca Cheptegei, athlète olympique immolée par son compagnon | TF1 INFO
Voy Boards Miss America
Byui Calendar Fall 2023
Moving Sales Craigslist
Satisfactory: How to Make Efficient Factories (Tips, Tricks, & Strategies)
The EyeDoctors Optometrists, 1835 NW Topeka Blvd, Topeka, KS 66608, US - MapQuest
LCS Saturday: Both Phillies and Astros one game from World Series
Bocca Richboro
Meridian Owners Forum
Blackboard Login Pjc
The Eight of Cups Tarot Card Meaning - The Ultimate Guide
When His Eyes Opened Chapter 3123
Creed 3 Showtimes Near Island 16 Cinema De Lux
Criglist Miami
Pioneer Library Overdrive
Rush County Busted Newspaper
Transformers Movie Wiki
Current Time In Maryland
Vlocity Clm
Tamilrockers Movies 2023 Download
Family Fare Ad Allendale Mi
Mandy Rose - WWE News, Rumors, & Updates
Cox Outage in Bentonville, Arkansas
Rage Of Harrogath Bugged
Www Craigslist Com Brooklyn
Craigslist En Brownsville Texas
Craigslist Rooms For Rent In San Fernando Valley
Top 1,000 Girl Names for Your Baby Girl in 2024 | Pampers
Sc Pick 3 Past 30 Days Midday
Rubmaps H
Charlotte North Carolina Craigslist Pets
Les BABAS EXOTIQUES façon Amaury Guichon
Ravenna Greataxe
Qvc Com Blogs
Latest Posts
Article information

Author: Delena Feil

Last Updated:

Views: 6092

Rating: 4.4 / 5 (65 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Delena Feil

Birthday: 1998-08-29

Address: 747 Lubowitz Run, Sidmouth, HI 90646-5543

Phone: +99513241752844

Job: Design Supervisor

Hobby: Digital arts, Lacemaking, Air sports, Running, Scouting, Shooting, Puzzles

Introduction: My name is Delena Feil, I am a clean, splendid, calm, fancy, jolly, bright, faithful person who loves writing and wants to share my knowledge and understanding with you.