-
Notifications
You must be signed in to change notification settings - Fork 32
Create JoystickMouseControl.ino #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AsherThomasBabu
wants to merge
5
commits into
arduino-libraries:master
Choose a base branch
from
AsherThomasBabu:patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fdba0ab
Create JoystickMouseControl.ino
AsherThomasBabu 2c5485c
Update JoystickMouseControl.ino
AsherThomasBabu cda9329
Update JoystickMouseControl.ino
AsherThomasBabu d99a249
Rename JoystickMouseControl.ino to examples/JoystickMouseControl/Joys…
AsherThomasBabu 7bc8ecb
Add files via upload
AsherThomasBabu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
JoystickMouseControl | ||
|
||
Controls the mouse from a joystick on an Arduino Leonardo, Micro or Due. | ||
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 Sep 2011 | ||
updated 28 Mar 2012 | ||
by Tom Igoe | ||
|
||
This example code is in the public domain. | ||
|
||
http://www.arduino.cc/en/Tutorial/JoystickMouseControl | ||
*/ | ||
|
||
#include "Mouse.h" | ||
|
||
// 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 | ||
AsherThomasBabu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
|
||
bool 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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.