在线观看www成人影院-在线观看www日本免费网站-在线观看www视频-在线观看操-欧美18在线-欧美1级

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

聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • 鼠標
    +關注

    關注

    6

    文章

    591

    瀏覽量

    39915
  • Arduino
    +關注

    關注

    188

    文章

    6477

    瀏覽量

    187834
收藏 人收藏

    評論

    相關推薦

    支撐座對設備性能有哪些影響?

    支撐座采用高品質鋼材制作,具有高強度和剛度,能夠為絲提供可靠的支撐,有效避免絲在運轉過程中出現彎曲和扭曲現象。
    的頭像 發表于 01-16 17:48 ?123次閱讀
    絲<b class='flag-5'>桿</b>支撐座對設備性能有哪些影響?

    自己設計的Arduino UNO R3主控板原理圖+PCB源文件(可直接打樣)

    一年前做的Arduino UNO r3,開源精神,把自己這塊板子奉獻給大家,板子沒有什么問題,可以直接打樣制作,原理圖文件都表明了型號。不用謝~ Arduino
    發表于 12-10 10:14

    采用霍爾效應傳感器的游戲手柄和控制設計

    電子發燒友網站提供《采用霍爾效應傳感器的游戲手柄和控制設計.pdf》資料免費下載
    發表于 10-30 09:56 ?0次下載
    采用霍爾效應傳感器的<b class='flag-5'>游戲</b>手柄和控制<b class='flag-5'>桿</b>設計

    怎樣用Arduino測試鋰電池容量

    本文詳細介紹了如何用Arduino測量鋰電池的容量。并附有電路圖和Arduino的程序代碼。
    的頭像 發表于 07-30 09:14 ?1043次閱讀
    怎樣用<b class='flag-5'>Arduino</b>測試鋰電池容量

    ESP32的程序是不是和uno r3一樣都是以插入就開始運行?

    大神們好,小弟一直在用Arduino Uno R3來運行hex編好了的程序,但是uno r3的容量實在是太少了,所以想問問ESP32可以代替Uno r3嗎? 還有,如果我有幾個hex程
    發表于 06-25 07:56

    S2GO_3D_TLE493DW2B6-A0無法與Arduino UNO一起工作是怎么回事?

    我想使用 TLE493D-W2B6 為 Youtube Maker 項目制作一個觸發控制裝置。 我有一塊 TLE493DW2B6 shield2go 板,我將它連接到 Arduino 上 我安裝
    發表于 05-28 07:03

    放下你手中的游戲鼠標 | 小白測功耗

    上一期我們測試了合宙辦公室常用的辦公鼠標,這一期我們測試游戲鼠標!小白對游戲鼠標的刻板印象:發光/狂拽酷炫但這次選的兩款熱門
    的頭像 發表于 05-13 17:09 ?1728次閱讀
    放下你手中的<b class='flag-5'>游戲</b><b class='flag-5'>鼠標</b> | 小白測功耗

    何用Arduino開發STM32G070?

    何用Arduino開發STM32G070,各位大神有相關教程嗎。我如何在Arduino的開發板管理器中添加STM32G070開發板,如何把程序下載到CPU中?
    發表于 04-07 08:22

    何用Arduino制作一個簡易自動喂魚器

    。 這個裝置幾乎可以安裝在任何類型的魚缸上,飼養裝置的尺寸也可以根據需要擴大。 說了這么多,讓我們開始看看制作這個飼養器需要哪些材料。 材料準備 步進電機 Arduino Uno/nano 步進電機驅動器
    發表于 03-28 11:25

    請問stm32H743II usb HOST如何識別雙遙游戲手柄?

    stm32H743II usb HOST 如何識別 雙遙游戲手柄?北通usb游戲手柄插到PC上顯示是XBOX 360手柄,手柄上傳為14個字節數據,分別為0-7兩個遙
    發表于 03-15 07:52

    全球游戲硬件市場將在未來十年持續增長

    該報告還稱,電子競技興起、消費者可支配收入提升及對仿真度高的游戲體驗追求是驅動游戲硬件行業增長主要原因。具體產品方面,游戲機、專業游戲PC
    的頭像 發表于 03-14 15:10 ?696次閱讀

    如何制作自己的Arduino電容計

    在這個項目中,您將學習如何制作自己的Arduino電容計(測量電容器的值,范圍從pF到1000的uF)。一般來說,電子愛好者喜歡設計自己的小工具而不是購買。在這個項目中,我們使用兩種電容測量方法,即
    的頭像 發表于 02-25 15:10 ?1732次閱讀
    如何<b class='flag-5'>制作</b>自己的<b class='flag-5'>Arduino</b>電容計

    如何使用Arduino制作智能垃圾箱

    在這個項目中,我將向您展示如何使用Arduino制作智能垃圾箱,當您帶著垃圾接近時,垃圾箱的蓋子會自動打開。
    的頭像 發表于 02-11 12:22 ?3226次閱讀
    如何使用<b class='flag-5'>Arduino</b><b class='flag-5'>制作</b>智能垃圾箱

    如何使用Arduino UNO板和電位器控制伺服電機

    在本Arduino伺服電機教程中,您將學習如何使用Arduino UNO板和電位器控制伺服電機。
    的頭像 發表于 02-11 10:11 ?2985次閱讀
    如何使用<b class='flag-5'>Arduino</b> <b class='flag-5'>UNO</b>板和電位器控制伺服電機

    如何使用Arduino UNO和TIP120晶體管驅動和控制直流電機的速度

    在本 Arduino 電機指南中,您將學習如何使用 Arduino UNO 和 TIP120晶體管驅動和控制直流電機的速度。在此示例中,您將使用按鈕來提高電機速度,然后減慢速度,這要歸功于脈寬調制 (PWM) 的強大功能。
    的頭像 發表于 02-11 10:08 ?1658次閱讀
    如何使用<b class='flag-5'>Arduino</b> <b class='flag-5'>UNO</b>和TIP120晶體管驅動和控制直流電機的速度
    主站蜘蛛池模板: 夜夜操操 | 欧美成人aaaa免费高清 | 色老二精品视频在线观看 | 日韩在线一区视频 | 日韩一级在线观看 | 国产成人午夜精品影院游乐网 | 中文在线最新版天堂 | 午夜免费成人 | 欧美色欧美亚洲高清在线观看 | 被暗卫肉高h | 成人网男女啪啪免费网站 | 在线91精品亚洲网站精品成人 | 免费看一级片 | 思思久久好好热精品国产 | 18毛片| 亚洲视频在线一区 | 狠狠色丁香婷婷综合久久片 | 日本一区二区不卡在线 | 免费精品视频在线 | 在线黄色网 | 欧美视频一区在线观看 | 久久亚洲国产视频 | 国产精品黄页网站在线播放免费 | 欧美 亚洲 国产 精品有声 | 色视频在线免费看 | 伊人精品久久久大香线蕉99 | 日本福利片午夜免费观着 | 香蕉久久夜色精品国产2020 | 国产精品特黄毛片 | 成人免费播放视频777777 | 老师下面很湿很爽很紧 | 神马电影天堂网 | 精品国产中文一级毛片在线看 | 一级片免费看 | 亚洲精品在线视频观看 | 午夜三级国产精品理论三级 | 免费人成黄页在线观看日本 | 色五阁| 爽天天天天天天天 | 天堂自拍 | 中文字幕在线观看第一页 |