/*
JoystickMouseControl
Controls the mouse from a joystick on an Arduino Leonardo, Micro or Due.
Uses a pushbutton to turn on and off mouse control, and a second pushbutton
to click the left mouse button.
Hardware:
- 2-axis joystick connected to pins A0 and A1
- pushbuttons connected to pin D2 and D3
The mouse movement is always relative. This sketch reads two analog inputs
that range from 0 to 1023 (or less on either end) and translates them into
ranges of -6 to 6.
The sketch assumes that the joystick resting values are around the middle of
the range, but that they vary within a threshold.
WARNING: When you use the Mouse.move() command, the Arduino takes over your
mouse! Make sure you have control before you use the command. This sketch
includes a pushbutton to toggle the mouse control state, so you can turn on
and off mouse control.
created 15 Sep 2011
updated 28 Mar 2012
by Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/JoystickMouseControl
*/#include"Mouse.h"// set pin numbers for switch, joystick axes, and LED:
constintswitchPin=2;// switch to turn on and off mouse control
constintmouseButton=3;// input pin for the mouse pushButton
constintxAxis=A0;// joystick X axis
constintyAxis=A1;// joystick Y axis
constintledPin=5;// Mouse control LED
// parameters for reading the joystick:
intrange=12;// output range of X or Y movement
intresponseDelay=5;// response delay of the mouse, in ms
intthreshold=range/4;// resting threshold
intcenter=range/2;// resting position value
boolmouseIsActive=false;// whether or not to control the mouse
intlastSwitchState=LOW;// previous switch state
さまざまな変数を定義します。
ジョイスティックは、A0とA1に接続します。
setup()
50
51
52
53
54
55
56
voidsetup(){pinMode(switchPin,INPUT);// the switch pin
pinMode(ledPin,OUTPUT);// the LED pin
// take control of the mouse:
Mouse.begin();}
voidloop(){// read the switch:
intswitchState=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:
intxReading=readAxis(A0);intyReading=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 <range>
*/intreadAxis(intthisAxis){// read the analog input:
intreading=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:
intdistance=reading-center;if(abs(distance)<threshold){distance=0;}// return the distance for this axis:
returndistance;}