Serial.println("Joystick does not appear to be connected. Please check wiring. Freezing..."
);
#endif
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 t
hese
Mouse.begin(); //Init mouse emulation
}
void loop() {
#if DEBUG
Serial.print("X: ");
Serial.print(joystick.getHorizontal());
Serial.print(" Y: ");
Serial.print(joystick.getVertical());
Serial.print(" Button: ");
Serial.println(joystick.getButton());
#endif
vertValue = joystick.getVertical() - vertZero; // read vertical offset
horzValue = joystick.getHorizontal() - 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 ((joystick.getButton() == 0) && (!mouseClickFlag)) // if the joystick button is pressed
{
mouseClickFlag = 1;
Mouse.press(MOUSE_LEFT); // click the left button down
}
else if ((joystick.getButton()) && (mouseClickFlag)) // if the joystick button is not pressed
{
mouseClickFlag = 0;
Mouse.release(MOUSE_LEFT); // release the left button
}
//delay(200); //remove "//" on this line if you need a small delay
}
Moving the Qwiic joystick will move your mouse on the screen. Pressing down on the button will cause a left
mouse click. Once you stop pressing down on the joystick, it will release the left mouse click. Depending on the
orientation for the vertical, you can invert the mouse's movement using the
invertMouse
variable at the beginning
of the example code.