Skip to content

Instantly share code, notes, and snippets.

@bboyho
Created February 5, 2020 06:11
Show Gist options
  • Save bboyho/56d4cd1bde6ce5dd841e78f1982db570 to your computer and use it in GitHub Desktop.
Save bboyho/56d4cd1bde6ce5dd841e78f1982db570 to your computer and use it in GitHub Desktop.
/*
This is a example written for the SparkFun Qwiic Joystick
SparkFun sells these at its website: www.sparkfun.com
Do you like this library? Help support SparkFun. Buy a board!
https://www.sparkfun.com/products/15168
Written by Wes Furuya @ SparkFun Electronics, February 5th, 2019
The Qwiic Joystick is a I2C controlled analog joystick
Example 1- Basic Readings:
This program uses the Qwiic Joystick Arduino Library to read the current
joystick position and button state, which is then printed out in the Serial
Monitor.
https://github.com/sparkfun/SparkFun_Qwiic_Joystick_Arduino_Library/examples
Development environment specifics:
Arduino IDE 1.8.5
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Mouse.h>
#include <Keyboard.h>
#include <Wire.h>
#include "SparkFun_Qwiic_Joystick_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_joystick
JOYSTICK joystick; //Create instance of this object
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;
void setup() {
//Serial.begin(9600);
//Serial.println("Modified Qwiic Joystick Example 1 using HID");
if (joystick.begin() == false)
{
Serial.println("Joystick does not appear to be connected. Please check wiring. Freezing...");
while (1);
}
delay(1000); // short delay to let outputs settle
vertZero = joystick.getVertical(); // get the initial values
horzZero = joystick.getHorizontal(); // Joystick should be in neutral position when reading these
}
void loop() {
//Serial.print("X: ");
//Serial.print(joystick.getHorizontal());
//Serial.print(" Y: ");
//Serial.print(joystick.getVertical());
//Serial.print(" Button: ");
//Serial.println(joystick.getButton());
vertValue = joystick.getVertical() - vertZero; // read vertical offset
horzValue = joystick.getHorizontal() - horzZero; // read horizontal offset
if (vertValue != 0)
Mouse.move(0, vertValue / sensitivity, 0); // move mouse on y axis
if (horzValue != 0)
Mouse.move(horzValue / sensitivity, 0, 0); // move mouse on x axis
if ((joystick.getButton() == 0) && (!mouseClickFlag)) // if the joystick button is pressed
{
//mouseClickFlag = 1;
//Mouse.press(MOUSE_LEFT); // click the left button down
Keyboard.write('z');
}
else if ((joystick.getButton()) && (mouseClickFlag)) // if the joystick button is not pressed
{
//mouseClickFlag = 0;
//Mouse.release(MOUSE_LEFT); // release the left button
}
//delay(200);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment