Arduino Mouse.move() doesn't work properly for some parameters in my function. what is wrong with my code?

On serial monitor i send arduino x and y coordinates for mouse move. it does move to given coordinates. But not for all parameters. for example for x,y=(200, 125), (200, 131), (200,245),(200,251),(200,365), (200,371) it doesn't move to correct place.
(pc resolution: 1920, 1080)
my arduino micro code is like below:

#include <Keyboard.h>
#include <Mouse.h>

int val;

int package_size = 6;          //package format: 0-N-{FNO}-{X}-{Y}-{P}
String package[6]= {"","","","","",""}; 
int key_press_time = 20;
int mouse_press_time = 20;


void setup() {
  Serial.begin(115200);
  // Serial.flush();
  pinMode(LED_BUILTIN, OUTPUT);
  Mouse.begin();  
}


void  loop() {
  String msg;
  if (Serial.available() > 0){
    msg = Serial.readString(); 
  }

  msg.trim(); 
  if(msg.length() >10){
      splitPackage(msg, "-", package);    
      if(package[0] == "0"){          
           int mX = package[3].toInt();
           int mY = package[4].toInt();
           int mod_val = package[5].toInt();
           mouseClick(mX, mY);           
       }
  }
  

 
}

void mouseMoveUpperLeft(int x, int y){
 
  for(int i=0; i<20; i++){
    Mouse.move(-100, -100, 0);
  }
}


void mouseClick(int x, int y){
  mouseMoveUpperLeft(100, 100);
  int val = 120;      // 127, 128
  int n = (int) y/val;
  int m = (int) y % val;
  Serial.println((String)"[X: ____, Y:"+y+"]: div:"+n+", mod: "+m);

  for(int i=0; i<n; i++){
    Mouse.move(0,val,0);  
    Serial.print((String)"mouse_Y move: (0, "+val+")");
    Serial.println("");    
    delay(10);
  }
  delay(20);
  Mouse.move(0,m,0);
  Serial.println((String)"mouse_Y move_mod: (0, "+m+")");
  delay(10);
  
  int t = (int) x/val;
  int k = x % val;
  Serial.println((String)"[X: "+x+", Y:___]: div:"+t+", mod: "+k);
  
  for(int i=0; i<t; i++){
    Mouse.move(val,0,0);
    Serial.println((String)"mouse_X move: ("+val+", 0)");
    delay(10);
  }
  delay(20);
  Mouse.move(k,0,0);
  Serial.println((String)"mouse_X move_mod: ("+k+", 0)");
  Serial.println("-----------------------------------------------------");
  delay(key_press_time);
  Mouse.click();
  Mouse.end();
}

void splitPackage(String str, char ch, String arr[6]){
  int splitter_index = 0;
  int next_splitter_index = str.indexOf("-");

  for(int i=0; i<6;i++){
    arr[i] = str.substring(splitter_index, next_splitter_index); 
    splitter_index = next_splitter_index+1;
    next_splitter_index = str.indexOf("-", next_splitter_index+1);    
  }
  
}

and serial monito output is like below:

and mouse positions on paint are indicated with red dots in the picture below:

I tried changing delay times, it didn't work. i tried arduino micro, it was the same result, i tried linux pc, and it was same again. i couldn't find any answer. why does it not move to correct place?
does anyone know why it acts like that for some parameters of x any?

why do you call

it was not working, so i wanted to try something else. so i included Mouse.end(). But it didn't change anything also.

i changed my mouseClick() function like this:

void mouseClick2(int x, int y){
  mouseMoveUpperLeft(100, 100); 
  int i = 0;
  int j=0;
  int val = 120;
  int div_y = (int) y/val;
  int mod_y = y % val;
  int div_x = (int) x / val;
  int mod_x = x % val;
  Mouse.move(mod_x, mod_y, 0);
  delay(10);
  while(true){      
      if(i>=div_y && j >= div_x){
        break;
      }
      if(div_y >i && div_x >j){
        Mouse.move(val, val,0);
        i= i+1;
        j=j+1;
      }else if (div_y >i){
        Mouse.move(0, val,0);
        i++;
      }else if (div_x >j){
        Mouse.move(val, 0,0);
        j++;
      }
      
      delay(10);
  }
  delay(10);
  Mouse.click();
}

and i sent (x,y) values and increasing y by 6 each iteration. and i got this result on paint.
still not precise both for x and y

can you remove the parsing and just do a simple code where in the setup you send some mouse clicks at different positions ?

#include <Mouse.h>

void setup() {
  Serial.begin(115200);
  delay(1000);
  Serial.println("Sending mouse commands");
  Mouse.begin();  

  for (int y = 100; y < 200; y +=5) {
    Mouse.move(200, y, 0);
    delay(50);
    Mouse.click();
  }

  Mouse.end();
}

void loop() {}

(typed here)

1 Like

i modified your code. relatively move mouse_Y 6 by each iteration. and it works smoothly.

#include <Mouse.h>

void setup() {
  Serial.begin(115200);
  delay(1000);
  Serial.println("Sending mouse commands");
  Mouse.begin();  

  for (int y = 0; y < 200; y ++) {
    Mouse.move(0, 6, 0);
    delay(50);
    Mouse.click();
  }

  Mouse.end();
}

result is like this below:
image

But when i run my function:


void setup() {
  Serial.begin(115200);
  // Serial.flush();
  pinMode(LED_BUILTIN, OUTPUT);
  delay(1000);
  Mouse.begin(); 
  for (int y = 107; y < 389; y +=6) {
      mouseClick2(200, y);
      delay(50); 
    }
  Mouse.end();
}
void mouseClick2(int x, int y){
  mouseMoveUpperLeft(100, 100); 
  int i = 0;
  int j=0;
  int val = 120;
  int div_y = (int) y/val;
  int mod_y = y % val;
  int div_x = (int) x / val;
  int mod_x = x % val;
  Mouse.move(mod_x, mod_y, 0);
  delay(10);
  while(true){      
      if(i>=div_y && j >= div_x){
        break;
      }
      if(div_y >i && div_x >j){
        Mouse.move(val, val,0);
        i= i+1;
        j=j+1;
      }else if (div_y >i){
        Mouse.move(0, val,0);
        i++;
      }else if (div_x >j){
        Mouse.move(val, 0,0);
        j++;
      }
      
      delay(10);
  }
  delay(10);
  Mouse.click();
}

it is the same result again:

what's that ?

Also what are you trying to do with this?

this function moves the mouse to upper left corner of the screen.

void mouseMoveUpperLeft(int x, int y){
 
  for(int i=0; i<20; i++){
    Mouse.move(-100, -100, 0);
  }
}

mouseClick2() function takes x, y parameters and move mouse to given coordinates. it is working but for some values it behaves strange like in the pictures i showed.

I have not looked at exactly what the math is doing, but why do you have a while loop? doesn't Mouse.move(x, y, 0) do what you want ?

Mouse.move() function moves the mouse from its current position. and it takes signed char as parameter which is a value between -127, +127. if you want to go further with mouse, you need to call the function a few times.

OK - got it, I looked at the code more closely


why does mouseMoveUpperLeft() have two parameters that are not used.
could be just

void mouseMoveUpperLeft() {
  for(int i=0; i<20; i++) Mouse.move(-100, -100, 0);
}

I assume that when you call mouseClick2(int x, int y) x and y are actually not negative and it's an absolute position on the screen is that correct?

given you always reset the cursor position to (0,0) I would not mess with modulo and signed values and would just do a few while loops to get where I need to be

here is an example of what it could look like (the code only prints on the Serial monitor - I guess you can adjust from there)

click to see the code
long mousePositionX = 0;
long mousePositionY = 0;

void printPosition() {
  Serial.print("Current cursor position (");
  Serial.print(mousePositionX);  Serial.write(',');
  Serial.print(mousePositionY);  Serial.println(")");
}

void mouseMoveUpperLeft() {
  mousePositionX = 0;
  mousePositionY = 0;
  Serial.print("\tMoved upper left, ");
  printPosition();
}

void mouseClick() {
  Serial.print("\tclick at "); printPosition();
}

void MouseMove(int8_t dx, int8_t dy) {
  Serial.print("\trelative move ∆(");
  Serial.print(dx); Serial.write(','); Serial.print(dy);
  Serial.print(")");
  mousePositionX +=  dx;
  mousePositionY +=  dy;
  Serial.write('.');
  printPosition();
}

void mouseClick(long targetX, long targetY) {
  Serial.print("Click requested at (");
  Serial.print(targetX);
  Serial.write(',');
  Serial.print(targetY);
  Serial.println(")");

  mouseMoveUpperLeft();

  // Move diagonally by 127 in both x and y directions
  while (mousePositionX < targetX - 127 && mousePositionY < targetY - 127) MouseMove(127, 127);

  // Move by 127 in the x direction
  while (mousePositionX < targetX - 127) MouseMove(127, 0);

  // Move by 127 in the y direction
  while (mousePositionY < targetY - 127) MouseMove(0, 127);

  // Take the remaining steps
  MouseMove(targetX - mousePositionX, targetY - mousePositionY);

  mouseClick();
}


void setup() {
  Serial.begin(115200);
  for (int y = 107; y < 389; y += 6) {
    mouseClick(200, y);
    delay(50);
  }
}

void loop() {}

running that code shows that it seems to work fine in terms of calculating the number of moves

Click requested at (200,107)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,0).Current cursor position (127,0)
	relative move ∆(73,107).Current cursor position (200,107)
	click at Current cursor position (200,107)
Click requested at (200,113)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,0).Current cursor position (127,0)
	relative move ∆(73,113).Current cursor position (200,113)
	click at Current cursor position (200,113)
Click requested at (200,119)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,0).Current cursor position (127,0)
	relative move ∆(73,119).Current cursor position (200,119)
	click at Current cursor position (200,119)
Click requested at (200,125)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,0).Current cursor position (127,0)
	relative move ∆(73,125).Current cursor position (200,125)
	click at Current cursor position (200,125)
Click requested at (200,131)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(73,4).Current cursor position (200,131)
	click at Current cursor position (200,131)
Click requested at (200,137)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(73,10).Current cursor position (200,137)
	click at Current cursor position (200,137)
Click requested at (200,143)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(73,16).Current cursor position (200,143)
	click at Current cursor position (200,143)
Click requested at (200,149)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(73,22).Current cursor position (200,149)
	click at Current cursor position (200,149)
Click requested at (200,155)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(73,28).Current cursor position (200,155)
	click at Current cursor position (200,155)
Click requested at (200,161)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(73,34).Current cursor position (200,161)
	click at Current cursor position (200,161)
Click requested at (200,167)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(73,40).Current cursor position (200,167)
	click at Current cursor position (200,167)
Click requested at (200,173)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(73,46).Current cursor position (200,173)
	click at Current cursor position (200,173)
Click requested at (200,179)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(73,52).Current cursor position (200,179)
	click at Current cursor position (200,179)
Click requested at (200,185)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(73,58).Current cursor position (200,185)
	click at Current cursor position (200,185)
Click requested at (200,191)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(73,64).Current cursor position (200,191)
	click at Current cursor position (200,191)
Click requested at (200,197)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(73,70).Current cursor position (200,197)
	click at Current cursor position (200,197)
Click requested at (200,203)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(73,76).Current cursor position (200,203)
	click at Current cursor position (200,203)
Click requested at (200,209)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(73,82).Current cursor position (200,209)
	click at Current cursor position (200,209)
Click requested at (200,215)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(73,88).Current cursor position (200,215)
	click at Current cursor position (200,215)
Click requested at (200,221)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(73,94).Current cursor position (200,221)
	click at Current cursor position (200,221)
Click requested at (200,227)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(73,100).Current cursor position (200,227)
	click at Current cursor position (200,227)
Click requested at (200,233)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(73,106).Current cursor position (200,233)
	click at Current cursor position (200,233)
Click requested at (200,239)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(73,112).Current cursor position (200,239)
	click at Current cursor position (200,239)
Click requested at (200,245)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(73,118).Current cursor position (200,245)
	click at Current cursor position (200,245)
Click requested at (200,251)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(73,124).Current cursor position (200,251)
	click at Current cursor position (200,251)
Click requested at (200,257)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(0,127).Current cursor position (127,254)
	relative move ∆(73,3).Current cursor position (200,257)
	click at Current cursor position (200,257)
Click requested at (200,263)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(0,127).Current cursor position (127,254)
	relative move ∆(73,9).Current cursor position (200,263)
	click at Current cursor position (200,263)
Click requested at (200,269)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(0,127).Current cursor position (127,254)
	relative move ∆(73,15).Current cursor position (200,269)
	click at Current cursor position (200,269)
Click requested at (200,275)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(0,127).Current cursor position (127,254)
	relative move ∆(73,21).Current cursor position (200,275)
	click at Current cursor position (200,275)
Click requested at (200,281)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(0,127).Current cursor position (127,254)
	relative move ∆(73,27).Current cursor position (200,281)
	click at Current cursor position (200,281)
Click requested at (200,287)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(0,127).Current cursor position (127,254)
	relative move ∆(73,33).Current cursor position (200,287)
	click at Current cursor position (200,287)
Click requested at (200,293)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(0,127).Current cursor position (127,254)
	relative move ∆(73,39).Current cursor position (200,293)
	click at Current cursor position (200,293)
Click requested at (200,299)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(0,127).Current cursor position (127,254)
	relative move ∆(73,45).Current cursor position (200,299)
	click at Current cursor position (200,299)
Click requested at (200,305)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(0,127).Current cursor position (127,254)
	relative move ∆(73,51).Current cursor position (200,305)
	click at Current cursor position (200,305)
Click requested at (200,311)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(0,127).Current cursor position (127,254)
	relative move ∆(73,57).Current cursor position (200,311)
	click at Current cursor position (200,311)
Click requested at (200,317)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(0,127).Current cursor position (127,254)
	relative move ∆(73,63).Current cursor position (200,317)
	click at Current cursor position (200,317)
Click requested at (200,323)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(0,127).Current cursor position (127,254)
	relative move ∆(73,69).Current cursor position (200,323)
	click at Current cursor position (200,323)
Click requested at (200,329)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(0,127).Current cursor position (127,254)
	relative move ∆(73,75).Current cursor position (200,329)
	click at Current cursor position (200,329)
Click requested at (200,335)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(0,127).Current cursor position (127,254)
	relative move ∆(73,81).Current cursor position (200,335)
	click at Current cursor position (200,335)
Click requested at (200,341)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(0,127).Current cursor position (127,254)
	relative move ∆(73,87).Current cursor position (200,341)
	click at Current cursor position (200,341)
Click requested at (200,347)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(0,127).Current cursor position (127,254)
	relative move ∆(73,93).Current cursor position (200,347)
	click at Current cursor position (200,347)
Click requested at (200,353)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(0,127).Current cursor position (127,254)
	relative move ∆(73,99).Current cursor position (200,353)
	click at Current cursor position (200,353)
Click requested at (200,359)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(0,127).Current cursor position (127,254)
	relative move ∆(73,105).Current cursor position (200,359)
	click at Current cursor position (200,359)
Click requested at (200,365)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(0,127).Current cursor position (127,254)
	relative move ∆(73,111).Current cursor position (200,365)
	click at Current cursor position (200,365)
Click requested at (200,371)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(0,127).Current cursor position (127,254)
	relative move ∆(73,117).Current cursor position (200,371)
	click at Current cursor position (200,371)
Click requested at (200,377)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(0,127).Current cursor position (127,254)
	relative move ∆(73,123).Current cursor position (200,377)
	click at Current cursor position (200,377)
Click requested at (200,383)
	Moved upper left, Current cursor position (0,0)
	relative move ∆(127,127).Current cursor position (127,127)
	relative move ∆(0,127).Current cursor position (127,254)
	relative move ∆(0,127).Current cursor position (127,381)
	relative move ∆(73,2).Current cursor position (200,383)
	click at Current cursor position (200,383)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.