Stepper not stepping

I am trying to run stepper. It is not stepping in For loop. In step2, when I comment out "Mystepper.step(1); // Step the motor". Loop starts working and starts printing the steppings.

Please help me where am i doing mistake?

#include <Stepper.h>

const int StepsPerRev = 400;  // Steps per revolution
Stepper Mystepper(StepsPerRev, 8, 9, 10, 11);

int AC, DI, ang, arm_sp;
int stepCount = 0;
const int Min_Angle = 1;
const int Max_Angle = 4;
const int Gear_Ratio = 50;
int Min_Arm_Sp = 100;
int Max_Arm_Sp = 1000;
volatile boolean Step_Direction = LOW;  // Use volatile for variables modified by interrupts

const int ac = 5;  // Pin for AC
const int di = 2;  // Pin for DI

enum State {
  IDLE,
  Step1,
  Step2,
  Step4,
} state = IDLE;

void setup() {
  Serial.begin(9600);
  pinMode(ac, INPUT);
  pinMode(di, INPUT_PULLUP);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  attachInterrupt(digitalPinToInterrupt(di), Direction, FALLING);

  Serial.println("Setup complete. Waiting for input...");
}

void loop() {
  handleReceivedData();
}

void handleReceivedData() {
  Arm();
}

void dataReceive() {
  AC = digitalRead(ac);
  ang = analogRead(A1);
  arm_sp = analogRead(A0);
  DI = digitalRead(di);
}

void Direction() {
  Step_Direction = !Step_Direction;
  Serial.print("Direction changed to: ");
  Serial.println(Step_Direction ? "Forward" : "Backward");
}

void Arm() {
  dataReceive();
  stepCount = 0;
  int originalPosition;
  ang = map(ang, 0, 1023, Min_Angle, Max_Angle);
  arm_sp = map(arm_sp, 0, 1023, Max_Arm_Sp, Min_Arm_Sp);
  ang = ang * Gear_Ratio;

  // Print debug information
  Serial.print("Mapped Angle: "); Serial.println(ang);
  Serial.print("Mapped Speed: "); Serial.println(arm_sp);

  // Ensure arm_sp is within valid range
  if (arm_sp < Min_Arm_Sp || arm_sp > Max_Arm_Sp) {
    Serial.println("Error: arm_sp out of range");
    return;
  }

  switch (state) {
    case IDLE:
      if (AC == 1) {
        Mystepper.setSpeed(0);
        state = IDLE;
      }
      if (AC == 0) {
        stepCount = 0;
        state = Step1;
      }
      break;

    case Step1:
      Serial.println("Step 1");
      stepCount = 0;
      originalPosition = stepCount;
      Serial.print("Original Position: "); Serial.println(originalPosition);
      state = Step2;
      break;

    case Step2:
      Serial.println("Step 2");
      dataReceive();
      ang = map(ang, 0, 1023, Min_Angle, Max_Angle);
      arm_sp = map(arm_sp, 0, 1023, Max_Arm_Sp, Min_Arm_Sp);
      ang = ang * Gear_Ratio;

      // Debug output
      Serial.print("AC: "); Serial.println(AC);
      Serial.print("DI: "); Serial.println(DI);
      Serial.print("Ang: "); Serial.println(ang);
      Serial.print("Speed: "); Serial.println(arm_sp);

      while (AC == 0) {
        Serial.println("While loop Step 2");
        dataReceive();
        ang = map(ang, 0, 1023, Min_Angle, Max_Angle);
        arm_sp = map(arm_sp, 0, 1023, Max_Arm_Sp, Min_Arm_Sp);
        ang = ang * Gear_Ratio;

        int targetPosition1 = originalPosition + ang;
        Serial.print("Moving to Target Position1 in Step2: "); Serial.println(targetPosition1);

        for (int i = 0; i < targetPosition1; i++) {
          if (digitalRead(di) == LOW) {
            Direction();
          }
          Serial.print("DI: "); Serial.println(DI);
          Serial.print("Step_Direction: "); Serial.println(Step_Direction);
          Mystepper.setSpeed((Step_Direction ? 1 : -1) * arm_sp);
          Serial.print("Stepping "); Serial.println(Step_Direction ? "Forward" : "Backward");
          Mystepper.step(1);  // Step the motor
          Serial.print("Stepping: "); Serial.println(i);
        }
        Serial.println("Cycle 1 Complete");
        delay(200);

        dataReceive();
        ang = map(ang, 0, 1023, Min_Angle, Max_Angle);
        arm_sp = map(arm_sp, 0, 1023, Min_Arm_Sp, Max_Arm_Sp);
        ang = ang * Gear_Ratio;

        Serial.print("Value of AC: "); Serial.println(AC);
        Serial.print("Value of DI: "); Serial.println(DI);
        if (AC == 1) {
          Serial.print("Moving to Center Position in Step2: 0");
          for (int i = 0; i < targetPosition1; i++) {
            Mystepper.setSpeed(-arm_sp);
            Mystepper.step(-1);  // Step the motor in reverse
          }
          stepCount = 0;
          state = IDLE;
          Serial.println("Going to IDLE from Step2 - at 0 Position");
          break;
        }

        int targetPosition2 = originalPosition - ang;
        Serial.print("Moving to Target Position2 in Step2: "); Serial.println(targetPosition2);

        for (int x = targetPosition2; x < 0; x++) {
          if (digitalRead(di) == LOW) {
            Direction();
          }
          Mystepper.setSpeed((Step_Direction ? 1 : -1) * arm_sp);
          Serial.print("Stepping "); Serial.println(Step_Direction ? "Forward" : "Backward");
          Mystepper.step(-1);  // Step the motor in reverse
          Serial.print("Stepping: "); Serial.println(x);
        }
        Serial.println("Cycle 2 Complete");

        dataReceive();
        if (AC == 1) {
          state = Step4;
          Serial.println("Going to Step4 from Step2 - to reach 0 Position");
          break;
        }
        delay(200);
        state = Step1;
      }
      break;

    case Step4:
      int targetPosition3 = originalPosition;
      Serial.print("Moving to Center Position in Step4: 0");
      Mystepper.setSpeed(arm_sp);
      Mystepper.step(-originalPosition);  // Move to center position
      state = IDLE;
      Serial.println("Step 4 - Going to IDLE");
      break;
  }
}

How are ac and di wired? Show a wiring diagram.

Thanks. I found solution.

You should post your solution so that others might learn also.

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