|
| 1 | + |
| 2 | +/* |
| 3 | + KeyboardAndMouseControl |
| 4 | + |
| 5 | + Controls the mouse from five pushbuttons on an Arduino Leonardo. |
| 6 | + |
| 7 | + Hardware: |
| 8 | + * 5 pushbuttons attached to D2, D3, D4, D5, D6 |
| 9 | + |
| 10 | + |
| 11 | + The mouse movement is always relative. This sketch reads |
| 12 | + four pushbuttons, and uses them to set the movement of the mouse. |
| 13 | + |
| 14 | + WARNING: When you use the Mouse.move() command, the Arduino takes |
| 15 | + over your mouse! Make sure you have control before you use the mouse commands. |
| 16 | + |
| 17 | + created 15 Mar 2012 |
| 18 | + modified 27 Mar 2012 |
| 19 | + by Tom Igoe |
| 20 | + |
| 21 | + this code is in the public domain |
| 22 | + |
| 23 | + */ |
| 24 | + |
| 25 | +// set pin numbers for the five buttons: |
| 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 | + // move mouse right |
| 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 | +} |
| 94 | + |
0 commit comments