Control Mouse using Arduino DUE

  **Tutorial: Controlling Mouse Cursor with Arduino Using Serial Communication**


In this tutorial, we'll explore how to control the mouse cursor using an Arduino board and serial communication. We'll write Arduino code that reads commands from the serial port and moves the mouse cursor accordingly. We'll use a library called `myMouse` for mouse control and utilize the Arduino's serial communication capabilities.


### Hardware and Software Requirements


- Arduino board (e.g., Arduino Uno)

- USB cable for connecting Arduino to the computer

- Arduino IDE installed on your computer

- `myMouse` library


### Setting Up the Hardware


1. Connect your Arduino board to your computer using the USB cable.

2. Open the Arduino IDE on your computer.


### Installing the Library


Before we start writing the code, we need to install the `myMouse` library:


1. Open the Arduino IDE.

2. Go to **Sketch > Include Library > Manage Libraries**.

3. Search for "myMouse" and install the library.


### Writing the Arduino Code


Now, let's write the Arduino code:


```cpp

#include <myMouse.h>


USBHost usb;

MouseController myMouse(usb);


boolean leftButton = false;

boolean middleButton = false;

boolean rightButton = false;


int v_x = 0;

int v_y = 0;

String received_code;

int index1;

int index2;

int index3;

int index4;

int index5;


void mouseHMoved() {

  Serial.print("Move: ");

  Serial.print(myMouse.getXChange());

  Serial.print(", ");

  Serial.println(myMouse.getYChange());


  myMouse.move(myMouse.getXChange(), myMouse.getYChange(), 0);

}


void mouseHDragged() {

  Serial.print("DRAG: ");

  Serial.print(myMouse.getXChange());

  Serial.print(", ");

  Serial.println(myMouse.getYChange());

}


void mouseHPressed() {

  Serial.print("Pressed: ");

  if (myMouse.getButton(LEFT_BUTTON)) {

    Serial.println("L");

    leftButton = true;

    myMouse.press();

  }


  if (myMouse.getButton(MIDDLE_BUTTON)) {

    Serial.println("M Butt Dn");

    middleButton = true;

  }


  if (myMouse.getButton(RIGHT_BUTTON)) {

    Serial.println("R");

    myMouse.press(MOUSE_RIGHT);

    rightButton = true;

  }

}


void mouseHReleased() {

  Serial.print("Released: ");

  if (!myMouse.getButton(LEFT_BUTTON) && leftButton == true) {

    Serial.print("L");

    leftButton = false;

    myMouse.release();

  }


  if (!myMouse.getButton(MIDDLE_BUTTON) && middleButton == true) {

    Serial.print("M");

    middleButton = false;

    Serial.println("M Butt Up");

  }


  if (!myMouse.getButton(RIGHT_BUTTON) && rightButton == true) {

    Serial.print("R");

    rightButton = false;

    myMouse.release(MOUSE_RIGHT);

  }

  Serial.println();

}


void setup() {

  Serial.begin(115200);

  myMouse.begin();

  Serial.println("Program started");

  delay(200);

}


void loop() {

  usb.Task();

  extraAutomation();

}


void extraAutomation() {

  while (Serial.available()) {

    char c = Serial.read();

    if (c == '*') {

      index1 = received_code.indexOf(',');

      String x_direction = received_code.substring(0, index1);

      index2 = received_code.indexOf(',', index1 + 1);

      String x_value = received_code.substring(index1 + 1, index2 + 1);

      index3 = received_code.indexOf(',', index2 + 1);

      String y_direction = received_code.substring(index2 + 1, index3 + 1);

      index4 = received_code.indexOf(',', index3 + 1);

      String y_value = received_code.substring(index3 + 1);


      String x_d = String(x_direction);

      String y_d = String(y_direction);

      int x_v = x_value.toInt();

      int y_v = y_value.toInt();

      int smooth = 2;


      if (x_d == "p" && y_d == "p,") {

        while (x_v > 0 || y_v > 0) {

          if (x_v > 0) {

            myMouse.move(10, 0, 0);

            x_v -= 1;

            delay(smooth);

          }

          if (y_v > 0) {

            myMouse.move(0, 10, 0);

            y_v -= 1;

            delay(smooth);

          }

        }

      }

      // Other conditions omitted for brevity

      myMouse.press();

      delay(105);

      myMouse.release();

      received_code = "";

    } else {

      received_code += c;

    }

  }

}

```


### Explanation


This code allows you to control the mouse cursor using serial communication with the Arduino board. Here's a breakdown of how it works:


- We include the necessary libraries and define some variables.

- We define functions for handling mouse movements, mouse button presses, and releases.

- In the `setup()` function, we initialize serial communication and the mouse controller.

- In the `loop()` function, we continuously check for serial data and call the `extraAutomation()` function to handle mouse movements based on received commands.

- The `extraAutomation()` function reads commands from the serial port and moves the mouse cursor accordingly.


### Conclusion


In this tutorial, you've learned how to control the mouse cursor using an Arduino board and serial communication. You can now send commands to the Arduino board via serial communication to move the mouse cursor in various directions. This functionality can be useful for projects where you need to interact with a computer using an Arduino board. Experiment with the code and explore additional functionalities you can implement with mouse control.

Comments

Popular posts from this blog

Apple iPad Air (2022) Specifications and Review in 2023

17-year-old suspect in fatal stabbing at Taylor Swift-themed dance event ID’d

Exercise vs Diet