第1步:材料
這個項目不需要很多材料:
1 Arduino Uno
5對公對母線
5對母對母線(連接到操縱桿模塊并添加操縱桿的延伸長度。
1個操縱桿(我使用了SainSmart PS2游戲桿模塊,并且會推薦它)
步驟2:設置Arduino Uno
Uno的設置可以在材料圖片,以及這里的說明:
將五根母頭線連接到操縱桿模塊的引腳上。現在,將五根公頭線連接到母線的末端并將它們連接到這樣的Arduino:
1.操縱桿上的地面到Arduino Gnd
2.操縱桿上的+ 5V到Arduino 5V
3。操縱桿上的UPx到Arduino上的A0
4.操縱桿上的UPy到A1
5. SW引腳(數字式點擊開關)到數字引腳7上Arduino
第3步:上傳Joysti ck程序到Arduino
將Uno連接到你的PC并上傳這里看到的操縱桿代碼(請注意我最初沒有創建這個代碼):
int pushPin = 7; // potentiometer wiper (middle terminal) connected to analog pin 3
int xPin = 0;
int yPin = 1;
int xMove = 0;
int yMove = 0;
// outside leads to ground and +5V
int valPush = HIGH; // variable to store the value read
int valX = 0;
int valY = 0;
void setup()
{
pinMode(pushPin,INPUT);
Serial.begin(9600); // setup serial
digitalWrite(pushPin,HIGH);
}
void loop()
{
valX = analogRead(xPin); // read the x input pin
valY = analogRead(yPin); // read the y input pin
valPush = digitalRead(pushPin); // read the push button input pin
Serial.println(String(valX) + “ ” + String(valY) + “ ” + valPush); //output to Java program
}
步驟4:設置Java程序
現在已經設置了Uno,我們需要將它連接到我的Java程序,該程序能夠獲取Uno的串行輸出值特殊庫RxTx并使用庫集合JNA移動鼠標。這兩個庫都包含在此步驟結束時供下載。請注意,我從示例RxTx中更改的代碼的唯一部分是添加了以我為操縱桿校準的方式移動鼠標的方法。它有點粗糙,但它符合我的目的。
我使用BlueJ作為我的IDE,但無論你使用哪種Java IDE,都要為這個項目安裝RxTx和JNA庫,我將其命名為“Mouse”。完成后,創建一個項目并包含以下代碼:
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;
}
}
步驟5:疑難解答
使Java程序正常工作可能是難。如果您遇到困難我會得到一些提示:
- 將PORT_NAMES []中的“Com4”字符串更改為您的arduino Uno所連接的端口。 (我從Java程序中的默認Com3更改為Com4)
- 指出與Raspberry Pi相關的行(如果你復制了我的程序,我已經這樣做了)
- 單擊“重建軟件包”或等效的IDE
-在IDE中重置Java虛擬機。甚至可能在第一次使用鼠標之前重置程序。
第6步:結論
我希望這個項目適用于您,并且您可以改善它。最終,最簡單的解決方案是使用Arduino Leonard或Mini作為鼠標輸入的系統設備,但我發現使Uno功能以非設計的方式 - 鼠標 - 通過使用我的方式很有趣有限的Java知識。
我獨自學習了很多方法,并希望將來增加一些功能:
-右鍵單擊按鈕。操縱桿有一個我保留左鍵的按鈕。
- 這個項目的實際設備驅動程序。我不確定這是否可行,也許有人可以就此問題給我啟發!
責任編輯:wv
-
鼠標
+關注
關注
6文章
590瀏覽量
39728 -
Arduino
+關注
關注
187文章
6464瀏覽量
186681
發布評論請先 登錄
相關推薦
評論