problem using arduino leonardo mousemove()

hello,

To a project, i want to use a Arduino leonardo card to emulate the mouse in order to synchronise 3 different programs. I have a push button on the card; when i press it, i want the mouse to move and to click on the start icons of the 3 differents programs one after an other.

I have to make 15 trials. But sometimes (1/7), the mouse move almost 1cm to far ... and so, all is offset.

Do you have any suggestion ?

Thank you,

Lea

Post one of your trials and let's see if we can spot where you are going wrong.

It sounds like you are expecting the mouse to move to specific locations. It does not. It moves relative amounts. Exactly how far the cursor moves in response to a mouse move directive is a function of many factors, including screen size, resolution, mouse acceleration, mouse speed, etc.

It is impossible for the Arduino to know anything about how the mouse on the PC is configured or what the screen size or resolution is, so it can not reverse engineer/guess exactly what to send to make the mouse move to an exact location.

Even if, by some miracle, that was possible, the Arduino has no idea where the application's frame is on the screen, or where the button is relative to the frame.

More realistic expectations on your part are going to be required.

Hello,

Thank you for your interest. There's my code :

#include "Mouse.h"
// Hardware de la carte Arduino:
// set board pin number for switch
const int switchPin = 2;      // switch to trigge mouse control

boolean mouseIsActive = false;    // whether or not to control the mouse
int lastSwitchState = LOW;        // previous switch state
int switchState;
uint8_t deplacement;
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // make pin 2 an input and turn on the
  // pullup resistor so it goes high unless
  // connected to ground:
  pinMode(switchPin, INPUT_PULLUP);

  //take control of the mouse:
  Mouse.begin();
}

void loop() {
   unsigned long currentMillis = millis();

  // if the button is pressed
  if (digitalRead(switchPin) == LOW) {
    deplacement = 1;
  }

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    
  switch (deplacement) {
      case 1:
        Mouse.move(127, -5);
        delay(1000);
        Mouse.click();
        delay(1000);
        ++deplacement;
        break;

         case 2:
        Mouse.move(-18, -85);
        delay(2);
        Mouse.click();
        delay(1000);
        ++deplacement;
        break;

      case 3:
        //do something when var equals 3
        Mouse.move(26,85);
        delay(500);
        Mouse.click();
        delay(2);
        ++deplacement;
        ///Mouse.click();
        
        break;

       case 4:
        //do something when var equals 3
        Mouse.move(127,-127);
        delay(2);
        Mouse.move(127,-127);
        delay(2);
        Mouse.move(87,-92);
        delay(2);
        Mouse.click();
        deplacement = 0;
        ///Mouse.click();

      default:
        // if nothing else matches, do the default
        // default is optional
        break;
    }
  }
}

To answer at PaulS I put in my code relative amout and the mouse goes in the right location and click on the right icons. So, most of the times it works, but sometimes, 2times on my 15 trials it goes almost 1cm too far ... It's wierd that the error does not occured each times or never, doesn't it ?

Léa

It's wierd that the error does not occured each times or never, doesn't it ?

No, it's not.

It's wierd that the error does not occured each times or never, doesn't it ?

As Paul said no it is not. This is because the PC end is somewhat casual about getting all the pulses from a mouse. This is because most of the time it doesn't matter as you move the mouse and carry on moving it until it hits the spot you want. You don't notice that you have moved the mouse in one direction slightly more. If you have a mouse mat you might notice that you have to lift the mouse in order to center it on the mat.

Try this test. Move the mouse by hand left and right repeatedly. You will notice that the mouse pointer and the position of the mouse on the screen do not stay synchronized but drift to one side.

I seem to remember seeing somewhere an enhanced library that used something like HID touch instead of HID mouse so you could do absolute mouse positioning and higher resolution mouse moves.
The question is where though.

renar:
To a project, i want to use a Arduino leonardo card to emulate the mouse in order to synchronise 3 different programs. I have a push button on the card; when i press it, i want the mouse to move and to click on the start icons of the 3 differents programs one after an other.

If the icon you want to click is in the same position on the screen all the time, you can move the mouse to that position on the screen. I use the Leonardo and the standard library to move to an absolute position on the screen. All you need to do is move to a corner of the screen to establish a base for where you are, then move to where you want to be. The mouse will end up exactly where you want it.

http://forum.arduino.cc/index.php?topic=362973.msg2503650#msg2503650

Riva:
I seem to remember seeing somewhere an enhanced library that used something like HID touch instead of HID mouse so you could do absolute mouse positioning and higher resolution mouse moves.
The question is where though.

This is of interest to me so I did some looking around and found:
HID/examples/Mouse/AbsoluteMouse/AbsoluteMouse.ino at master · NicoHood/HID · GitHub - A quick test didn't work for me with my Micro.
Teensyduino: Using USB Mouse with Teensy on the Arduino IDE - "(Teensy LC & 3.0 & 3.1 Only)"
Edits to the Mouse library:
added Mouse.moveAbs() as a new feature for absolute mouse positioning… · nospam2000/Arduino@7b77d1e · GitHub
https://github.com/keyboardio/Arduino/commit/a520873aba7f2c99eda6c9b1979df170939d9f68
USB Absolute Mouse Mode - Libraries - Arduino Forum

dmjlambert:
All you need to do is move to a corner of the screen to establish a base for where you are, then move to where you want to be. The mouse will end up exactly where you want it.

I've been using this technique and it works consistently for me within a pixel or two. I wrote a function to do it and made it into a library.

pert:
I wrote a function to do it and made it into a library.

Wow, that is a good idea. :slight_smile:

Thank you for this way of reflexion... I will try it on Monday !