[136] | 1 | /*
|
---|
| 2 | Esplora Joystick Mouse
|
---|
| 3 |
|
---|
| 4 | This sketch shows you how to read the joystick and use it to control the movement
|
---|
| 5 | of the cursor on your computer. You're making your Esplora into a mouse!
|
---|
| 6 |
|
---|
| 7 | WARNING: this sketch will take over your mouse movement. If you lose control
|
---|
| 8 | of your mouse do the following:
|
---|
| 9 | 1) unplug the Esplora.
|
---|
| 10 | 2) open the EsploraBlink sketch
|
---|
| 11 | 3) hold the reset button down while plugging your Esplora back in
|
---|
| 12 | 4) while holding reset, click "Upload"
|
---|
| 13 | 5) when you see the message "Done compiling", release the reset button.
|
---|
| 14 |
|
---|
| 15 | This will stop your Esplora from controlling your mouse while you upload a sketch
|
---|
| 16 | that doesn't take control of the mouse.
|
---|
| 17 |
|
---|
| 18 | Created on 22 Dec 2012
|
---|
| 19 | by Tom Igoe
|
---|
| 20 | Updated 8 March 2014
|
---|
| 21 | by Scott Fitzgerald
|
---|
| 22 |
|
---|
| 23 | http://arduino.cc/en/Reference/EsploraReadJoystickSwitch
|
---|
| 24 |
|
---|
| 25 | This example is in the public domain.
|
---|
| 26 | */
|
---|
| 27 |
|
---|
| 28 | #include <Esplora.h>
|
---|
| 29 |
|
---|
| 30 | void setup()
|
---|
| 31 | {
|
---|
| 32 | Serial.begin(9600); // initialize serial communication with your computer
|
---|
| 33 | Mouse.begin(); // take control of the mouse
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | void loop()
|
---|
| 37 | {
|
---|
| 38 | int xValue = Esplora.readJoystickX(); // read the joystick's X position
|
---|
| 39 | int yValue = Esplora.readJoystickY(); // read the joystick's Y position
|
---|
| 40 | int button = Esplora.readJoystickSwitch(); // read the joystick pushbutton
|
---|
| 41 | Serial.print("Joystick X: "); // print a label for the X value
|
---|
| 42 | Serial.print(xValue); // print the X value
|
---|
| 43 | Serial.print("\tY: "); // print a tab character and a label for the Y value
|
---|
| 44 | Serial.print(yValue); // print the Y value
|
---|
| 45 | Serial.print("\tButton: "); // print a tab character and a label for the button
|
---|
| 46 | Serial.print(button); // print the button value
|
---|
| 47 |
|
---|
| 48 | int mouseX = map(xValue, -512, 512, 10, -10); // map the X value to a range of movement for the mouse X
|
---|
| 49 | int mouseY = map(yValue, -512, 512, -10, 10); // map the Y value to a range of movement for the mouse Y
|
---|
| 50 | Mouse.move(mouseX, mouseY, 0); // move the mouse
|
---|
| 51 |
|
---|
| 52 | if (button == 0) { // if the joystick button is pressed
|
---|
| 53 | Mouse.press(); // send a mouse click
|
---|
| 54 | } else {
|
---|
| 55 | Mouse.release(); // if it's not pressed, release the mouse button
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | delay(10); // a short delay before moving again
|
---|
| 59 | }
|
---|
| 60 |
|
---|