Created
September 7, 2026 15:50
-
-
Save yazdipour/a52ab7bcbc6d778a6d43527975695e76 to your computer and use it in GitHub Desktop.
Modified version of https://makerworld.com/en/models/2474475-simple-cad-joystick#profileId-2717096
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
| #include <Mouse.h> | |
| #include <Keyboard.h> | |
| #define X A0 | |
| #define Y A1 | |
| #define SW 2 | |
| #define DEAD 25 | |
| #define MOUSE_SPEED 10 | |
| #define TICK 10 | |
| #define LOCK 150 | |
| bool pan = 0; | |
| bool moving = 0; | |
| int prev = HIGH; | |
| unsigned long tToggle = 0; | |
| unsigned long tTick = 0; | |
| inline void apply() { | |
| if (!pan) | |
| Keyboard.press(KEY_LEFT_SHIFT); | |
| } | |
| inline void releaseAll() { | |
| Keyboard.release(KEY_LEFT_SHIFT); | |
| } | |
| void setup() { | |
| Keyboard.begin(); | |
| Mouse.begin(); | |
| pinMode(SW, INPUT_PULLUP); | |
| } | |
| void loop() { | |
| unsigned long now = millis(); | |
| int b = digitalRead(SW); | |
| if (prev == HIGH && b == LOW && now - tToggle > LOCK) { | |
| tToggle = now; | |
| pan = !pan; | |
| if (moving) { | |
| releaseAll(); | |
| apply(); | |
| } | |
| } | |
| prev = b; | |
| if (now - tTick < TICK) | |
| return; | |
| tTick = now; | |
| int dx = analogRead(X) - 512; | |
| int dy = analogRead(Y) - 512; | |
| if (abs(dx) > DEAD || abs(dy) > DEAD) { | |
| if (!moving) { | |
| moving = 1; | |
| apply(); | |
| } | |
| int mouseX = (dx * MOUSE_SPEED) / 512; | |
| int mouseY = (dy * MOUSE_SPEED) / 512; | |
| Mouse.move(mouseX, mouseY, 0); | |
| } | |
| else if (moving) { | |
| moving = 0; | |
| releaseAll(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment