Not able to vary speed of stepper motor to reach position

Hello,
I am using three stepper motor with suitable drivers. Attached code is working fine except I am not able to vary speed of motor with pot in case RUNZ and RUNZ1. I tried runToPosition() as well as runSpeed(). But no result. Please guide

void runFSM() {
  Data_recieve(a, n);
  AM = A[7];
  FR = A[6];
  ang = A[2];
  drive_sp = A[3];
  arm_sp = A[4];

  stepperZ.setMaxSpeed(40000);
  stepperZ.setAcceleration(40000);
  drive_sp = map(A[3], 0, 1023, 0, Auto_Speed);

  switch (state) {
    case IDLE:
      if (AM == 0) {
        state = RUNZ;
      }
      break;

    case RUNZ:
      if (AM == 0) {
        stepperZ.setCurrentPosition(0);
        Serial.println("2");
        arm_sp = map(A[4], 0, 1023, 0, 40000);
        digitalWrite(Relay_Air, HIGH);
        stepperZ.moveTo(18000);
        stepperZ.setSpeed(arm_sp);
        // Set the speed and move to the target position
        stepperZ.runToPosition();
        state = RUNX;
      }
      break;


    case RUNX:
      {
        unsigned long distance = map(ang, 0, 1023, 0, 827);
        stepperL.setMaxSpeed(5000);
        stepperL.setAcceleration(100000);
        stepperR.setMaxSpeed(5000);
        stepperR.setAcceleration(100000);
        unsigned long steps = ((60 * distance * 800UL) / (3.1415 * 310UL));
        Serial.println(steps);

        if (FR == 0) {
          DirectionX(); // Assuming DirectionX() is defined elsewhere
        }

        for (unsigned long i = 0; i <= steps; i++) {
          stepperL.setSpeed(Speed_Factor * drive_sp);
          stepperR.setSpeed(Speed_Factor * drive_sp);
          stepperL.moveTo(Step_DirectionX);
          stepperR.moveTo(Step_DirectionX);
          stepperL.run();
          stepperR.run();
        }
        stepperL.setCurrentPosition(0);
        stepperR.setCurrentPosition(0);
        state = RUNZ1;
      }
      break;

    case RUNZ1:
      if (AM == 0) {
        Serial.println("2");
        arm_sp = map(A[4], 0, 1023, 0, 40000);
        digitalWrite(Relay_Air, HIGH);
        stepperZ.moveTo(0);
        stepperZ.setSpeed(arm_sp);
        stepperZ.runToPosition();
        state = RUNX1;
      }
      break;

    case RUNX1:
      {
        unsigned long distance = map(ang, 0, 1023, 0, 827);
        stepperL.setMaxSpeed(5000);
        stepperL.setAcceleration(100000);
        stepperR.setMaxSpeed(5000);
        stepperR.setAcceleration(100000);
        unsigned long steps = ((60 * distance * 800UL) / (3.1415 * 310UL));
        Serial.println(steps);

        if (FR == 0) {
          DirectionX(); // Assuming DirectionX() is defined elsewhere
        }

        for (unsigned long i = 0; i <= steps; i++) {
          stepperL.setSpeed(Speed_Factor * drive_sp);
          stepperR.setSpeed(Speed_Factor * drive_sp);
          stepperL.moveTo(Step_DirectionX);
          stepperR.moveTo(Step_DirectionX);
          stepperL.run();
          stepperR.run();
        }
        stepperL.setCurrentPosition(0);
        stepperR.setCurrentPosition(0);

        state = IDLE;
      }
      break;
  }
}

void DirectionX() {
  Step_DirectionX = !Step_DirectionX;
}

That is not a complete sketch. What stepper library are you using? AccelStepper?

After setting your speed and position, you need to call .run() or .runSpeed() as often as possible until you get to your final position. It you don't care about doing anything else, you can use .runNewPosition() which is a blocking call.

You are correct I am using accel library. I tried as per below code also but no luck

case RUNZ:
  if (AM == 0) {
    stepperZ.setCurrentPosition(0);
    Serial.println("2");
    arm_sp = map(A[4], 0, 1023, 0, 40000);
    digitalWrite(Relay_Air, HIGH);
    stepperZ.moveTo(18000);
    stepperZ.setSpeed(arm_sp);

    while (stepperZ.distanceToGo() != 18000) {
      stepperZ.runSpeed();
    }
    state = RUNX;
  }
  break;

You should be counting down to 0

    while (stepperZ.distanceToGo() != 0) {
      stepperZ.runSpeed();

Which is blocking, so you can just replace that with

stepperZ.runToNewPosition(18000);

It may help you to study the examples that come with the library.

What processor/board are you using? A steprate of 40000 is too high for many boards.

Arduino Mega

Then 40000 is far too high. Try with 4000.

It is running at constant speed with below code. I want to vary speed through pot.

    case RUNZ:
      if (AM == 0) {
        stepperZ.setCurrentPosition(0);
        Serial.println("2");
        arm_sp = map(A[4], 0, 1023, 0, 4000);  // Adjust the upper limit as needed
        digitalWrite(Relay_Air, HIGH);

          stepperZ.setSpeed(arm_sp);
          stepperZ.moveTo(5900);
          stepperZ.runToPosition();
          state = RUNX;
      }
      break;

Unfortunately you still don't show the complete sketch. So we cannot see where and how A[4] is set.

I solved it.

I am feeling MobaTools is better library then Accel in controlling multiple steppers with speed control.

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