#include #include #include #include USBHIDMouse Mouse; #define LED_BUILTIN 48 const int buttonPin = 0; int previousButtonState = HIGH; void smoothMouseMove(int xOffset, int yOffset, unsigned long durationMs, float steps) { if (steps <= 0) { // Avoid division by zero or infinite loop if steps is non-positive Mouse.move(xOffset, yOffset); // Fallback to instant move return; } // Calculate the movement per step for X and Y float xStep = (float)xOffset / steps; float yStep = (float)yOffset / steps; // Calculate the delay between each step unsigned long delayPerStep = durationMs / steps; // Perform the movement in small steps for (int i = 0; i < steps; i++) { // Move the mouse by the calculated step amount. // We cast to int because Mouse.move expects integers, // but float calculations ensure more accurate distribution over steps. Mouse.move((int)xStep, (int)yStep); delay(delayPerStep); // Pause for a short duration } // Handle any remaining fractional movement due to integer casting // This ensures the mouse ends up exactly at the target offset int remainingX = xOffset - (int)(xStep * steps); int remainingY = yOffset - (int)(yStep * steps); if (remainingX != 0 || remainingY != 0) { Mouse.move(remainingX, remainingY); } } void setup() { delay(1000); Serial.begin(115200); Serial.println("Starting ESP32-S3 USB Mouse"); pinMode(LED_BUILTIN, OUTPUT); USB.begin(); Mouse.begin(); delay(2000); } void loop() { int buttonState = digitalRead(buttonPin); const int desktopFullScreenWidth = 1920; // To move my mouse a whole screen width to the right on a 1080p screen const int purepixels = 6230; const int kiripurepixels = 12995; const int input = purepixels; if ((buttonState != previousButtonState) && (buttonState == LOW)) { digitalWrite(LED_BUILTIN, LOW); smoothMouseMove(input, 0, 1500, (input / 10)); delay(500); Serial.println("Button Pressed - Mouse should have sent event."); } previousButtonState = buttonState; digitalWrite(LED_BUILTIN, HIGH); }