Last active
April 4, 2023 13:10
-
-
Save squirelo/272772bd6fe3e328293e66d8408faa0f to your computer and use it in GitHub Desktop.
This file contains 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
#include <TinyUSB_Mouse_and_Keyboard.h> // Include the TinyUSB_Mouse_and_Keyboard library | |
const int joystickXPin = A0; // Pin for the X-axis of the joystick | |
const int joystickYPin = A1; // Pin for the Y-axis of the joystick | |
const int joystickButtonPin = 2; // Pin for the joystick button | |
const int sensitivity = 5; // Sensitivity for mouse movements | |
int xValue = 0; // Value for the X-axis of the joystick | |
int yValue = 0; // Value for the Y-axis of the joystick | |
bool joystickButtonPressed = false; // State of the joystick button | |
void setup() { | |
Keyboard.begin(); // Initialize the Keyboard library | |
Mouse.begin(); // Initialize the Mouse library | |
Serial.begin(115200); // Initialize the serial communication | |
while (! Serial) delay(1); // Wait for the serial communication to be established | |
pinMode(joystickXPin, INPUT); // Set the X-axis pin of the joystick as an input | |
pinMode(joystickYPin, INPUT); // Set the Y-axis pin of the joystick as an input | |
pinMode(joystickButtonPin, INPUT_PULLUP); // Set the joystick button pin as an input with a pull-up resistor | |
} | |
void loop() { | |
xValue = analogRead(joystickXPin); // Read the analog value from the X-axis pin of the joystick | |
yValue = analogRead(joystickYPin); // Read the analog value from the Y-axis pin of the joystick | |
joystickButtonPressed = !digitalRead(joystickButtonPin); // Read the digital value from the joystick button pin | |
int xMouse = map(xValue, 0, 1023, -sensitivity, sensitivity); // Map the X-axis value to the range of mouse movement from -sensitivity to +sensitivity | |
int yMouse = map(yValue, 0, 1023, -sensitivity, sensitivity); // Map the Y-axis value to the range of mouse movement from -sensitivity to +sensitivity | |
if (abs(xMouse) > sensitivity / 2 || abs(yMouse) > sensitivity / 2) { | |
Mouse.move(xMouse, yMouse); // If the mapped values exceed half of the sensitivity, move the mouse by xMouse and yMouse | |
} | |
if (joystickButtonPressed) { | |
Mouse.press(MOUSE_LEFT); // If the joystick button is pressed, press the left button of the mouse | |
} else { | |
Mouse.release(MOUSE_LEFT); // If the joystick button is not pressed, release the left button of the mouse | |
} | |
delay(10); // Add a small delay to avoid overwhelming the loop | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment