Tutorial.JoystickMouseControl History

Hide minor edits - Show changes to output

October 31, 2012, at 09:58 PM by Scott Fitzgerald -
Changed lines 4-7 from:
Using the Mouse library, you can controls a computer's onscreen cursor with an Arduino Leonardo. This particular example uses a pushbutton to turn on and off mouse control with a joystick.

Cursor movement from the Leonardo is always relative. So every time the analog input is read, the cursor's position is updated relative to it's current position.
to:
Using the Mouse library, you can controls a computer's onscreen cursor with an Arduino Leonardo, Micro, or Due. This particular example uses a pushbutton to turn on and off mouse control with a joystick.

Cursor movement from the Arduino is always relative. So every time the analog input is read, the cursor's position is updated relative to it's current position.
Changed lines 10-11 from:
The pushbutton allows you to toggle mouse control on and off. A status LED connected to pin 5 lights when the Leonardo is controlling the mouse.
to:
The pushbutton allows you to toggle mouse control on and off. A status LED connected to pin 5 lights when the Arduino is controlling the mouse.
Changed line 16 from:
* Arduino Leonardo board
to:
* Arduino Leonardo, Micro, or Due board
Deleted line 39:
Changed lines 41-46 from:



(:source lang=arduino tabwidth=4:)
/*
JoystickMouseControl
to:

(:source http://arduino.cc/en/pub/code/master/build/shared/examples/09.USB/Mouse/JoystickMouseControl/JoystickMouseControl.ino lang=arduino tabwidth=4:)
Deleted lines 43-161:
Controls the mouse from a joystick on an Arduino Leonardo.
Uses a pushbutton to turn on and off mouse control, and
a second pushbutton to click the left mouse button

Hardware:
* 2-axis joystick connected to pins A0 and A1
* pushbuttons connected to pin D2 and D3

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.

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.

created 15 Sept 2011
updated 28 Mar 2012
by Tom Igoe

this code is in the public domain

*/

// set pin numbers for switch, joystick axes, and LED:
const int switchPin = 2; // switch to turn on and off mouse control
const int mouseButton = 3; // input pin for the mouse pushButton
const int xAxis = A0; // joystick X axis
const int yAxis = A1; // joystick Y axis
const int ledPin = 5; // Mouse control LED

// parameters for reading the joystick:
int range = 12; // output range of X or Y movement
int responseDelay = 5; // response delay of the mouse, in ms
int threshold = range/4; // resting threshold
int center = range/2; // resting position value

boolean mouseIsActive = false; // whether or not to control the mouse
int lastSwitchState = LOW; // previous switch state

void setup() {
pinMode(switchPin, INPUT); // the switch pin
pinMode(ledPin, OUTPUT); // the LED pin
// take control of the mouse:
Mouse.begin();
}

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;
// turn on LED to indicate mouse state:
digitalWrite(ledPin, mouseIsActive);
}
}
// save switch state for next comparison:
lastSwitchState = switchState;

// read and scale the two axes:
int xReading = readAxis(A0);
int yReading = readAxis(A1);

// if the mouse control state is active, move the mouse:
if (mouseIsActive) {
Mouse.move(xReading, yReading, 0);
}

// read the mouse button and click or not click:
// if the mouse button is pressed:
if (digitalRead(mouseButton) == HIGH) {
// if the mouse is not pressed, press it:
if (!Mouse.isPressed(MOUSE_LEFT)) {
Mouse.press(MOUSE_LEFT);
}
}
// else the mouse button is not pressed:
else {
// if the mouse is pressed, release it:
if (Mouse.isPressed(MOUSE_LEFT)) {
Mouse.release(MOUSE_LEFT);
}
}

delay(responseDelay);
}

/*
reads an axis (0 or 1 for x or y) and scales the
analog input range to a range from 0 to <range>
*/

int readAxis(int thisAxis) {
// read the analog input:
int reading = analogRead(thisAxis);

// map the reading from the analog input range to the output range:
reading = map(reading, 0, 1023, 0, range);

// if the output reading is outside from the
// rest position threshold, use it:
int distance = reading - center;

if (abs(distance) < threshold) {
distance = 0;
}

// return the distance for this axis:
return distance;
}



(:sourceend:)
Changed line 52 from:
(:include LeonardoSeeAlsoInclude:)
to:
(:include LeonardoSeeAlsoInclude:)
May 21, 2012, at 07:42 PM by Scott Fitzgerald -
Changed lines 42-167 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/09.USB(Leonardo)/Mouse/JoystickMouseControl/JoystickMouseControl.ino lang=arduino tabwidth=4:)
to:



(:source lang=arduino tabwidth=4:)
/*
JoystickMouseControl

Controls the mouse from a joystick on an Arduino Leonardo.
Uses a pushbutton to turn on and off mouse control, and
a second pushbutton to click the left mouse button

Hardware:
* 2-axis joystick connected to pins A0 and A1
* pushbuttons connected to pin D2 and D3

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.

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.

created 15 Sept 2011
updated 28 Mar 2012
by Tom Igoe

this code is in the public domain

*/

// set pin numbers for switch, joystick axes, and LED:
const int switchPin = 2; // switch to turn on and off mouse control
const int mouseButton = 3; // input pin for the mouse pushButton
const int xAxis = A0; // joystick X axis
const int yAxis = A1; // joystick Y axis
const int ledPin = 5; // Mouse control LED

// parameters for reading the joystick:
int range = 12; // output range of X or Y movement
int responseDelay = 5; // response delay of the mouse, in ms
int threshold = range/4; // resting threshold
int center = range/2; // resting position value

boolean mouseIsActive = false; // whether or not to control the mouse
int lastSwitchState = LOW; // previous switch state

void setup() {
pinMode(switchPin, INPUT); // the switch pin
pinMode(ledPin, OUTPUT); // the LED pin
// take control of the mouse:
Mouse.begin();
}

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;
// turn on LED to indicate mouse state:
digitalWrite(ledPin, mouseIsActive);
}
}
// save switch state for next comparison:
lastSwitchState = switchState;

// read and scale the two axes:
int xReading = readAxis(A0);
int yReading = readAxis(A1);

// if the mouse control state is active, move the mouse:
if (mouseIsActive) {
Mouse.move(xReading, yReading, 0);
}

// read the mouse button and click or not click:
// if the mouse button is pressed:
if (digitalRead(mouseButton) == HIGH) {
// if the mouse is not pressed, press it:
if (!Mouse.isPressed(MOUSE_LEFT)) {
Mouse.press(MOUSE_LEFT);
}
}
// else the mouse button is not pressed:
else {
// if the mouse is pressed, release it:
if (Mouse.isPressed(MOUSE_LEFT)) {
Mouse.release(MOUSE_LEFT);
}
}

delay(responseDelay);
}

/*
reads an axis (0 or 1 for x or y) and scales the
analog input range to a range from 0 to <range>
*/

int readAxis(int thisAxis) {
// read the analog input:
int reading = analogRead(thisAxis);

// map the reading from the analog input range to the output range:
reading = map(reading, 0, 1023, 0, range);

// if the output reading is outside from the
// rest position threshold, use it:
int distance = reading - center;

if (abs(distance) < threshold) {
distance = 0;
}

// return the distance for this axis:
return distance;
}



(:sourceend:)
May 02, 2012, at 04:15 PM by Scott Fitzgerald -
Changed line 42 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/09.Mouse/JoystickMouseControl/JoystickMouseControl.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/09.USB(Leonardo)/Mouse/JoystickMouseControl/JoystickMouseControl.ino lang=arduino tabwidth=4:)
April 27, 2012, at 06:32 PM by Scott Fitzgerald -
Changed line 51 from:
(:include LeonardoSeeAlsoIncludes:)
to:
(:include LeonardoSeeAlsoInclude:)
April 27, 2012, at 06:30 PM by Scott Fitzgerald -
Changed line 32 from:
%height=300px%[[Attach:JoystickMouse_bb.png | Attach:JoystickMouse_bb.png]]
to:
%height=300px%[[Attach:JoystickMouse_2_bb.png | Attach:JoystickMouse_2_bb.png]]
Changed line 42 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/10.Mouse/JoystickMouseControl/JoystickMouseControl.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/09.Mouse/JoystickMouseControl/JoystickMouseControl.ino lang=arduino tabwidth=4:)
Changed line 51 from:
\\
to:
(:include LeonardoSeeAlsoIncludes:)
March 28, 2012, at 08:24 PM by Scott Fitzgerald -
Changed lines 4-5 from:
With the Mouse library, you can controls a computer's onscreen cursor with an Arduino Leonardo. This particular example uses a pushbutton to turn on and off mouse control with a joystick.
to:
Using the Mouse library, you can controls a computer's onscreen cursor with an Arduino Leonardo. This particular example uses a pushbutton to turn on and off mouse control with a joystick.
Changed lines 8-9 from:
Two analog inputs ranging from 0 to 1023 are translated to ranges of -60 to 60. The sketch assumes that the joystick resting values are around the middle of the range, but that they vary within a threshold.
to:
Two analog inputs ranging from 0 to 1023 are translated to ranges of -12 to 12. The sketch assumes that the joystick resting values are around the middle of the range, but that they vary within a threshold.

The pushbutton allows you to toggle mouse control on and off. A status LED connected to pin 5 lights when the Leonardo is controlling the mouse.
December 07, 2011, at 01:43 AM by Scott Fitzgerald -
Changed lines 43-48 from:
to:
'''See Also'''
*[[/Reference/MouseClick | Mouse.click]]()
*[[/Reference/MouseMove | Mouse.move]]()
*[[/Reference/MousePress | Mouse.press]]()
*[[/Reference/MouseRelease | Mouse.release]]()
*[[/Reference/MouseIsPressed | Mouse.isPressed]]()
November 28, 2011, at 09:06 PM by Scott Fitzgerald -
Changed line 40 from:
(:source http://arduino.cc/en/pub/code/master/libraries/Mouse/examples/JoystickMouseControl/JoystickMouseControl.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/10.Mouse/JoystickMouseControl/JoystickMouseControl.ino lang=arduino tabwidth=4:)
November 22, 2011, at 03:25 AM by Scott Fitzgerald -
Changed lines 10-11 from:
''NB: When you use the Mouse.move() command, the Arduino takes over your computer's cursor! To insure you don't lose control of your computer while running a sketch with this function, make sure to set up a controller before you call Mouse.move(). This sketch includes a pushbutton to toggle the mouse control state, so you can turn on and off mouse control.''
to:
'''NB: When you use the Mouse.move() command, the Arduino takes over your computer's cursor! To insure you don't lose control of your computer while running a sketch with this function, make sure to set up a controller before you call Mouse.move(). This sketch includes a pushbutton to toggle the mouse control state, so you can turn on and off mouse control.'''
November 22, 2011, at 03:25 AM by Scott Fitzgerald -
Changed lines 10-11 from:
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.
to:
''NB: When you use the Mouse.move() command, the Arduino takes over your computer's cursor! To insure you don't lose control of your computer while running a sketch with this function, make sure to set up a controller before you call Mouse.move(). This sketch includes a pushbutton to toggle the mouse control state, so you can turn on and off mouse control.''
November 22, 2011, at 03:06 AM by Scott Fitzgerald -
Changed lines 4-9 from:
Controls the mouse from a joystick on an Arduino Leonardo. Uses a pushbutton to turn on and off mouse control.

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 -60 to 60. The sketch assumes that the joystick resting values are around the middle of the range, but that they vary within a threshold.

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.
to:
With the Mouse library, you can controls a computer's onscreen cursor with an Arduino Leonardo. This particular example uses a pushbutton to turn on and off mouse control with a joystick.

Cursor movement from the Leonardo is always relative. So every time the analog input is read, the cursor's position is updated relative to it's current position.

Two analog inputs ranging from 0 to 1023 are translated to ranges of -60 to 60. The sketch assumes that the joystick resting values are around the middle of the range, but that they vary within a threshold.

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.
November 22, 2011, at 12:42 AM by Scott Fitzgerald -
Changed lines 31-32 from:
%height=300px%[[Attach:joystickMouse_schem.png | Attach:joystickMouse.png]]
to:
%height=300px%[[Attach:joystickMouse_schem.png | Attach:joystickMouse_schem.png]]
November 22, 2011, at 12:41 AM by Scott Fitzgerald -
Changed lines 23-25 from:
Connect your Leonardo board to your computer with a micro-USB cable. The pushbutton is connected to pin 6. If you're using a part like the Joystick shield pictured below, you may not need a pulldown resistor. The x-axis on the joystick is connected to analog in 0, the y-axis iss on analog in 1.

to:
(:div class=Circuit:)
Connect your Leonardo board to your computer with a micro-USB cable. The pushbutton is connected to pin 6. If you're using a part like the Joystick shield pictured below, you may not need a pulldown resistor. The x-axis on the joystick is connected to analog in 0, the y-axis is on analog in 1.

[-click the images to enlarge-]

%height=300px%[[Attach:JoystickMouse_bb.png | Attach:JoystickMouse_bb.png]]
[-image developed using [[http://www.fritzing.org |Fritzing]]. For more circuit examples, see the [[http://fritzing.org/projects/|Fritzing project page]] -]

%height=300px%[[Attach:joystickMouse_schem.png | Attach:joystickMouse.png]]

(:divend:)
November 19, 2011, at 07:55 PM by Scott Fitzgerald -
Changed lines 15-16 from:
* One 10-kilohm resistor
to:
* One 10-kilohm resistor (if needed)
Changed lines 23-25 from:
Connect your Leonardo board to your computer with a micro-USB cable.

to:
Connect your Leonardo board to your computer with a micro-USB cable. The pushbutton is connected to pin 6. If you're using a part like the Joystick shield pictured below, you may not need a pulldown resistor. The x-axis on the joystick is connected to analog in 0, the y-axis iss on analog in 1.

November 19, 2011, at 07:15 PM by Scott Fitzgerald -
Changed lines 4-9 from:
Controls the mouse from a joystick on an Arduino Leonardo. Uses a pushbutton to turn on and off mouse control.

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 -60 to 60. The sketch assumes that the joystick resting values are around the middle of the range, but that they vary within a threshold.

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.
to:
Controls the mouse from a joystick on an Arduino Leonardo. Uses a pushbutton to turn on and off mouse control.

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 -60 to 60. The sketch assumes that the joystick resting values are around the middle of the range, but that they vary within a threshold.

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.
November 19, 2011, at 07:14 PM by Scott Fitzgerald -
November 19, 2011, at 07:14 PM by Scott Fitzgerald -
Changed lines 4-5 from:
This example listens for a byte coming from the serial port. When received, the Leonardo send a keystroke back to the computer. The sent keystroke is one higher than what is received, so if you send an "a" from the serial monitor, you'll receive a "b" from the Leonardo. A "1" will return a "2" and so on.
to:
Controls the mouse from a joystick on an Arduino Leonardo. Uses a pushbutton to turn on and off mouse control.

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 -60 to 60. The sketch assumes that the joystick resting values are around the middle of the range, but that they vary within a threshold.

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.
Changed lines 13-16 from:
to:
* 2 axis joystick
* momentary pushbutton
* One 10-kilohm resistor
November 19, 2011, at 06:43 PM by Scott Fitzgerald -
Changed line 22 from:
(:source http://arduino.cc/en/pub/code/master/libraries/Mouse/examples/JoustickMouseControl/JoustickMouseControl.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/libraries/Mouse/examples/JoystickMouseControl/JoystickMouseControl.ino lang=arduino tabwidth=4:)
November 19, 2011, at 06:43 PM by Scott Fitzgerald -
Changed line 22 from:
(:source http://arduino.cc/en/pub/code/master/libraries/Mouse/JoustickMouseControl/JoustickMouseControl.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/libraries/Mouse/examples/JoustickMouseControl/JoustickMouseControl.ino lang=arduino tabwidth=4:)
November 19, 2011, at 06:42 PM by Scott Fitzgerald -
Added lines 1-26:
''Examples > Joystick ''

!!!Joystick Mouse Control
This example listens for a byte coming from the serial port. When received, the Leonardo send a keystroke back to the computer. The sent keystroke is one higher than what is received, so if you send an "a" from the serial monitor, you'll receive a "b" from the Leonardo. A "1" will return a "2" and so on.

(:div class=BOM :)
'''Hardware Required'''
* Arduino Leonardo board

'''Software Required'''
*none

(:divend:)

'''Circuit'''
Connect your Leonardo board to your computer with a micro-USB cable.


'''Code'''

(:div class=code :)
(:source http://arduino.cc/en/pub/code/master/libraries/Mouse/JoustickMouseControl/JoustickMouseControl.ino lang=arduino tabwidth=4:)
(:divend:)


\\

Share