Button Mouse Control

Control cursor movement with 5 pushbuttons.

Using the Mouse library, you can controls a computer's onscreen cursor with an Arduino Leonardo, Micro, or Due. This particular example uses five pushbuttons to move the onscreen cursor. Four of the buttons are directional (up, down, left, right) and one is for a left mouse click

Cursor movement from the Arduino is always relative. Every time an input is read, the cursor's position is updated relative to it's current position.

Whenever one of the directional buttons is pressed, the Arduino will move the mouse, mapping a HIGH input to a range of 5 in the appropriate direction.

The fifth button is for controlling a left click from a mouse. When pressed, the board sends a press to the computer. When the button is released, the computer will recognize the event.

NB: When you use the

Mouse.move()
command, the Arduino takes over your computer's cursor! To insure you don't lose control of your computer while running a sketch with this function, make sure to set up a controller before you call Mouse.move(). This sketch only updates the cursor position when a button is pressed.

Hardware Required

  • Arduino Leonardo, Micro or Due board

  • 5x momentary pushbuttons

  • 5x 10k ohm resistors

  • hook-up wires

  • breadboard

Circuit

Connect your board to your computer with a micro-USB cable. The buttons are connected to digital inputs from 2 to 6. Make sure you use 10k ohm pulldown resistors.

circuit

Schematic

schematic

Code

1/*
2
3 ButtonMouseControl
4
5 For Leonardo and Due boards only.
6
7 Controls the mouse from five pushbuttons on an Arduino Leonardo, Micro or Due.
8
9 Hardware:
10
11 - five pushbuttons attached to D2, D3, D4, D5, D6
12
13 The mouse movement is always relative. This sketch reads four pushbuttons,
14
15 and uses them to set the movement of the mouse.
16
17 WARNING: When you use the Mouse.move() command, the Arduino takes over your
18
19 mouse! Make sure you have control before you use the mouse commands.
20
21 created 15 Mar 2012
22
23 modified 27 Mar 2012
24
25 by Tom Igoe
26
27 This example code is in the public domain.
28
29 http://www.arduino.cchttps://www.arduino.cc/en/Tutorial/ButtonMouseControl
30
31*/
32
33#include "Mouse.h"
34
35// set pin numbers for the five buttons:
36
37const int upButton = 2;
38
39const int downButton = 3;
40
41const int leftButton = 4;
42
43const int rightButton = 5;
44
45const int mouseButton = 6;
46
47int range = 5; // output range of X or Y movement; affects movement speed
48int responseDelay = 10; // response delay of the mouse, in ms
49
50void setup() {
51
52 // initialize the buttons' inputs:
53
54 pinMode(upButton, INPUT);
55
56 pinMode(downButton, INPUT);
57
58 pinMode(leftButton, INPUT);
59
60 pinMode(rightButton, INPUT);
61
62 pinMode(mouseButton, INPUT);
63
64 // initialize mouse control:
65
66 Mouse.begin();
67}
68
69void loop() {
70
71 // read the buttons:
72
73 int upState = digitalRead(upButton);
74
75 int downState = digitalRead(downButton);
76
77 int rightState = digitalRead(rightButton);
78
79 int leftState = digitalRead(leftButton);
80
81 int clickState = digitalRead(mouseButton);
82
83 // calculate the movement distance based on the button states:
84
85 int xDistance = (leftState - rightState) * range;
86
87 int yDistance = (upState - downState) * range;
88
89 // if X or Y is non-zero, move:
90
91 if ((xDistance != 0) || (yDistance != 0)) {
92
93 Mouse.move(xDistance, yDistance, 0);
94
95 }
96
97 // if the mouse button is pressed:
98
99 if (clickState == HIGH) {
100
101 // if the mouse is not pressed, press it:
102
103 if (!Mouse.isPressed(MOUSE_LEFT)) {
104
105 Mouse.press(MOUSE_LEFT);
106
107 }
108
109 }
110
111 // else the mouse button is not pressed:
112
113 else {
114
115 // if the mouse is pressed, release it:
116
117 if (Mouse.isPressed(MOUSE_LEFT)) {
118
119 Mouse.release(MOUSE_LEFT);
120
121 }
122
123 }
124
125 // a delay so the mouse doesn't move too fast:
126
127 delay(responseDelay);
128}

Learn more

You can find more basic tutorials in the built-in examples section.

You can also explore the language reference, a detailed collection of the Arduino programming language.

Last revision 2015/07/28 by SM

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.