Skip to main content

DesignSpark Mechanical Virtual Controller

Hi guys, have you ever used CAD software? Have you used DesignSpark Mechanical before? No matter which CAD software you have used before, I believe you also need to use the mouse as an orientation tool to preview your design as you model it. But did you ever think you could grab it and spin it with your gestures just like Tony Stark works on the Iron Man suit? Let's use Arduino to work on it.

By the way, DesignSpark has launched a new subscription plan for everyone, whether you are an explorer, creator, or engineer, there is always one for you.

DesignSpark Mechanical

Before we program the Arduino, we need to understand how to control the mouse on the DesignSpark Mechanical.

In 3D mode, we can press the mouse wheel to rotate the direction of your design. If you want to zoom in and out, you can scroll the mouse wheel to zoom the design.

https://designspark.zendesk.com/hc/en-us/articles/212037265-How-can-I-pan-and-zoom-the-design-

Mouse.h

After we know the mouse control on the DesignSpark Mechanical, let’s talk about the Mouse.h library. There are 7 functions included in the library. And in his project, I’ll mainly use 5 of them.

Known to start emulating a mouse connected to the computer. Mouse.begin() must be called before controlling the computer. To end control, use Mouse.end().

To move the cursor on a connected computer. The motion onscreen is always relative to the cursor’s current location, use Mouse.move().

To send a button press a connected computer. A press is the equivalent of clicking and continue holding the mouse button, use Move.press(). A press is cancelled with Mouse.release(). To decide which button to press, you can use MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE.

Arduino_LSM6DS3.h

In this project, I will use Arduino Nano 33 IOT to make it. The Nano 33 IoT comes with an IMU (LSM6DS3) that we can use to make the motion tracking device and implement it in this project.

Program

#include "Mouse.h"
#include <Arduino_LSM6DS3.h>

// set pin numbers for the buttons:
const int buttonPin1 = 3; //Dynamic pan
const int buttonPin2 = 4; //Zoom in
const int buttonPin3 = 5; //Zoom out

// variables will change:
int buttonState1 = 0;  // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;

int range = 5;              // output range of X or Y movement; affects movement speed
int responseDelay = 10;     // response delay of the mouse, in ms

Setup

void setup() {

  // initialize mouse control:
  Mouse.begin();  

  while (!Serial);

  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
    while (1);
  }
}

Loop

void loop() {

  float x, y, z;
  int state;
  int upState;
  int downState;
  int rightState;
  int leftState;
  int clickState;
  int wheel = 0;

  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);

Motion tracking 

As the Nano 33 IoT comes with an IMU (LSM6DS3) that we can use to make the motion tracking device in the project. To make the motion tracking device, we use the accelerometer to calculate the movement distance.

First, we need to make some measurements to see how tilt controls mouse movement in the different directions we want. Below I use "0.5" as the critical point to determine the cursor to move forward, down, right and left. *Accelerometer range is set at -4 |+4 g with -/+0.122 mg resolution in Arduino LSM6DS3 library.

After we get the direction the cursor is moving and the value on the accelerometer. Then we need to convert that to the distance the cursor has moved. We will use accelerometer value * range, the range value set earlier, which is the output range for X or Y movement; affects the speed of movement.

Finally, we can use Mouse.move() to move the cursor on the connected computer.

if (IMU.accelerationAvailable()) {
      IMU.readAcceleration(x, y, z);

      if(z<=-0.5){    //Down
        state = downState;
        Serial.println("Down");
        Serial.print("z:");
        Serial.print(z);
      }else if (y>=0.5){    //Right
        state = rightState;
        Serial.println("Right");
        Serial.print("y:");
        Serial.print(y);
      }else if (z>=0.5){    //Up
        state = upState;
        Serial.println("Up");
        Serial.print("z:");
        Serial.print(z);
      }else if (y<=-0.5){   //Left
        state = leftState;
        Serial.println("Left");
        Serial.print("y:");
        Serial.print(y);
      }    
    }

    // calculate the movement distance:
    int  xDistance = (y) * range;
    int  yDistance = (-z) * range;

    Serial.println();
    Serial.print("xDistance: ");
    Serial.println(xDistance);
    Serial.print("yDistance: ");
    Serial.println(yDistance);

    if ((xDistance != 0) || (yDistance != 0)) { //Cursor move
       Mouse.move(xDistance, yDistance, 0);
    }else{
      Mouse.release();
    }

Spin the design

Spin the design on DesignSpark Mechanical is simply pressing the mouse wheel to rotate the orientation of the design, which I mentioned earlier. Therefore, we can use the command "Mouse.press()" to control the pressing of the button connected to the computer. In the Mouse.h() library, Mouse.press() defaults to pressing the left button, which also allows changing the parameters to MOUSE_RIGHT, MOUSE_MIDDLE to control different button presses on the mouse.

if (buttonState1 == HIGH) { //Dynamic pin
      Mouse.press(MOUSE_MIDDLE);
      Serial.print("press");
      // if X or Y is non-zero, move:
      if ((xDistance != 0) || (yDistance != 0)) {
        Mouse.move(xDistance, yDistance, 0);
      }
    }
    else{
      Mouse.release(MOUSE_MIDDLE);
    }

Zoom in & out

To zoom in and out of the design, initially on the mouse we need to scroll the scroll wheel to zoom in and out. Then, in the project, I change it by clicking a button to adjust.

In the Mouse.h() library, there is a command Mouse.move(). Before, we just used Mouse.move(xDistance, yDistance, 0) to control the cursor to move to the appropriate distance on the x-axis and y-axis, but I still explain what "0" means. In fact, the syntax for Mouse.move is "Mouse.move(xVal, yVal, wheel)". So, if we want to zoom in and out of the design, we need to adjust the "wheel" value.

if (buttonState2 == HIGH) { //Zoom in
      wheel -= 1;
      Mouse.move(0, 0, wheel);
      Serial.print("Zoom in");
      delay(100);
    }else if (buttonState3 == HIGH){  //Zoom out
      wheel += 1;
      Mouse.move(0, 0, wheel);
      Serial.print("Zoom out");
      delay(100);
    }

  // a delay so the mouse doesn't move too fast:
  delay(responseDelay);
  Mouse.end();
}

Finally, I hope you enjoy this project. Last but not least, don't miss out on special offers on DesignSpark's new subscription plans.

JulianWong has not written a bio yet…