Arduino Function: Mouse.move


This function moves the cursor on a connected computer. The motion onscreen is always relative to the cursor's current location. Use this function after call to begin().


Mouse.move(xVal, yPos, wheel);


  • xVal: amount to move along the x-axis - signed char
  • yVal: amount to move along the y-axis - signed char
  • wheel: amount to move scroll wheel - signed char




When you use the Mouse.move() command, the Arduino takes over your mouse! Make sure you have control before you use the command. A pushbutton to toggle the mouse control state is effective.


//analog sensor for X axis
const int xAxis = A1;

// analog sensor for Y axis
const int yAxis = A2;

// output range of X or Y movement
int range = 12;

// response delay of the mouse, in ms
int responseDelay = 2;

// resting threshold
int threshold = range/4;

// resting position value
int center = range/2;

// actual analogRead minima for {x, y}
int minima[] = { 1023, 1023};

// actual analogRead maxima for {x, y}
int maxima[] = { 0,0};

// pin numbers for {x, y}
int axis[] = { xAxis, yAxis};

// final mouse readings for {x, y}
int mouseReading[2];

void setup() {

void loop() {
// read and scale the two axes:
int xReading = readAxis(0);
int yReading = readAxis(1);

// move the mouse:
Mouse.move(xReading, yReading, 0);

/* 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 axisNumber) {
// distance from center of the output range
int distance = 0;

// read the analog input:
int reading = analogRead(axis[axisNumber]);

// if the current reading exceeds the max or min for this axis,
// reset the max or min:
if (reading < minima[axisNumber]) {
minima[axisNumber] = reading;
if (reading > maxima[axisNumber]) {
maxima[axisNumber] = reading;

// map the reading from the analog input range to the output range:
reading = map(reading, minima[axisNumber], maxima[axisNumber], 0, range);

// if the output reading is outside from the // rest position threshold, use it:
if (abs(reading - center) > threshold) {
dsistance = (reading - center);

// the Y axis needs to be inverted in order to // map the movemment correctly:
if (axisNumber == 1) {
distance = -distance;
// return the distance for this axis: return distance;

Based on the reference originally written by Arduino Community, and