W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
使用鼠標(biāo)庫,你可以使用Arduino Leonardo,Micro或Due來控制計(jì)算機(jī)的屏幕光標(biāo)。
這個(gè)特殊的例子使用五個(gè)按鈕來移動(dòng)屏幕上的光標(biāo)。四個(gè)按鈕是方向性的(上,下,左,右),一個(gè)是用于鼠標(biāo)左鍵單擊。來自Arduino的光標(biāo)移動(dòng)總是相對(duì)的。每次讀取輸入時(shí),光標(biāo)的位置都會(huì)相對(duì)于當(dāng)前位置進(jìn)行更新。
只要有一個(gè)方向按鈕被按下,Arduino就會(huì)移動(dòng)鼠標(biāo),在合適的方向上將HIGH輸入映射到5的范圍。
第五個(gè)按鈕用于控制來自鼠標(biāo)的左鍵單擊。當(dāng)按鈕被釋放時(shí),計(jì)算機(jī)將識(shí)別事件。
你將需要以下組件:
按照電路圖連接面包板上的組件,如下圖所示。
在計(jì)算機(jī)上打開Arduino IDE軟件。使用Arduino語言進(jìn)行編碼控制你的電路。通過單擊“New”打開一個(gè)新的草圖文件。
對(duì)于本例,你需要使用Arduino IDE 1.6.7
/* Button Mouse Control For Leonardo and Due boards only .Controls the mouse from five pushbuttons on an Arduino Leonardo, Micro or Due. Hardware: * 5 pushbuttons attached to D2, D3, D4, D5, D6 The mouse movement is always relative. This sketch reads four pushbuttons, and uses them to set the movement of the mouse. WARNING: When you use the Mouse.move() command, the Arduino takes over your mouse! Make sure you have control before you use the mouse commands. */ #include "Mouse.h" // set pin numbers for the five buttons: const int upButton = 2; const int downButton = 3; const int leftButton = 4; const int rightButton = 5; const int mouseButton = 6; int range = 5; // output range of X or Y movement; affects movement speed int responseDelay = 10; // response delay of the mouse, in ms void setup() { // initialize the buttons' inputs: pinMode(upButton, INPUT); pinMode(downButton, INPUT); pinMode(leftButton, INPUT); pinMode(rightButton, INPUT); pinMode(mouseButton, INPUT); // initialize mouse control: Mouse.begin(); } void loop() { // read the buttons: int upState = digitalRead(upButton); int downState = digitalRead(downButton); int rightState = digitalRead(rightButton); int leftState = digitalRead(leftButton); int clickState = digitalRead(mouseButton); // calculate the movement distance based on the button states: int xDistance = (leftState - rightState) * range; int yDistance = (upState - downState) * range; // if X or Y is non-zero, move: if ((xDistance != 0) || (yDistance != 0)) { Mouse.move(xDistance, yDistance, 0); } // if the mouse button is pressed: if (clickState == HIGH) { // if the mouse is not pressed, press it: if (!Mouse.isPressed(MOUSE_LEFT)) { Mouse.press(MOUSE_LEFT); } } else { // else the mouse button is not pressed: // if the mouse is pressed, release it: if (Mouse.isPressed(MOUSE_LEFT)) { Mouse.release(MOUSE_LEFT); } } // a delay so the mouse does not move too fast: delay(responseDelay); }
使用micro-USB線將電路板連接到計(jì)算機(jī)。按鈕連接到引腳2至6的數(shù)字輸入。確保使用10k下拉電阻。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: