這篇文章來(lái)源于DevicePlus.com英語(yǔ)網(wǎng)站的翻譯稿。

本文最初發(fā)布在deviceplus.jp網(wǎng)站上,而后被翻譯成英語(yǔ)。
目錄
前言
電子設(shè)計(jì)步驟
關(guān)于Arduino Pro Micro
使之被識(shí)別為HID
使用操縱桿創(chuàng)建鼠標(biāo)設(shè)備
結(jié)論
相關(guān)文章
前言
本文中,我將介紹一種不一樣的Arduino使用方式。乍一看,照片中的Arduino看起來(lái)像我們之前系列中使用過(guò)的Arduino Pro Mini,但其實(shí)這是另一種Arduino。它被稱為“Arduino Pro Micro”。雖然“Mini”變成了“Micro”,尺寸卻并沒(méi)有發(fā)生任何變化,因此,兩者有點(diǎn)難以區(qū)分。這種Arduino在連接到電腦時(shí)會(huì)被識(shí)別為鼠標(biāo)或鍵盤等HID設(shè)備。
電子設(shè)計(jì)步驟
預(yù)計(jì)完成時(shí)間:60分鐘
所需元器件
Arduino主機(jī)(Arduino Pro Micro)
面包板
雙軸操縱桿模塊#27800
輕觸開(kāi)關(guān)
220Ω 電阻
1. 關(guān)于Arduino Pro Micro
Arduino Pro Micro是一種Arduino,配備有名為“ATmega32U4”的芯片(UNO等配有ATmega328P等)。該芯片最大的特點(diǎn)是當(dāng)通過(guò)USB連接時(shí)會(huì)偽裝成鍵盤和鼠標(biāo)等人機(jī)接口設(shè)備(HID)。配備ATmega32U4的Arduino除了“Pro Micro”之外,還被稱為“Arduino Leonardo”,是非常有名的開(kāi)發(fā)板。
在編寫(xiě)程序時(shí),您可以選擇名為“Arduino Leonardo.”的開(kāi)發(fā)板。

乍一看,Arduino Pro Mini與Arduino Pro Micro的外觀非常相似。
但是,Pro Micro具有可以連接到智能手機(jī)等設(shè)備的USB連接器,而Pro Mini只有一個(gè)串行連接器。
2. 使之被識(shí)別為HID
現(xiàn)在,我們讓外觀相似的Arduino Pro Micro讀取示例程序并嘗試讓電腦將其識(shí)別為HID。
嘗試運(yùn)行Arduino IDE的“File”-“Sketch Example”-“09.USB”-“Keyboard”-“KeyboardMessage”程序。
在這個(gè)程序中,我們創(chuàng)建一個(gè)在引腳4上設(shè)有開(kāi)關(guān)的簡(jiǎn)單電路,當(dāng)引腳4被按下時(shí),應(yīng)通過(guò)鍵盤輸入顯示按下的次數(shù)。
(這次,我將引腳4改換為引腳7)

#include "Keyboard.h" const int buttonPin = 7; // input pin for pushbutton int previousButtonState = HIGH; // for checking the state of a pushButton int counter = 0; // button push counter void setup() { // make the pushButton pin an input: pinMode(buttonPin, INPUT); // initialize control over the keyboard: Keyboard.begin(); } void loop() { // read the pushbutton: int buttonState = digitalRead(buttonPin); // if the button state has changed, if ((buttonState != previousButtonState) // and it's currently pressed: && (buttonState == HIGH)) { // increment the button counter counter++; // type out a message Keyboard.print("You pressed the button "); Keyboard.print(counter); Keyboard.println(" times."); } // save the current button state for comparison next time: previousButtonState = buttonState; }

編寫(xiě)程序并打開(kāi)記事本后,無(wú)需觸碰鍵盤,每按一次按鈕,就會(huì)按照上面的描述進(jìn)行計(jì)數(shù)。
如果可以如此輕松地制作USB設(shè)備,那么就可以實(shí)現(xiàn)更多夢(mèng)想!
3. 使用操縱桿創(chuàng)建鼠標(biāo)設(shè)備
我們已經(jīng)知道Arduino Pro Micro可以用作HID,下面我想通過(guò)將它與其他一些元器件組合來(lái)創(chuàng)建鼠標(biāo)設(shè)備。這一次,我將使用曾經(jīng)在無(wú)線電控制設(shè)備制作中使用過(guò)的操縱桿,并嘗試創(chuàng)建一個(gè)可以用操縱桿和輕觸開(kāi)關(guān)來(lái)代替鼠標(biāo)的設(shè)備。
首先,準(zhǔn)備一個(gè)可用于設(shè)置操縱桿方向的程序。


將電路添加到之前的輕觸開(kāi)關(guān)電路中。將操縱桿和后面要使用的LED連接到引腳2。
Code Example
const int _UDPIN = A0; // UD Input
const int _LRPIN = A1; // LR Input
const int _SWPIN = 7; // Digital Pin
int _UD = 0; // Value for Up/Down
int _LR = 0; // Value for Left/Right
void setup() {
Serial.begin(9600);
pinMode(_SWPIN,INPUT) ;
}
void loop() {
_UD = analogRead(_UDPIN);
_LR = analogRead(_LRPIN);
Serial.print("UP-DOWN:");
Serial.print(_UD, DEC);
Serial.print(" - Left-Rright:");
Serial.println(_LR, DEC);
if (digitalRead(_SWPIN) == HIGH) {
Serial.println("switch on");
}
delay(100);
}

經(jīng)過(guò)確認(rèn),可以知道它讀取了程序,轉(zhuǎn)動(dòng)操縱桿時(shí)數(shù)字會(huì)發(fā)生變化。
接下來(lái),讓我們將操縱桿數(shù)字值轉(zhuǎn)換為鼠標(biāo)坐標(biāo)。實(shí)際上,這個(gè)程序也是已經(jīng)備好的示例程序,所以讓我們來(lái)用用看。請(qǐng)選擇“File”-“Sketch Example”-“09.USB”-“Mouse”-“JoystickMouseControl”。
執(zhí)行此程序時(shí),會(huì)將上下(模擬引腳A2)和左右(模擬引腳A1)的值反映在鼠標(biāo)坐標(biāo)上。此外,由于引腳2通過(guò)接入5V電源來(lái)實(shí)現(xiàn)開(kāi)關(guān)功能的,因此可以通過(guò)將引腳2與VCC相連或?qū)㈤_(kāi)關(guān)夾在中間的方式來(lái)打開(kāi)/關(guān)閉設(shè)備。
Code Example
#include "Mouse.h" // set pin numbers for switch, joystick axes, and LED: const int switchPin = 5; // switch to turn on and off mouse control const int mouseButton = 7; // input pin for the mouse pushButton const int xAxis = A1; // joystick X axis const int yAxis = A2; // joystick Y axis const int ledPin = 2; // Mouse control LED // parameters for reading the joystick: int range = 12; // output range of X or Y movement int responseDelay = 5; // response delay of the mouse, in ms int threshold = range / 4; // resting threshold int center = range / 2; // resting position value boolean mouseIsActive = false; // whether or not to control the mouse int lastSwitchState = LOW; // previous switch state void setup() { pinMode(switchPin, INPUT); // the switch pin pinMode(ledPin, OUTPUT); // the LED pin // take control of the mouse: Mouse.begin(); } void loop() { // read the switch: int switchState = digitalRead(switchPin); // if it's changed and it's high, toggle the mouse state: if (switchState != lastSwitchState) { if (switchState == HIGH) { mouseIsActive = !mouseIsActive; // turn on LED to indicate mouse state: digitalWrite(ledPin, mouseIsActive); } } // save switch state for next comparison: lastSwitchState = switchState; // read and scale the two axes: int xReading = readAxis(A0); int yReading = readAxis(A1); // if the mouse control state is active, move the mouse: if (mouseIsActive) { Mouse.move(xReading, yReading, 0); } // read the mouse button and click or not click: // if the mouse button is pressed: if (digitalRead(mouseButton) == HIGH) { // if the mouse is not pressed, press it: if (!Mouse.isPressed(MOUSE_LEFT)) { Mouse.press(MOUSE_LEFT); } } // else the mouse button is not pressed: else { // if the mouse is pressed, release it: if (Mouse.isPressed(MOUSE_LEFT)) { Mouse.release(MOUSE_LEFT); } } delay(responseDelay); } /* reads an axis (0 or 1 for x or y) and scales the analog input range to a range from 0 to */ int readAxis(int thisAxis) { // read the analog input: int reading = analogRead(thisAxis); // map the reading from the analog input range to the output range: reading = map(reading, 0, 1023, 0, range); // if the output reading is outside from the // rest position threshold, use it: int distance = reading - center; if (abs(distance) < threshold) { distance = 0; } // return the distance for this axis: return distance; }
完成編程后,我們來(lái)嘗試讓它動(dòng)起來(lái)。
哦,它真的動(dòng)起來(lái)了!
結(jié)論
這次,我們學(xué)習(xí)了使用Arduino Pro Micro創(chuàng)建基于Arduino的USB設(shè)備時(shí)的基本流程。在下一篇文章中,我們將進(jìn)一步深化應(yīng)用Arduino Pro Micro,嘗試創(chuàng)建更具“Device Plus”風(fēng)格的USB設(shè)備,讓項(xiàng)目更具挑戰(zhàn)性!
審核編輯:湯梓紅
-
usb
+關(guān)注
關(guān)注
60文章
8481瀏覽量
286111 -
HID
+關(guān)注
關(guān)注
2文章
142瀏覽量
49043 -
Arduino
+關(guān)注
關(guān)注
190文章
6527瀏覽量
197524
發(fā)布評(píng)論請(qǐng)先 登錄
怎樣制作一個(gè)基于Arduino Pro Micro與ADXL345的陀螺儀體感鼠標(biāo)呢
制作基于Arduino的多功能電能表
使用Arduino pro micro的USB墊
使用arduino pro micro制作一個(gè)游戲控制器
分享一個(gè)不錯(cuò)的使用Arduino Leonardo PC音量控制的項(xiàng)目
Arduino_1.5.5_軟件下載
Arduino_1.5.6_軟件下載
怎樣用Arduinopromicro將電腦觸控板轉(zhuǎn)換為USB設(shè)備
怎樣將USB游戲控制器添加到Arduino Leonardo / Micro
如何對(duì)便宜又緊湊的Arduino Pro Mini進(jìn)行編程
使用Arduino pro micro板的USB宏墊
使用Arduino Leonardo和紅外傳感器制作手勢(shì)控制設(shè)備
用Raspberry Pi和Arduino Micro制作的虛擬窺視孔
可以用Arduino來(lái)制作USB設(shè)備嗎?嘗試通過(guò)Arduino Pro Micro(Leonardo)使用HID功能
評(píng)論