3D mouse joystick and Mouse.move() issue

Hi Guys,

I’m Working on making a 3D mouse using the Leonardo’s Mouse function and an analog joystick. The cad software I use (solidworks) you hold the middle button down and drag the mouse to rotate your object and release when done. I found it annoying that the pointer did not go back to the point where it was when I started to rotate the part so, I added some code to keep track of the total x and y displacement and move the pointer the inverse of those displacements when I was done rotating so the pointer would return the the starting position. However it’s not, it seems to move a random amount and Im not sure why. hopefully someone could look at my code and tell me why? Thank you.

#define Liy A2
#define Lix A3
#define lPress 10

int inputx = 0;
int inputy = 0;
int mappedx = 0;
int mappedy = 0;
long int tslm = 0;

int dx = 0;
int dy = 0;

boolean lbState = false;
boolean mp = false;

void setup() {
  while (millis()< 5000){
    //do nothing
  Serial.println("Mouse Active");



void loop() {
  inputx = analogRead(Lix); // read position of joystick in the X direction
  inputy = analogRead(Liy); // read position of joystick in the y direction
  lbState = digitalRead(lPress); // read state of button

  if((inputx>445 || inputx<425 || inputy>450 || inputy<435) && lbState){
    if(mp == false){// if middle mouse button is not pressed then press it
      mp = true;
    mappedx = map(inputx,0,900,-10,10); //scale the analog read to negative and positive step rates making the difference between the final values larger increases speed sensitivity
    mappedy = map(inputy,0,900,-10,10);
    Mouse.move(mappedx,mappedy,0);//move mouse in x and y direction
    dx = dx+mappedx; //record displacment of mouse in x direction
    dy = dy+mappedy; //record displacement of mouse in y direction
    tslm = millis();// set time of last movement

  /*  if(lbState == false && (inputy>500 || inputy<440)){
   mappedy = map(inputy,0,900,-1,1);
   delay(10); // zoom is very sensitive

    if(millis()-tslm>100){ // release middle mouse button if movement has not occurred in 100 milliseconds
      mp = false;
      Mouse.move(-dx,-dy,0); // move mouse back to starting position
      dx = 0; //reset x displacement
      dy = 0; //reset y displacement



However it's not, it seems to move a random amount and Im not sure why. hopefully someone could look at my code and tell me why?

The mouse movements are all relative. The OS takes the relative motion, the mouse starting position, the mouse resolution, the screen resolution, etc. into account to create an absolute ending position. It will be exceedingly difficult for you to reverse engineer the process to create a relative movement that results in a specific absolute location.

Thanks Pauls, I see what you mean its based on velocity and position and would be hard to get an equation to return the pointer back to the start point. since this was the case i figured having the pointer return to the center of my drawing view was the second best case so I used a for loop to increment the pointer over to the top right corner of the screen so that I always have a standard home position then another for loop to get the pointer back into the center of my screen.

// home pointer to upper right hand corner
for(int f=0;f<120;f++){

//move to desiredposition
for (int f = 0;f<55;f++){