Project 11.00 Using the Board as a Mouse

The MCU trainer can also act like a mouse on a computer! This functionality is unique with microcontrollers. In this project you’ll learn how to use it as such!

Project Code:

//////////////////////////////////////////////////////
// 11.00 - Using The Board As A Mouse

#include "Mouse.h"

byte SW1 = 1;
byte pressed = LOW;

void setup() {
  pinMode(SW1, INPUT);

  Mouse.begin();
}

void loop() {
  if (digitalRead(SW1) == pressed) {

    for (byte x = 0; x < 100; x++) {
      Mouse.move(1, 0, 0);
    }

    for (byte x = 0; x < 100; x++) {
     Mouse.move(0, 1, 0);
    }

    for (byte x = 0; x < 100; x++) {
      Mouse.move(-1, 0, 0);
    }

    for (byte x = 0; x < 100; x++) {
      Mouse.move(0, -1, 0);
    }
  }

  //You can also:
  /*
    Click the mouse:
    Mouse.press(MOUSE_RIGHT);
    Mouse.press(MOUSE_MIDDLE);
    Mouse.press(MOUSE_LEFT);

    Release the mouse:
    Mouse.release(MOUSE_RIGHT);
    Mouse.release(MOUSE_MIDDLE);
    Mouse.release(MOUSE_LEFT);
    
  */
}
//////////////////////////////////////////////////////

*If you’re copying and pasting the code, or typing from scratch, delete everything out of a new Arduino sketch and paste / type in the above text.

Before you write a program that includes mouse functionality it is important to be able to control it. In this project we don’t activate the mouse functionality unless a button is pressed. When you plug the MCU Trainer in, you don’t want your mouse going crazy!

The first thing that we need to do is include the proper library. There is no need to install this library as it comes standard with the Arduino IDE. All you’d have to do is type in “ #include “Mouse.h”.

#include "Mouse.h"

Next, we need to include some variables to be used with the button.

byte SW1 = 1;
byte pressed = LOW;

In the setup() function, we need to set the pinMode of the button and initialize the mouse functionality. Initializing the MCU Trainer as mouse can be done with the Mouse.begin() method.

void setup() {
  pinMode(SW1, INPUT);

  Mouse.begin();
}

In the loop() function, we check to see if the button has been pressed before using the mouse functionality.

void loop() {
  if (digitalRead(SW1) == pressed) {

Once SW1 has been pressed, we can use the Mouse.move method to move the mouse! The first parameter is how many pixels to move in the x direction, the second is how many pixels to move in the y direction and the last is how many lines to scroll. The mouse will move instantly so it has been put into a series of for-loops and run a certain number of times to make incremental changes instead (So we can see it change).

for (byte x = 0; x < 100; x++) {
  //         x  y  scroll
  Mouse.move(1, 0, 0);
}

for (byte x = 0; x < 100; x++) {
  //         x  y  scroll
  Mouse.move(0, 1, 0);
}

To move in the opposite direction all you have to do is pass it a negative value.

for (byte x = 0; x < 100; x++) {
  //         x  y  scroll
  Mouse.move(-1, 0, 0);
}

for (byte x = 0; x < 100; x++) {
    Mouse.move(0, -1, 0);
  }
}

Here are some additional functions you can use with the Mouse.h library:

  //You can also:
  /*
    Click the mouse:
    Mouse.press(MOUSE_RIGHT);
    Mouse.press(MOUSE_MIDDLE);
    Mouse.press(MOUSE_LEFT);

    Release the mouse:
    Mouse.release(MOUSE_RIGHT);
    Mouse.release(MOUSE_MIDDLE);
    Mouse.release(MOUSE_LEFT);
    
  */
}
//////////////////////////////////////////////////////
Previous
Previous

Project 12.00 Using the Board as a Keyboard

Next
Next

Project 10.01 Sending IR