Debian Codegraph @

examples/09.USB/Mouse/JoystickMouseControl/JoystickMouseControl.ino

raw
1 /*
2 JoystickMouseControl
3
4 Controls the mouse from a joystick on an Arduino Leonardo, Micro or Due.
5 Uses a pushbutton to turn on and off mouse control, and a second pushbutton
6 to click the left mouse button.
7
8 Hardware:
9 - 2-axis joystick connected to pins A0 and A1
10 - pushbuttons connected to pin D2 and D3
11
12 The mouse movement is always relative. This sketch reads two analog inputs
13 that range from 0 to 1023 (or less on either end) and translates them into
14 ranges of -6 to 6.
15 The sketch assumes that the joystick resting values are around the middle of
16 the range, but that they vary within a threshold.
17
18 WARNING: When you use the Mouse.move() command, the Arduino takes over your
19 mouse! Make sure you have control before you use the command. This sketch
20 includes a pushbutton to toggle the mouse control state, so you can turn on
21 and off mouse control.
22
23 created 15 Sep 2011
24 updated 28 Mar 2012
25 by Tom Igoe
26
27 This example code is in the public domain.
28
29 https://www.arduino.cc/en/Tutorial/BuiltInExamples/JoystickMouseControl
30 */
31
32 #include "Mouse.h"
33
34 // set pin numbers for switch, joystick axes, and LED:
35 const int switchPin = 2; // switch to turn on and off mouse control
36 const int mouseButton = 3; // input pin for the mouse pushButton
37 const int xAxis = A0; // joystick X axis
38 const int yAxis = A1; // joystick Y axis
39 const int ledPin = 5; // Mouse control LED
40
41 // parameters for reading the joystick:
42 int range = 12; // output range of X or Y movement
43 int responseDelay = 5; // response delay of the mouse, in ms
44 int threshold = range / 4; // resting threshold
45 int center = range / 2; // resting position value
46
47 bool mouseIsActive = false; // whether or not to control the mouse
48 int lastSwitchState = LOW; // previous switch state
49
50 void setup() {
51 pinMode(switchPin, INPUT); // the switch pin
52 pinMode(ledPin, OUTPUT); // the LED pin
53 // take control of the mouse:
54 Mouse.begin();
55 }
56
57 void loop() {
58 // read the switch:
59 int switchState = digitalRead(switchPin);
60 // if it's changed and it's high, toggle the mouse state:
61 if (switchState != lastSwitchState) {
62 if (switchState == HIGH) {
63 mouseIsActive = !mouseIsActive;
64 // turn on LED to indicate mouse state:
65 digitalWrite(ledPin, mouseIsActive);
66 }
67 }
68 // save switch state for next comparison:
69 lastSwitchState = switchState;
70
71 // read and scale the two axes:
72 int xReading = readAxis(A0);
73 int yReading = readAxis(A1);
74
75 // if the mouse control state is active, move the mouse:
76 if (mouseIsActive) {
77 Mouse.move(xReading, yReading, 0);
78 }
79
80 // read the mouse button and click or not click:
81 // if the mouse button is pressed:
82 if (digitalRead(mouseButton) == HIGH) {
83 // if the mouse is not pressed, press it:
84 if (!Mouse.isPressed(MOUSE_LEFT)) {
85 Mouse.press(MOUSE_LEFT);
86 }
87 }
88 // else the mouse button is not pressed:
89 else {
90 // if the mouse is pressed, release it:
91 if (Mouse.isPressed(MOUSE_LEFT)) {
92 Mouse.release(MOUSE_LEFT);
93 }
94 }
95
96 delay(responseDelay);
97 }
98
99 /*
100 reads an axis (0 or 1 for x or y) and scales the analog input range to a range
101 from 0 to <range>
102 */
103
104 int readAxis(int thisAxis) {
105 // read the analog input:
106 int reading = analogRead(thisAxis);
107
108 // map the reading from the analog input range to the output range:
109 reading = map(reading, 0, 1023, 0, range);
110
111 // if the output reading is outside from the rest position threshold, use it:
112 int distance = reading - center;
113
114 if (abs(distance) < threshold) {
115 distance = 0;
116 }
117
118 // return the distance for this axis:
119 return distance;
120 }