Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Author Stefano Sbarra
- //April 2020
- //FREE FOR ANY NON COMMERCIAL PROJECT
- /*
- * Modified by Tommy Vanhullebusch
- * December 2020
- *
- */
- //THIS SKETCH MAKES ARDUINO LEONARDO (OR MICRO) WORK AS A SPACE 3D MOUSE FOR INVENTOR AND FUSION 360
- //NORMAL JOYSTICK MOVE ACT AS VIEW ROTATION USING F4 KEY
- //PRESS AND MOVE ACT AS VIEW ZOOM USING F3 KEY
- //QUICK PRESS OF THE JOYSTICK GOES TO HOME VIEW USING F6
- //ADDITIONAL FUNCTIONS AND SWITCHES ARE EASY TO IMPLEMENT (EXAMPLE SLICE VIEW)
- /*
- *
- * Actions optimised for use with Fusion 360
- *
- * Joystick move: Orbit (Shift-Middle Mouse button-Mouse Movement
- * Joystick move while pressed: Pan (Middel Mouse button-Mouse Movement
- * Joystick click: Zoom all (F6)
- * Joystick double-click: not implemented
- * Button 1: Zoom in (Mouse Scroll down)
- * Button 2: Zoom out (Mouse Scroll up)
- * Button 3: New sketch (ctrl-alt-s made a new shortcut for this)
- * The mouse can be turned ON or OFF by pressing button 3 for 2 seconds
- *
- */
- #include <Mouse.h>
- #include <Keyboard.h>
- int horzPin = A1; // Analog output of horizontal joystick pin
- int vertPin = A0; // Analog output of vertical joystick pin
- int selPin = 10; // select button pin of joystick
- int b1Pin = 7; // button 1
- int b2Pin = 8; // button 2
- int b3Pin = 9; // button 3
- char f6key = KEY_F6; // zoom to all
- char shiftkey = KEY_LEFT_SHIFT; // Needed for orbit
- char ctrlkey = KEY_LEFT_CTRL; // Needed for custom shortcut
- char altkey = KEY_LEFT_ALT; // Needed for custom shortcut
- int moved = 0; // checks if the joystick has moved
- int clicked = 0; // is mouse button clicked or not
- int pressed = 0; // checks if the joystick has been pressed
- int mouseOn = 0; // flag to signal that the mouse is on or off
- unsigned long previousMillis = 0; // Used for slowing down the zoom buttons
- const long zoomDelay = 70; // Delay between two zoom actions
- int vertZero, horzZero; // Stores the initial value of each axis, usually around 512
- int vertValue, horzValue; // Stores current analog output of each axis
- const int sensitivity = 200; // Higher sensitivity value = slower mouse, should be <= about 500
- int mouseClickFlag = 0; // another click check
- int b1flag = 0; // click check
- int b2flag = 0; // click check
- int b3flag = 0; // click check
- //int invertMouse = 1; // Invert joystick based on orientation
- int invertMouse = -1; // Noninverted joystick based on orientation
- unsigned long releasetime = 0; // used to register the releasing time (to select "zoom all" or orbit/pan)
- unsigned long presstime = 0; // as above
- unsigned long btn3released = 0; // used to register the releasing time of button 3
- unsigned long btn3pressed = 0; // as above
- unsigned long diff = 0; // difference between time checks to read quick or long press
- unsigned long nextMove = 0; // time to next move
- const long mouseDelay = 10; // delay to slow down the mouse movements
- const long interval = 500; // interval to establish if quick or long
- void setup()
- {
- pinMode(horzPin, INPUT); // Set both analog pins as inputs
- pinMode(vertPin, INPUT);
- pinMode(selPin, INPUT); // set button select pin as input
- pinMode(b1Pin, INPUT); // set button 1 pin as input
- pinMode(b2Pin, INPUT); // set button 2 pin as input
- pinMode(b3Pin, INPUT); // set button 3 pin as input
- digitalWrite(selPin, HIGH); // Pull button select pin high
- digitalWrite(b1Pin, HIGH); // Pull button 1 pin high
- digitalWrite(b2Pin, HIGH); // Pull button 2 pin high
- digitalWrite(b3Pin, HIGH); // Pull button 3 pin high
- delay(1000); // short delay to let outputs settle
- vertZero = analogRead(vertPin); // get the initial values
- horzZero = analogRead(horzPin); // Joystick should be in neutral position when reading these
- Mouse.begin(); // Init mouse emulation
- Keyboard.begin(); // Init keyboard emulation
- }
- void loop()
- {
- unsigned long currentMillis = millis();
- vertValue = (analogRead(vertPin) - vertZero) * invertMouse; // read vertical offset
- horzValue = (analogRead(horzPin) - horzZero) * invertMouse; // read horizontal offset
- //BUTTON 1
- if ((mouseOn == 1) && (digitalRead(b1Pin) == LOW) && (currentMillis > (previousMillis + zoomDelay))) // if button 1 is pressed
- {
- Mouse.move (0, 0, -1);
- previousMillis = currentMillis;
- }
- //BUTTON 2
- if ((mouseOn == 1) && (digitalRead(b2Pin) == LOW) && (currentMillis > (previousMillis + zoomDelay))) // if button 2 is pressed
- {
- Mouse.move (0, 0, 1);
- previousMillis = currentMillis;
- }
- //BUTTON 3
- if ((digitalRead(b3Pin) == LOW) && (!b3flag)) // if button 3 is pressed
- {
- b3flag = 1;
- btn3pressed = currentMillis;
- }
- if ((digitalRead(b3Pin) == HIGH) && (b3flag))
- {
- b3flag = 0;
- btn3released = currentMillis;
- diff = btn3released - btn3pressed;
- if (diff > 1000) {
- // Long press to turn the Space mouse ON or OFF
- if (mouseOn == 1) {
- mouseOn = 0;
- }
- else {
- mouseOn = 1;
- }
- }
- else if (mouseOn == 1) {
- Keyboard.press(ctrlkey);
- Keyboard.press(altkey);
- Keyboard.press('s');
- delay(50);
- Keyboard.releaseAll();
- }
- delay(10);
- }
- //JOYSTICK CLICK
- if ((digitalRead(selPin) == LOW) && (!mouseClickFlag)) // if the joystick button is pressed
- {
- presstime = currentMillis;
- mouseClickFlag = 1;
- pressed = 1;
- }
- if ((digitalRead(selPin) == HIGH) && (mouseClickFlag)) // if the joystick button is not pressed
- {
- releasetime = currentMillis;
- mouseClickFlag = 0;
- pressed = 0;
- diff = (releasetime - presstime); // check if the click was quick or long
- if ((mouseOn == 1) && (diff < interval) && (diff > 20)) // if quick call zoom all (F6)
- {
- Keyboard.write(f6key);
- }
- }
- if (currentMillis > nextMove) {
- if (((vertValue > 2)||(vertValue < (-2))) && (currentMillis > nextMove)){ // position two instead of 0 cancel the small fluctuation between readings
- moved = 1;
- }
- else {
- vertValue = 0;
- }
- if (((horzValue > 2)||(horzValue < (-2))) && (currentMillis > nextMove)){
- moved = 1;
- }
- else {
- horzValue = 0;
- }
- if ((mouseOn == 1) && (clicked == 1) && (vertValue <= 2) && (vertValue >= (-2)) && (horzValue <= 2) && (horzValue >= (-2)) ){ // if joystick is in neutral position release all
- clicked = 0;
- Keyboard.releaseAll();
- Mouse.release(MOUSE_MIDDLE);
- }
- }
- if (moved == 1) { // move mouse with middle mousebutton pressed to orbit/pan view
- moved = 0;
- nextMove = currentMillis + mouseDelay;
- if (mouseOn == 1) {
- if (clicked == 0) {
- clicked = 1;
- if (pressed == 0) { // press shift key to orbit else release it to pan
- Keyboard.press(shiftkey);
- delay(5);
- }
- Mouse.press(MOUSE_MIDDLE);
- delay(5);
- }
- Mouse.move((horzValue / sensitivity), (vertValue / sensitivity), 0);
- }
- }
- }
RAW Paste Data