2011-12-16 21:58:42 +01:00
|
|
|
/*
|
|
|
|
JoystickMouseControl
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
Controls the mouse from a joystick on an Arduino Leonardo, Micro or Due.
|
2017-07-14 21:34:00 +02:00
|
|
|
Uses a pushbutton to turn on and off mouse control, and a second pushbutton
|
|
|
|
to click the left mouse button.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
Hardware:
|
|
|
|
- 2-axis joystick connected to pins A0 and A1
|
|
|
|
- pushbuttons connected to pin D2 and D3
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-14 21:34:00 +02:00
|
|
|
The mouse movement is always relative. This sketch reads two analog inputs
|
|
|
|
that range from 0 to 1023 (or less on either end) and translates them into
|
|
|
|
ranges of -6 to 6.
|
|
|
|
The sketch assumes that the joystick resting values are around the middle of
|
|
|
|
the range, but that they vary within a threshold.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-14 21:34:00 +02:00
|
|
|
WARNING: When you use the Mouse.move() command, the Arduino takes over your
|
|
|
|
mouse! Make sure you have control before you use the command. This sketch
|
|
|
|
includes a pushbutton to toggle the mouse control state, so you can turn on
|
|
|
|
and off mouse control.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-12 22:18:23 +02:00
|
|
|
created 15 Sep 2011
|
2017-07-15 00:30:34 +02:00
|
|
|
updated 28 Mar 2012
|
|
|
|
by Tom Igoe
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-12 22:18:23 +02:00
|
|
|
This example code is in the public domain.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-12 22:18:23 +02:00
|
|
|
http://www.arduino.cc/en/Tutorial/JoystickMouseControl
|
2017-07-15 00:35:58 +02:00
|
|
|
*/
|
2011-12-16 21:58:42 +01:00
|
|
|
|
2015-07-15 16:05:28 +02:00
|
|
|
#include "Mouse.h"
|
|
|
|
|
2011-12-16 21:58:42 +01:00
|
|
|
// set pin numbers for switch, joystick axes, and LED:
|
2012-03-15 18:01:34 +01:00
|
|
|
const int switchPin = 2; // switch to turn on and off mouse control
|
|
|
|
const int mouseButton = 3; // input pin for the mouse pushButton
|
2013-10-21 09:58:40 +02:00
|
|
|
const int xAxis = A0; // joystick X axis
|
2012-03-15 18:01:34 +01:00
|
|
|
const int yAxis = A1; // joystick Y axis
|
2013-10-21 09:58:40 +02:00
|
|
|
const int ledPin = 5; // Mouse control LED
|
2011-12-16 21:58:42 +01:00
|
|
|
|
|
|
|
// parameters for reading the joystick:
|
|
|
|
int range = 12; // output range of X or Y movement
|
2012-03-15 18:01:34 +01:00
|
|
|
int responseDelay = 5; // response delay of the mouse, in ms
|
2013-10-21 09:58:40 +02:00
|
|
|
int threshold = range / 4; // resting threshold
|
|
|
|
int center = range / 2; // resting position value
|
2011-12-16 21:58:42 +01:00
|
|
|
|
2018-05-30 13:30:13 +02:00
|
|
|
bool mouseIsActive = false; // whether or not to control the mouse
|
2011-12-16 21:58:42 +01:00
|
|
|
int lastSwitchState = LOW; // previous switch state
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
pinMode(switchPin, INPUT); // the switch pin
|
2013-10-21 09:58:40 +02:00
|
|
|
pinMode(ledPin, OUTPUT); // the LED pin
|
|
|
|
// take control of the mouse:
|
2012-03-28 20:40:44 +02:00
|
|
|
Mouse.begin();
|
2011-12-16 21:58:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
// read the switch:
|
|
|
|
int switchState = digitalRead(switchPin);
|
|
|
|
// if it's changed and it's high, toggle the mouse state:
|
|
|
|
if (switchState != lastSwitchState) {
|
|
|
|
if (switchState == HIGH) {
|
|
|
|
mouseIsActive = !mouseIsActive;
|
2012-03-15 18:01:34 +01:00
|
|
|
// turn on LED to indicate mouse state:
|
|
|
|
digitalWrite(ledPin, mouseIsActive);
|
2013-10-21 09:58:40 +02:00
|
|
|
}
|
2011-12-16 21:58:42 +01:00
|
|
|
}
|
|
|
|
// save switch state for next comparison:
|
|
|
|
lastSwitchState = switchState;
|
|
|
|
|
2012-03-15 18:01:34 +01:00
|
|
|
// read and scale the two axes:
|
|
|
|
int xReading = readAxis(A0);
|
|
|
|
int yReading = readAxis(A1);
|
2011-12-16 21:58:42 +01:00
|
|
|
|
2012-03-15 18:01:34 +01:00
|
|
|
// if the mouse control state is active, move the mouse:
|
2011-12-16 21:58:42 +01:00
|
|
|
if (mouseIsActive) {
|
|
|
|
Mouse.move(xReading, yReading, 0);
|
2013-10-21 09:58:40 +02:00
|
|
|
}
|
2012-03-27 21:00:24 +02:00
|
|
|
|
2012-03-15 18:01:34 +01:00
|
|
|
// read the mouse button and click or not click:
|
2012-03-27 21:00:24 +02:00
|
|
|
// if the mouse button is pressed:
|
2012-03-15 18:01:34 +01:00
|
|
|
if (digitalRead(mouseButton) == HIGH) {
|
|
|
|
// if the mouse is not pressed, press it:
|
|
|
|
if (!Mouse.isPressed(MOUSE_LEFT)) {
|
2013-10-21 09:58:40 +02:00
|
|
|
Mouse.press(MOUSE_LEFT);
|
2012-03-15 18:01:34 +01:00
|
|
|
}
|
2013-10-21 09:58:40 +02:00
|
|
|
}
|
2012-03-15 18:01:34 +01:00
|
|
|
// else the mouse button is not pressed:
|
|
|
|
else {
|
|
|
|
// if the mouse is pressed, release it:
|
|
|
|
if (Mouse.isPressed(MOUSE_LEFT)) {
|
2013-10-21 09:58:40 +02:00
|
|
|
Mouse.release(MOUSE_LEFT);
|
2012-03-15 18:01:34 +01:00
|
|
|
}
|
|
|
|
}
|
2012-03-27 21:00:24 +02:00
|
|
|
|
2011-12-16 21:58:42 +01:00
|
|
|
delay(responseDelay);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-07-14 21:34:00 +02:00
|
|
|
reads an axis (0 or 1 for x or y) and scales the analog input range to a range
|
|
|
|
from 0 to <range>
|
2017-07-15 00:35:58 +02:00
|
|
|
*/
|
2011-12-16 21:58:42 +01:00
|
|
|
|
2013-10-21 09:58:40 +02:00
|
|
|
int readAxis(int thisAxis) {
|
2011-12-16 21:58:42 +01:00
|
|
|
// read the analog input:
|
2012-03-15 18:01:34 +01:00
|
|
|
int reading = analogRead(thisAxis);
|
2011-12-16 21:58:42 +01:00
|
|
|
|
|
|
|
// map the reading from the analog input range to the output range:
|
2012-03-15 18:01:34 +01:00
|
|
|
reading = map(reading, 0, 1023, 0, range);
|
|
|
|
|
2017-07-14 21:34:00 +02:00
|
|
|
// if the output reading is outside from the rest position threshold, use it:
|
2012-03-15 18:01:34 +01:00
|
|
|
int distance = reading - center;
|
2012-03-27 21:00:24 +02:00
|
|
|
|
2012-03-15 18:01:34 +01:00
|
|
|
if (abs(distance) < threshold) {
|
|
|
|
distance = 0;
|
2013-10-21 09:58:40 +02:00
|
|
|
}
|
2012-03-15 18:01:34 +01:00
|
|
|
|
2011-12-16 21:58:42 +01:00
|
|
|
// return the distance for this axis:
|
|
|
|
return distance;
|
|
|
|
}
|