Created
January 2, 2020 23:18
-
-
Save bboyho/c187160c28a94ea8c5db95d2373e5282 to your computer and use it in GitHub Desktop.
HID Joystick Mouse Example (Modified)
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
/* HID Joystick Mouse Example | |
by: Jim Lindblom | |
date: 1/12/2012 | |
license: MIT License - Feel free to use this code for any purpose. | |
No restrictions. Just keep this license if you go on to use this | |
code in your future endeavors! Reuse and share. | |
This is very simplistic code that allows you to turn the | |
SparkFun Thumb Joystick (http://www.sparkfun.com/products/9032) | |
into an HID Mouse. The select button on the joystick is set up | |
as the mouse left click. | |
*/ | |
#include <Mouse.h> | |
int horzPin = 0; // Analog output of horizontal joystick pin | |
int vertPin = 1; // Analog output of vertical joystick pin | |
int selPin = 9; // select button pin of joystick | |
int vertZero, horzZero; // Stores the initial value of each axis, usually around 512 | |
int vertValue, horzValue; // Stores current analog output of each axis | |
const int sensitivity = 200; // Higher sensitivity value = slower mouse, should be <= about 500 | |
int mouseClickFlag = 0; | |
int invertMouse = -1; //Value to invert joystick based on orientation | |
void setup() | |
{ | |
pinMode(horzPin, INPUT); // Set both analog pins as inputs | |
pinMode(vertPin, INPUT); | |
pinMode(selPin, INPUT); // set button select pin as input | |
digitalWrite(selPin, HIGH); // Pull button select pin high | |
delay(1000); // short delay to let outputs settle | |
vertZero = analogRead(vertPin); // get the initial values | |
horzZero = analogRead(horzPin); // Joystick should be in neutral position when reading these | |
} | |
void loop() | |
{ | |
vertValue = analogRead(vertPin) - vertZero; // read vertical offset | |
horzValue = analogRead(horzPin) - horzZero; // read horizontal offset | |
if (vertValue != 0) | |
Mouse.move(0, (invertMouse * (vertValue / sensitivity)), 0); // move mouse on y axis | |
if (horzValue != 0) | |
Mouse.move((invertMouse * (horzValue / sensitivity)), 0, 0); // move mouse on x axis | |
if ((digitalRead(selPin) == 0) && (!mouseClickFlag)) // if the joystick button is pressed | |
{ | |
mouseClickFlag = 1; | |
Mouse.press(MOUSE_LEFT); // click the left button down | |
} | |
else if ((digitalRead(selPin)) && (mouseClickFlag)) // if the joystick button is not pressed | |
{ | |
mouseClickFlag = 0; | |
Mouse.release(MOUSE_LEFT); // release the left button | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment