Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15682

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

$
0
0

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?

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 15682

Latest Images

Trending Articles



Latest Images