Pages: [1]   Go Down
Author Topic: Leonardo Mouse.move problem  (Read 1508 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi there,

I have just bought the Arduino Leonardo to have some mouse "work" done automatically.
I am also wondering what numbers to put for the x and y coordinates. The original sketch had the high x and y values which did not seem to work so I broke down the high values to more and more smaller values which results in this awfully looking sketch.

So, it does what it is supposed to do:
I position the mouse pointer on the first icon, then I switch on (pin 13). The Leonardo clicks the first icon, then moves up und left to the second icon, clicks it, and moves back to the original position of the first icon.

I used the same values for x and y as in the first block (see sketch) without the minus.

What it really does:
It starts where I position the mouse cursor, move to the correct position of icon 2, then returns "approximately" to the first icon, but it is somewhat more right and more low than it should be. Then it moves up to icon 2 again (seems to hit the correct position as in the first run), and moves back to the same (false) position as in the first run. From this point, there seem to be no more glitches.

What did I do wrong?

I am using Win 7, with two monitors attached, running at 1920 x 1200 pixels. Desktop is expanded. I also tried to use only one monitor only but it did not solve the problem.

Here is the sketch. Do not blame me for it. I am beginner. :-)

Kind regards,

Chris

/*
Mausbewegung
 */
 
// An Pin 13 ist eine LED angeschlossen, muss initialisiert werden.
// buttonState wird gebraucht, um den Schalter abzufragen.
int LED = 13;
int buttonState;

// Setup wird nur 1 x beim Start des Programms durchlaufen
void setup() {               
  // Hier wird Pin 13 als AUSGANG definiert.
  pinMode(LED, OUTPUT);
  // An Pin 12 hängt der Schalter. Pin 12 muss als EINGANG definiert werden.
  pinMode(12, INPUT);
  // Pin 12 in definierten Zustand versetzen
  digitalWrite(12, HIGH);
}

// Die Routine loop läuft endlos. Die Funktion innerhalb der Routine
// soll aber nur bei eingeschaltetem Schalter laufen, daher while/wend
// Die LED dient zur optischen Kontrolle, ob die Funktion läuft, wenn der
// Schalter eingeschaltet ist.

void loop() {
  buttonState = digitalRead(12);
  delay(1000);
  Mouse.begin();         
    if(buttonState == LOW){
           // x = links/rechts, y = hoch/runter     
          //         x y   wheel
          Mouse.click(MOUSE_LEFT); // Click first icon (Start)
          delay(random(32,70));
          Mouse.move(-24, -24, 0);
          delay(random(32,70));
          Mouse.move(-24, -24, 0);
          delay(random(32,70));
          Mouse.move(-24, -24, 0);
          delay(random(32,70));
          Mouse.move(-24, -24, 0);
          delay(random(32,70));
          Mouse.move(-24, -24, 0);
          delay(random(32,70));
          Mouse.move(-24, -30, 0);
          delay(random(32,70));
          Mouse.move(-30, -24, 0);
          delay(random(32,70));
          Mouse.move(-24, -24, 0);
          delay(random(32,70));
          Mouse.move(-14, -24, 0);
          delay(random(32,70));
          Mouse.move(-22, -24, 0);
          delay(random(32,70));
          Mouse.move(-20, -24, 0);
          delay(random(32,70));
          Mouse.move(-14, -20, 0);
          delay(random(32,70));
          Mouse.move(-12, -26, 0);
          delay(random(32,70));
          Mouse.move(-14, -30, 0);
          delay(random(32,70));
          Mouse.move(-21, -26, 0);
          delay(random(32,70));
          Mouse.click(MOUSE_LEFT); // Click second icon
          delay(1000);
         
          Mouse.move(24, 24, 0); // ... move back to start.
          delay(random(32,70));
          Mouse.move(24, 24, 0);
          delay(random(32,70));
          Mouse.move(24, 24, 0);
          delay(random(32,70));
          Mouse.move(24, 24, 0);
          delay(random(32,70));
          Mouse.move(24, 24, 0);
          delay(random(32,70));
          Mouse.move(24, 30, 0);
          delay(random(32,70));
          Mouse.move(30, 24, 0);
          delay(random(32,70));
          Mouse.move(24, 24, 0);
          delay(random(32,70));
          Mouse.move(14, 24, 0);
          delay(random(32,70));
          Mouse.move(22, 24, 0);
          delay(random(32,70));
          Mouse.move(20, 24, 0);
          delay(random(32,70));
          Mouse.move(14, 20, 0);
          delay(random(32,70));
          Mouse.move(12, 26, 0);
          delay(random(32,70));
          Mouse.move(14, 30, 0);
          delay(random(32,70));
          Mouse.move(21, 26, 0);
          delay(random(32,70));
//          Mouse.click(MOUSE_LEFT); // Icon 2 anwählen
          delay(1000);
/*          delay(random(32,70));
          Mouse.move(20, 26, 0);
          delay(random(32,70));
          Mouse.move(18, 24, 0);
          delay(random(32,70));
          Mouse.move(0, 18, 0);
          delay(random(32,70));
          Mouse.move(0, 18, 0);
          delay(random(32,70));
          Mouse.move(0, 16, 0);
          delay(random(32,70));
          Mouse.move(0, 25, 0);
          delay(random(32,70));
          Mouse.move(0, 32, 0);
          delay(random(32,70));
          Mouse.move(3, 30, 0);
          delay(1000);
//          Mouse.click(MOUSE_LEFT); // "Confirm" Button
          delay(random(32,70));
          Mouse.move(20, 20, 0); // ...
          delay(random(32,70));
          Mouse.move(20, 20, 0);
          delay(random(32,70));
          Mouse.move(20, 20, 0);
          delay(random(32,70));
          Mouse.move(20, 20, 0);
          delay(random(32,70));         
          Mouse.move(20, 20, 0); // ...
          delay(random(32,70));
          Mouse.move(20, 20, 0);
          delay(random(32,70));
          Mouse.move(20, 20, 0);
          delay(random(32,70));
          Mouse.move(30, 20, 0);
          delay(random(32,70));         
          Mouse.move(30, 0, 0); // ...
          delay(random(32,70));         
          Mouse.move(22, 5, 0);
          delay(random(32,70));         
          Mouse.move(26, 20, 0);
          delay(random(32,70));         
          Mouse.move(28, 18, 0); // ... */
//          Mouse.click(MOUSE_LEFT);
          digitalWrite(LED, HIGH);   // LED on (blink once for optical confirmation)
          delay(25);                 // Pause 25 ms
          digitalWrite(LED, LOW);    // LED off
          delay(2000);
        }
  Mouse.end();
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
          Mouse.click(MOUSE_LEFT); // Click first icon (Start)
          delay(random(32,70));
          Mouse.move(-24, -24, 0);
          delay(random(32,70));
When trying to debug a problem, random() is NOT your friend.

Code:
  Mouse.begin();         
    if(buttonState == LOW){
        }
  Mouse.end();
If the switch state is NOT low, you call Mouse.begin() and Mouse.end() anyway. Why is that?

A function to move the mouse some amount, with inputs for sign in X and sign in Y (+1 or -1) would ensure that the mouse was indeed moved the same in each block. I'm not going to parse through all your code to see that you do indeed move back the same amount you move forward (or that you move forwards the same amount you move backwards).
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello PaulS,

thank you for your answer. I have updated the sketch as per your suggestion: Removed all random delays and put the Mouse.begin() and Mouse.end() within the if-clause. I also removed the unnecessary parts with /* and */ ...

I agree with you - my expectation is that the mouse moves back the same amount as moved forward. And I really put the correct values! The interesting point is the glitch is only happening in the first cycle. When the mouse is put back to the wrong starting position, it will keep going to the same position until the end of time.

Below is the the new, cleaner sketch. The problem still exists.

Kind regards,

Chris


Code:
int LED = 13;
int buttonState;


void setup() {              
  pinMode(LED, OUTPUT);
  pinMode(12, INPUT);
  digitalWrite(12, HIGH);
}

void loop() {
  buttonState = digitalRead(12);
  delay(1000);

  if(buttonState == LOW){
    Mouse.begin();
    Mouse.click(MOUSE_LEFT); // Click first icon (Start)
    Mouse.move(-24, -24, 0);
    Mouse.move(-24, -24, 0);
    Mouse.move(-24, -24, 0);
    Mouse.move(-24, -24, 0);
    Mouse.move(-24, -24, 0);
    Mouse.move(-24, -30, 0);
    Mouse.move(-30, -24, 0);
    Mouse.move(-24, -24, 0);
    Mouse.move(-14, -24, 0);
    Mouse.move(-22, -24, 0);
    Mouse.move(-20, -24, 0);
    Mouse.move(-14, -20, 0);
    Mouse.move(-12, -26, 0);
    Mouse.move(-14, -30, 0);
    Mouse.move(-21, -26, 0);
    Mouse.click(MOUSE_LEFT); // Click second icon
    delay(1000);

    Mouse.move(24, 24, 0); // ... move back to start.
    Mouse.move(24, 24, 0);
    Mouse.move(24, 24, 0);
    Mouse.move(24, 24, 0);
    Mouse.move(24, 24, 0);
    Mouse.move(24, 30, 0);
    Mouse.move(30, 24, 0);
    Mouse.move(24, 24, 0);
    Mouse.move(14, 24, 0);
    Mouse.move(22, 24, 0);
    Mouse.move(20, 24, 0);
    Mouse.move(14, 20, 0);
    Mouse.move(12, 26, 0);
    Mouse.move(14, 30, 0);
    Mouse.move(21, 26, 0);

    delay(1000);
    digitalWrite(LED, HIGH);   // LED on (blink once for optical confirmation)
    delay(25);                 // Pause 25 ms
    digitalWrite(LED, LOW);    // LED off
    delay(2000);
    Mouse.end();
  }
}
« Last Edit: July 01, 2012, 05:30:43 pm by Ovations » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

the following sketch shows the problem even better. As you can see from the sketch, the mouse is moved left, up, right and down the same amounts. But after the first cycle it is not back to the point where it started.

To verify, just place the cursor on a point of a text and start the program. You will notice, that the cursor will be put more to the right as where it started.

How can I fix this?

Best regards,

Chris

Code:
int buttonState;

void setup() {              
  pinMode(12, INPUT);
  digitalWrite(12, HIGH);
}

void loop() {
  buttonState = digitalRead(12);
  delay(2000);

  if(buttonState == LOW){
    Mouse.begin();
    Mouse.move(-20, 0, 0);
    Mouse.move(-20, 0, 0);
    Mouse.move(-20, 0, 0);
    Mouse.move(-20, 0, 0);

    delay(500);

    Mouse.move(0, -20, 0);
    Mouse.move(0, -20, 0);
    Mouse.move(0, -20, 0);
    Mouse.move(0, -20, 0);

    delay(500);

    Mouse.move(20, 0, 0);
    Mouse.move(20, 0, 0);
    Mouse.move(20, 0, 0);
    Mouse.move(20, 0, 0);

    delay(500);

    Mouse.move(0, 20, 0);
    Mouse.move(0, 20, 0);
    Mouse.move(0, 20, 0);
    Mouse.move(0, 20, 0);

    delay(2000);
    Mouse.end();
  }
}
« Last Edit: July 01, 2012, 05:31:34 pm by Ovations » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Please edit your posts, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello Nick,

done.

I have spent several hours trying to fix my sketch but it seems to be impossible. Meanwhile I assume it is not my code doing strange things. It also seems that the glitches are bigger when the values given to Mouse.move() are higher.

I have re-programmed my original code with for-next and using small values for Mouse.move() but it is not very satisfying to have (small) manual corrections to give to the mouse cursor.

Best regards,

Chris
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I think the problem is, mouse movements are accelerated. That is, they are non-linear. This is so if you move the mouse quickly it covers more ground than if you move it slowly. This modified sketch moves the mouse around in a square, and on my Mac at least, it arrives back at the exact same spot.

Code:
int buttonState;

void setup() {               
  pinMode(12, INPUT);
  digitalWrite(12, HIGH);
}

void loop() {
  int i;
  const int amount = 500;
 
  buttonState = digitalRead(12);

  if(buttonState == LOW){
    Mouse.begin();
    for (i = 0; i < amount; i++)
      Mouse.move(-1, 0, 0);

    delay(500);

    for (i = 0; i < amount; i++)
      Mouse.move(0, -1, 0);

    delay(500);

    for (i = 0; i < amount; i++)
      Mouse.move(1, 0, 0);

    delay(500);

    for (i = 0; i < amount; i++)
      Mouse.move(0, 1, 0);

    delay(2000);
    Mouse.end();
  }
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello Nick,

that's an idea - the mouse acceleration! I will check on that tomorrow and let you know.

Kind regards,

Chris
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello Nick,

thank you very much for your hint regarding mouse acceleration. This was causing the inaccuracy.
I just had to uncheck a box in the Windows mouse settings and update my sketch. It now exactly (!!!) does what I expected.

Kind regards,

Chris
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello Nick,

thank you very much for your hint regarding mouse acceleration. This was causing the inaccuracy.
I just had to uncheck a box in the Windows mouse settings and update my sketch. It now exactly (!!!) does what I expected.

Kind regards,

Chris


Hi Chris!

I am having this exact problem! What settings did you alter?

Regards,

Luke
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Disable mouse acceleration. Depending on the operating system, and version, it is hard to say exactly how you do that. Maybe tell your OS that you have a trackpad which tends to not use acceleration. I can't even see the option under XP. Maybe set "pointer speed" to slow, for example.

Here's an example I found doing a Google search:

http://www.nextlevelgamer.com/tweaks/disabling-mouse-acceleration-in-windows-xp
Logged

Pages: [1]   Go Up
Jump to: