Pages: [1]   Go Down
Author Topic: Mouse.click() does not work  (Read 1146 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am running the following code to move the mouse and click it using input from the keyboard:

Code:
void setup() {

  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
 
  Mouse.begin();
 
}

void loop() {
   
  // use serial input to control the mouse:
  if (Serial.available() > 0) {
    char inChar = Serial.read();
    Mouse.begin();
    switch (inChar) {   
    case 'u':
      // move mouse up
      Serial.println("u received");
      Mouse.move(0, -40);
      break;
    case 'd':
      // move mouse down
      Mouse.move(0, 40);
      break;
    case 'l':
      // move mouse left
      Mouse.move(-40, 0);
      break;
    case 'r':
      // move mouse right
      Mouse.move(40, 0);
      break;
    case 'm':
      // perform mouse left click
      Mouse.click(MOUSE_LEFT);
      break;
    }
  }
}

When I run my code on the Arduino Due, and send 'u' on the serial port for example, which should move my mouse cursor up, nothing at all happens. Same goes for Mouse.click(). I have no idea what I'm doing incorrectly.
Logged

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

Once you have configured the Leonardo to be a mouse, it cannot also communicate via serial comms at the same time. It is either a serial port or a mouse (or a keyboard, etc.).
Logged


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

OK, so does that mean I have to have a "Serial.end()" segment before I do a "Mouse.begin()" every time?
Logged

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

Also, this example on the Arduino website seems to be doing exactly this:

http://arduino.cc/en/Tutorial/KeyboardAndMouseControl
Logged

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

Oh, OK. Looks like I was wrong. Well, not the first time, eh?

I just tested that on my Leonardo (the example you linked to). So you are right, it can do both.

But I don't know about the Due. I'll move this thread to the Due part of the forum.
Logged


Forum Administrator
Milano, Italy
Offline Offline
Sr. Member
*****
Karma: 23
Posts: 292
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


a4rashid,

with the Arduino Due the Serial class communicate with the Programming Port.

If you have connected your PC on the Due's Native Port you shoud use the SerialUSB object
(this means that you must replace all the Serial with SerialUSB in your sketch).
Logged

C.

Pages: [1]   Go Up
Jump to: