Mouse.move()
Description
Moves the cursor on a connected computer. The motion onscreen is always relative to the cursor’s current location. Before using
Mouse.move()
you must call Mouse.begin()Syntax
Mouse.move(xVal, yVal, wheel)
Parameters
: amount to move along the x-axis. Allowed data types:xVal
.signed char
: amount to move along the y-axis. Allowed data types:yVal
.signed char
: amount to move scroll wheel. Allowed data types:wheel
.signed char
Returns
Nothing
Example Code
1#include <Mouse.h>2
3 const int xAxis = A1; //analog sensor for X axis4 const int yAxis = A2; // analog sensor for Y axis5
6 int range = 12; // output range of X or Y movement7 int responseDelay = 2; // response delay of the mouse, in ms8 int threshold = range / 4; // resting threshold9 int center = range / 2; // resting position value10 int minima[] = {1023, 1023}; // actual analogRead minima for {x, y}11 int maxima[] = {0, 0}; // actual analogRead maxima for {x, y}12 int axis[] = {xAxis, yAxis}; // pin numbers for {x, y}13 int mouseReading[2]; // final mouse readings for {x, y}14
15
16 void setup() {17 Mouse.begin();18 }19
20 void loop() {21 // read and scale the two axes:22 int xReading = readAxis(0);23 int yReading = readAxis(1);24
25 // move the mouse:26 Mouse.move(xReading, yReading, 0);27 delay(responseDelay);28 }29
30 /*31 reads an axis (0 or 1 for x or y) and scales the32 analog input range to a range from 0 to <range>33 */34
35 int readAxis(int axisNumber) {36 int distance = 0; // distance from center of the output range37
38 // read the analog input:39 int reading = analogRead(axis[axisNumber]);40
41 // of the current reading exceeds the max or min for this axis,42 // reset the max or min:43 if (reading < minima[axisNumber]) {44 minima[axisNumber] = reading;45 }46 if (reading > maxima[axisNumber]) {47 maxima[axisNumber] = reading;48 }49
50 // map the reading from the analog input range to the output range:51 reading = map(reading, minima[axisNumber], maxima[axisNumber], 0, range);52
53 // if the output reading is outside from the54 // rest position threshold, use it:55 if (abs(reading - center) > threshold) {56 distance = (reading - center);57 }58
59 // the Y axis needs to be inverted in order to60 // map the movement correctly:61 if (axisNumber == 1) {62 distance = -distance;63 }64
65 // return the distance for this axis:66 return distance;67 }
Notes and Warnings
When you use the
Mouse.move()
command, the Arduino board takes over your mouse! Make sure you have control before you use the command. A pushbutton to toggle the mouse control state is effective.See also
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.