#include<Mouse.h>constintxAxis=A1;//analog sensor for X axis
constintyAxis=A2;// analog sensor for Y axis
intrange=12;// output range of X or Y movement
intresponseDelay=2;// response delay of the mouse, in ms
intthreshold=range/4;// resting threshold
intcenter=range/2;// resting position value
intminima[]={1023,1023};// actual analogRead minima for {x, y}
intmaxima[]={0,0};// actual analogRead maxima for {x, y}
intaxis[]={xAxis,yAxis};// pin numbers for {x, y}
intmouseReading[2];// final mouse readings for {x, y}
voidsetup(){Mouse.begin();}voidloop(){// read and scale the two axes:
intxReading=readAxis(0);intyReading=readAxis(1);// move the mouse:
Mouse.move(xReading,yReading,0);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(intaxisNumber){intdistance=0;// distance from center of the output range
// read the analog input:
intreading=analogRead(axis[axisNumber]);// of 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){distance=(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:
returndistance;}