AccelStepper - Move and Position

I cannot unerstand the relationship between the argument in the move() command and the value returned by currentPosition(). For example move(3000) moves only a tiny fraction of one revolution, and then currentPostion() changes by only one unit. move(30000) gives a similar result. Using any negative vaue for move() makes the currentPosition() value reduce by one unit.

#include <AccelStepper.h>
// Bluetooth connection to HC-05
#include <SoftwareSerial.h> // use the software uart
#include <Wire.h>
SoftwareSerial bluetooth(A5,A4); // RX, TX


#define MOTOR_LIFT_ENABLE_PIN 8
#define MOTOR_LIFT_STEP_PIN 2
#define MOTOR_LIFT_DIR_PIN 5


#define MOTOR_STEER_ENABLE_PIN 8
#define MOTOR_STEER_STEP_PIN 3
#define MOTOR_STEER_DIR_PIN 6


#define MOTOR_TILT_ENABLE_PIN 8
#define MOTOR_TILT_STEP_PIN 4
#define MOTOR_TILT_DIR_PIN 7

#define MOTOR_MOVE_ENABLE_PIN 8
#define MOTOR_MOVE_STEP_PIN 12
#define MOTOR_MOVE_DIR_PIN 13

AccelStepper motorLift(1, MOTOR_LIFT_STEP_PIN, MOTOR_LIFT_DIR_PIN);
AccelStepper motorSteer(1, MOTOR_STEER_STEP_PIN, MOTOR_STEER_DIR_PIN);
AccelStepper motorTilt(1, MOTOR_TILT_STEP_PIN, MOTOR_TILT_DIR_PIN);
AccelStepper motorMove(1, MOTOR_MOVE_STEP_PIN, MOTOR_MOVE_DIR_PIN);

void setup()
{
  Serial.begin(9600);Serial.println("Arduino ready");
  // first set up the BT connection
  bluetooth.begin(9600); // start the bluetooth uart at 9600 which is its default
  delay(200); // wait for voltage stabilize
  bluetooth.print("AT+NAMEquilkin.com"); 
  delay(3000); // wait for settings to take effect. 

   pinMode(MOTOR_LIFT_ENABLE_PIN, OUTPUT);
   pinMode(MOTOR_STEER_ENABLE_PIN, OUTPUT);
   pinMode(MOTOR_TILT_ENABLE_PIN, OUTPUT);
   pinMode(MOTOR_MOVE_ENABLE_PIN, OUTPUT);

   motorLift.setEnablePin(MOTOR_LIFT_ENABLE_PIN);
   motorLift.setPinsInverted(false, false, true);
   motorLift.setAcceleration(100);
   //motorLift.setMaxSpeed(100);
   //motorLift.setSpeed(100);
   motorLift.enableOutputs();

   motorSteer.setEnablePin(MOTOR_STEER_ENABLE_PIN);
   motorSteer.setPinsInverted(false, false, true);
   motorSteer.setAcceleration(100);
   //motorSteer.setMaxSpeed(100);
   //motorSteer.setSpeed(100);
   motorSteer.enableOutputs();

   motorTilt.setEnablePin(MOTOR_TILT_ENABLE_PIN);
   motorTilt.setPinsInverted(false, false, true);
   motorTilt.setAcceleration(100);
   //motorTilt.setMaxSpeed(100);
   //motorTilt.setSpeed(100);
   motorTilt.enableOutputs();

   motorMove.setEnablePin(MOTOR_TILT_ENABLE_PIN);
   motorMove.setPinsInverted(false, false, true);
   motorMove.setAcceleration(100);
   //motorMove.setMaxSpeed(100);
   //motorMove.setSpeed(100);
   motorMove.enableOutputs();
}

void loop()
{
  if (bluetooth.available()) { // check if anything in UART buffer

    char action = bluetooth.read(); // action value is text character
    digitalWrite(13,!digitalRead(13)); // toggle the onboard LED
    
    switch (action) {
      case '1': 
        motorMove.enableOutputs();
         motorMove.move(32000);
        motorMove.run();
        break;
    
      case '2':
         motorMove.move(-32000);
        motorMove.run();
        break;

      case '3': 
         motorSteer.move(3000);
        motorSteer.run();
        break;
    
      case '4':
         motorSteer.move(-3000);
        motorSteer.run();
        break;

        case '5':
        motorMove.disableOutputs();
        break;

    }
    Serial.print("positions: ");
    Serial.print(motorMove.currentPosition());
    Serial.print(" and ");
    Serial.println(motorSteer.currentPosition());
  }
}

I'm using A4988 drivers on a CNC shield.

run() move one step towards the position set with move()
run() should be in the loop and you could check distanceToGo() to see if you have acheived the new position..
AccelStepper/classAccelStepper

good luck.. ~q

OK, thanks. So the code below (from a supplier of the Arduino hardware, and on which I based by own code) makes little sense then?

void loop()
{
   motorX.move(3000);
   motorX.run();
   motorY.move(3000);
   motorY.run();
   motorZ.move(3000);
   motorZ.run();
   motorA.move(3000);
   motorA.run();
}

I've now changed to runToPosition() and it makes more sense. Had to change the max speed as well, otherwise far too slow!

You're welcome..

and no, not so great demo code there..
didn't recommend runToPosistion() as it blocks execution until destination reached..
can't move and steer at the same time..

was thinking something like this maybe..

void loop()
{
  static bool printMove = false;
  static bool printSteer = false;
  static bool stop = true;

  if (bluetooth.available()) { // check if anything in UART buffer

    char action = bluetooth.read(); // action value is text character
    digitalWrite(13, !digitalRead(13)); // toggle the onboard LED

    switch (action) {
      case '1':
        motorMove.enableOutputs();
        motorMove.move(32000);
        stop = false;
        printMove = true;
        break;

      case '2':
        motorMove.move(-32000);
        stop = false;
        printMove = true;
        break;

      case '3':
        motorSteer.move(3000);
        printSteer = true;
        break;

      case '4':
        motorSteer.move(-3000);
        printSteer = true;
        break;

      case '5':
        motorMove.disableOutputs();
        stop = true;
        printMove = true;
        break;

    }
  }

  //step motors print once when dest reached..
  if (!stop) {
    if (!motorMove.run() && printMove) {
      Serial.print("Move completed : "); Serial.println(motorMove.currentPosition());
      printMove = false;
    }
  } else if (printMove) {
    Serial.print("Move Stopped : "); Serial.println(motorMove.currentPosition());
    printMove = false;
  }
  //can steer while stopped??
  if (!motorSteer.run() && printSteer) {
    Serial.print("Steer completed : "); Serial.println(motorSteer.currentPosition());
    printSteer = false;
  }
}

untested sorry..

good luck.. ~q

Perfect thank you. Now tested - no changes required :grinning:

1 Like