Project 11.00 Using the Board as a Mouse

This Arduino sketch demonstrates how to use the MC Trainer board as a mouse input device!

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);
    
  */
}
//////////////////////////////////////////////////////
				
			

Learn More In Our Free Instructional Guidebook

This comprehensive guidebook for the 1st Maker Space Microcontroller Trainer provides a comprehensive introduction to the world of Arduino programming for beginners. It guides users through the foundational concepts of microcontrollers, detailing the unique features of the Arduino Leonardo-compatible MCU Trainer board. The manual offers a step-by-step journey from understanding the hardware components and the Arduino programming language to the vibrant global community of Arduino enthusiasts. It delves into the intricacies of each onboard circuit, explaining their functionalities and applications. With a focus on hands-on learning, the manual includes a series of coding exercises, tutorials in C/C++, and insights into the Arduino IDE.

More Projects

Project 1.00 Blink

In this project, you’ll learn how to blink an LED!

Project 1.01 Blink x2

In this project, you’ll learn how to blink more than one LED!

Bri Wagner

Education Solution Specialist

Bri Wagner brings a bachelor’s degree in Elementary Education and nine years of classroom teaching experience to her role at 1st Maker Space. A former Teacher of the Year Award recipient, Bri is passionate about creating engaging, hands-on learning experiences that inspire student success. She understands the impact that innovative instruction and meaningful classroom resources can have on student achievement and is dedicated to partnering with educators to bring high-quality STEM solutions into classrooms. Bri is committed to empowering teachers and helping students develop the creativity, confidence, and problem-solving skills they need to thrive.