0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

如何用Arduino Uno和游戏杆制作PC鼠标

454398 来源:网络整理 作者:佚名 2019-11-05 17:01 次阅读

第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

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 鼠标
    +关注

    关注

    5

    文章

    576

    浏览量

    39193
  • Arduino
    +关注

    关注

    184

    文章

    6427

    浏览量

    184834
收藏 人收藏

    评论

    相关推荐

    请问stm32H743II usb HOST如何识别双遥游戏手柄?

    stm32H743II usb HOST 如何识别 双遥游戏手柄?北通usb游戏手柄插到PC上显示是XBOX 360手柄,手柄上传为14个字节数据,分别为0-7两个遥
    发表于 03-15 07:52

    如何使用Arduino UNO板和电位器控制伺服电机

    在本Arduino伺服电机教程中,您将学习如何使用Arduino UNO板和电位器控制伺服电机。
    的头像 发表于 02-11 10:11 670次阅读
    如何使用<b class='flag-5'>Arduino</b> <b class='flag-5'>UNO</b>板和电位器控制伺服电机

    Arduino999759 UNO功能是什么?

    Arduino999759UNO 功能是什么?
    发表于 11-01 06:09

    Arduino UNO R3转接板原理图

    电子发烧友网站提供《Arduino UNO R3转接板原理图.pdf》资料免费下载
    发表于 09-15 15:27 18次下载
    <b class='flag-5'>Arduino</b> <b class='flag-5'>UNO</b> R3转接板原理图

    如何将气体放电游戏、气体放电鼠标和气体放电键盘整合到一个USB设备中

    应用程序: 演示如何将气体放电游戏、气体放电鼠标和气体放电键盘整合到一个USB设备中。 BSP 版本: NUC230/240系列 BSP CMSIS V3.01.001 硬件
    发表于 08-23 07:18

    stm32H743II usb HOST如何识别双遥游戏手柄?

    stm32H743II usb HOST 如何识别 双遥游戏手柄?北通usb游戏手柄插到PC上显示是XBOX 360手柄,手柄上传为14个字节数据,分别为0-7两个遥
    发表于 08-09 06:16

    如何使用Arduino Uno和三个LED制作简单的电子蜡烛?

    在本文中,工程师展示了如何使用Arduino Uno开发板和三个LED制作简单的电子蜡烛,你可以根据需要增加LED的数量。
    的头像 发表于 07-11 09:49 1108次阅读
    如何使用<b class='flag-5'>Arduino</b> <b class='flag-5'>Uno</b>和三个LED<b class='flag-5'>制作</b>简单的电子蜡烛?

    ARDUINO UNO案例之乐高积木盒

    电子发烧友网站提供《ARDUINO UNO案例之乐高积木盒.zip》资料免费下载
    发表于 07-10 15:16 0次下载
    <b class='flag-5'>ARDUINO</b> <b class='flag-5'>UNO</b>案例之乐高积木盒

    基于Arduino Uno的步进音序器开源分享

    步进音序器扩展板是Artis Lab在 2016 年春季制作的第一个项目。该板是 Arduino Uno 的扩展,可实现产生 8 位音调序列的六级步进音序器。
    发表于 07-07 16:15 0次下载
    基于<b class='flag-5'>Arduino</b> <b class='flag-5'>Uno</b>的步进音序器开源分享

    用于PC/笔记本电脑的Arduino uno RFID安全系统

    电子发烧友网站提供《用于PC/笔记本电脑的Arduino uno RFID安全系统.zip》资料免费下载
    发表于 07-05 10:52 0次下载
    用于<b class='flag-5'>PC</b>/笔记本电脑的<b class='flag-5'>Arduino</b> <b class='flag-5'>uno</b> RFID安全系统

    最小的Arduino Uno

    电子发烧友网站提供《最小的Arduino Uno.zip》资料免费下载
    发表于 07-04 10:51 0次下载
    最小的<b class='flag-5'>Arduino</b> <b class='flag-5'>Uno</b>

    arduino uno制作暗/亮电平表

    电子发烧友网站提供《用arduino uno制作暗/亮电平表.zip》资料免费下载
    发表于 07-04 09:35 0次下载
    用<b class='flag-5'>arduino</b> <b class='flag-5'>uno</b><b class='flag-5'>制作</b>暗/亮电平表

    Arduino UNO迷你气象站

    电子发烧友网站提供《Arduino UNO迷你气象站.zip》资料免费下载
    发表于 06-16 10:24 0次下载
    <b class='flag-5'>Arduino</b> <b class='flag-5'>UNO</b>迷你气象站

    Arduino Uno制作的四轴飞行器

    电子发烧友网站提供《Arduino Uno制作的四轴飞行器.zip》资料免费下载
    发表于 06-15 09:26 4次下载
    <b class='flag-5'>Arduino</b> <b class='flag-5'>Uno</b><b class='flag-5'>制作</b>的四轴飞行器

    使用Arduino Uno控制LED矩阵

    电子发烧友网站提供《使用Arduino Uno控制LED矩阵.zip》资料免费下载
    发表于 06-13 16:12 1次下载
    使用<b class='flag-5'>Arduino</b> <b class='flag-5'>Uno</b>控制LED矩阵