| 1 |
/* |
| 2 |
KeyboardAndMouseControl |
| 3 |
|
| 4 |
Controls the mouse from five pushbuttons on an Arduino Leonardo, Micro or Due. |
| 5 |
|
| 6 |
Hardware: |
| 7 |
- five pushbuttons attached to D2, D3, D4, D5, D6 |
| 8 |
|
| 9 |
The mouse movement is always relative. This sketch reads four pushbuttons, and |
| 10 |
uses them to set the movement of the mouse. |
| 11 |
|
| 12 |
WARNING: When you use the Mouse.move() command, the Arduino takes over your |
| 13 |
mouse! Make sure you have control before you use the mouse commands. |
| 14 |
|
| 15 |
created 15 Mar 2012 |
| 16 |
modified 27 Mar 2012 |
| 17 |
by Tom Igoe |
| 18 |
|
| 19 |
This example code is in the public domain. |
| 20 |
|
| 21 |
https://www.arduino.cc/en/Tutorial/BuiltInExamples/KeyboardAndMouseControl |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "Keyboard.h" |
| 25 |
#include "Mouse.h" |
| 26 |
|
| 27 |
// set pin numbers for the five buttons: |
| 28 |
const int upButton = 2; |
| 29 |
const int downButton = 3; |
| 30 |
const int leftButton = 4; |
| 31 |
const int rightButton = 5; |
| 32 |
const int mouseButton = 6; |
| 33 |
|
| 34 |
void setup() { // initialize the buttons' inputs: |
| 35 |
pinMode(upButton, INPUT); |
| 36 |
pinMode(downButton, INPUT); |
| 37 |
pinMode(leftButton, INPUT); |
| 38 |
pinMode(rightButton, INPUT); |
| 39 |
pinMode(mouseButton, INPUT); |
| 40 |
|
| 41 |
Serial.begin(9600); |
| 42 |
// initialize mouse control: |
| 43 |
Mouse.begin(); |
| 44 |
Keyboard.begin(); |
| 45 |
} |
| 46 |
|
| 47 |
void loop() { |
| 48 |
// use serial input to control the mouse: |
| 49 |
if (Serial.available() > 0) { |
| 50 |
char inChar = Serial.read(); |
| 51 |
|
| 52 |
switch (inChar) { |
| 53 |
case 'u': |
| 54 |
// move mouse up |
| 55 |
Mouse.move(0, -40); |
| 56 |
break; |
| 57 |
case 'd': |
| 58 |
// move mouse down |
| 59 |
Mouse.move(0, 40); |
| 60 |
break; |
| 61 |
case 'l': |
| 62 |
// move mouse left |
| 63 |
Mouse.move(-40, 0); |
| 64 |
break; |
| 65 |
case 'r': |
| 66 |
// move mouse right |
| 67 |
Mouse.move(40, 0); |
| 68 |
break; |
| 69 |
case 'm': |
| 70 |
// perform mouse left click |
| 71 |
Mouse.click(MOUSE_LEFT); |
| 72 |
break; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
// use the pushbuttons to control the keyboard: |
| 77 |
if (digitalRead(upButton) == HIGH) { |
| 78 |
Keyboard.write('u'); |
| 79 |
} |
| 80 |
if (digitalRead(downButton) == HIGH) { |
| 81 |
Keyboard.write('d'); |
| 82 |
} |
| 83 |
if (digitalRead(leftButton) == HIGH) { |
| 84 |
Keyboard.write('l'); |
| 85 |
} |
| 86 |
if (digitalRead(rightButton) == HIGH) { |
| 87 |
Keyboard.write('r'); |
| 88 |
} |
| 89 |
if (digitalRead(mouseButton) == HIGH) { |
| 90 |
Keyboard.write('m'); |
| 91 |
} |
| 92 |
|
| 93 |
} |