Stepper control using mouse movement

Basic idea: move stepper motors using computer mouse through serial port. Right and left mouse movement result in x-axis stepper rotating CW and CCW, up and down mouse movement results in y-axis stepper moving.

  1. Although my driver is HBS860H but the following link uses a very similar drive, they both have the same connection to the Arduino and the simple code works exactly same on both. I am showing you this link to reference the connection to the Arduino in case you need to see and to see the simple code used for a stepper motor:
    TB6600 Stepper Motor Driver with Arduino Tutorial (3 Examples)

  2. The following link is a tutorial of what moves the Arduino + 2 servos + mouse. This is done with SERVO motors, but I would like to do the same with STEPPER motors.

• I provided links because I don’t not want to copy and paste other people’s code here.
Now, keeping the processing software code the same. Asked someone to come up with the following Arduino code for the stepper motors.
Result: both steppers are instantly reacting to the mouse movement of x and y but are only making extremely small rotations and making humming noises.

//Arduino code:
// Include the AccelStepper library:
#include <AccelStepper.h>

// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin1 2
#define stepPin1 3
#define motorInterfaceType1 1

#define dirPin2 4
#define stepPin2 5
#define motorInterfaceType2 1

#define stepsPerRevolution 200
// Set the maximum speed and acceleration:

#define maxSpeed 900
#define acceleration 500

// Create a new instance of the AccelStepper class:
AccelStepper stepper1 = AccelStepper(motorInterfaceType1, stepPin1, dirPin1);
AccelStepper stepper2 = AccelStepper(motorInterfaceType2, stepPin2, dirPin2);

//set initial values for x and y
int ypos = 0;
int xpos = 0;

void setup() {



  Serial.begin(19200); // 19200 is the rate of communication
  Serial.println("Rolling"); // some output for debug purposes.


void loop() {
  static int v = 0; // value to be sent to the servo (0-180)
  if ( Serial.available()) {
    char ch =; // read in a character from the serial port and assign to ch
    switch (ch) { // switch based on the value of ch
      case '0'...'9': // if it's numeric
        v = v * 10 + ch - '0';
           so if the chars sent are 45x (turn x servo to 45 degs)..
           v is the value we want to send to the servo and it is currently 0
           The first char (ch) is 4 so
           0*10 = 0 + 4 - 0 = 4;
           Second char is 4;
           4*10 = 40 + 5 = 45 - 0 = 45;
           Third char is not a number(0-9) so we  drop through...
      case 'x': // if it's x
          ....and land here
          where we send the value of v which is now 45 to the x servo
          and then reset v to 0
        moveStepper(1, v);
        v = 0;
      case 'y':

        moveStepper(2, v);
        v = 0;

void moveStepper(int stepper, int step)
  if (stepper == 1)
    // Set the target position:
    // Run to target position with set speed and acceleration/deceleration:;
    // Set the target position:
    // Run to target position with set speed and acceleration/deceleration:;

You have to call EVERY time through loop().

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.