/*
ButtonMouseControl
For Leonardo and Due boards only.
Controls the mouse from five pushbuttons on an Arduino Leonardo, Micro or Due.
Hardware:
- five 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.
created 15 Mar 2012
modified 27 Mar 2012
by Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/ButtonMouseControl
*/#include"Mouse.h"// set pin numbers for the five buttons:
constintupButton=2;constintdownButton=3;constintleftButton=4;constintrightButton=5;constintmouseButton=6;intrange=5;// output range of X or Y movement; affects movement speed
intresponseDelay=10;// response delay of the mouse, in ms
ボタンを接続するデジタルピンを定義します。
rangeという変数は、マウスを動かす距離を制御します。
setup()
39
40
41
42
43
44
45
46
47
48
49
voidsetup(){// initialize the buttons' inputs:
pinMode(upButton,INPUT);pinMode(downButton,INPUT);pinMode(leftButton,INPUT);pinMode(rightButton,INPUT);pinMode(mouseButton,INPUT);// initialize mouse control:
Mouse.begin();}
voidloop(){// read the buttons:
intupState=digitalRead(upButton);intdownState=digitalRead(downButton);intrightState=digitalRead(rightButton);intleftState=digitalRead(leftButton);intclickState=digitalRead(mouseButton);// calculate the movement distance based on the button states:
intxDistance=(leftState-rightState)*range;intyDistance=(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 the mouse button is not pressed:
else{// if the mouse is pressed, release it:
if(Mouse.isPressed(MOUSE_LEFT)){Mouse.release(MOUSE_LEFT);}}// a delay so the mouse doesn't move too fast:
delay(responseDelay);}