else if ((digitalRead(selPin)) && (mouseClickFlag)) // if the joystick button is not pressed
{
mouseClickFlag = 0;
Mouse.release(MOUSE_LEFT); // release the left button
}
}
This sketch is set up so that an analog joystick connected to analog pins A0 and A1 can be used to move your
mouse cursor.
The
loop()
of this code continuously monitors the horizontal and vertical analog values of the joystick and sends
the
Mouse.move()
command based on what it reads. It'll move the mouse in steps, depending on what the
sensitivity variable is set to. With sensitivity set to 2, the cursor will move in either 1 or 2 pixel steps. Depending on
the orientation of the joystick, there is also an option to invert the mouse based on how the "V" is pointing by
adjusting
invertMouse
.
The select switch on the joystick is used to control the mouse left click. Notice this code is using
Mouse.press()
and
Mouse.release()
, rather than just calling a single
Mouse.click()
. This requires a bit more coding, but it
allows you to do things like drag-and-drop, double click, etc.
For more HID example code, check out the Arduino-supplied examples under the
File
>
Examples
>
09.USB
menu. Or check out Arduino's reference language under USB for more information.
ARDUINO LANGUAGE REFERENCE > USB
Example 3: Qwiic Enabled HID Mouse and Keyboard
Note:
If you have not previously installed an Arduino library, please check out our installation guide.
As another option, you can try to use a Qwiic device to take advantage of the HID class. We broke up the Qwiic
HID examples into three parts. Depending on your application and personal preference, you could essentially use
a different Qwiic enabled device.
Example 3a: HID Mouse with Qwiic Joystick
Example 3b: HID Keyboard with Qwiic Keypad
Example 3c: Qwiic HID Mouse and Keyboard Combined
Example 3a: HID Mouse with Qwiic Joystick
For the scope of this tutorial example, we will be using the Qwiic Joystick so that we do not need to solder any
pins. Make sure to check out the Qwiic Joystick Hookup Guide before continuing on with the examples below.