One condition is not working

Please help. Below is code to oscillate stepper around centre position. Entire code is working fine except one condition. Given below

void Arm() {
  Data_recieve(a, n);
  unsigned long now = millis();
  if (now - lastRecvTime > TIMEOUT_DURATION) {
    Serial.println("Signal lost");
    resetData();  // Signal lost, Reset
  }
  AC = A[5];
  ang = A[2];
  arm_sp = A[4];
  stepper.setCurrentPosition(0);
  int originalPosition;
  ang = map(ang, 0, 1023, Min_Angle, Max_Angle);
  arm_sp = map(arm_sp, 0, 1023, Min_Arm_Sp, Max_Arm_Sp);
  stepper.setMaxSpeed(arm_sp);
  stepper.setSpeed(arm_sp);
  stepper.setAcceleration(20000);
  ang = ang * 30;

  switch (state) {
    case IDLE:
      if (AC == 1) {
        stepper.stop();
        state = IDLE;
      }

      if (AC == 0) {
        digitalWrite(Relay_Blast, HIGH);
        stepper.setCurrentPosition(0);
        state = Step1;
      }
      break;

    case Step1:
      {
        originalPosition = stepper.currentPosition();  // Store original position when entering Step1
        // Serial.println("Original Position: " + String(originalPosition));
        state = Step3;
      }
      break;

    case Step3:
      {
        int targetPosition1 = originalPosition + ang;
        int targetPosition2 = originalPosition - ang;

        while (AC == 0) {
          Serial.println("Moving to Target Position1 in Step3: " + String(targetPosition1));
          stepper.moveTo(originalPosition + ang);
          stepper.runToPosition();
          delay(100);

          Data_recieve(a, n);
          AC = A[5];
          ang = A[2];
          arm_sp = A[4];
          ang = map(ang, 0, 1023, Min_Angle, Max_Angle);
          arm_sp = map(arm_sp, 0, 1023, Min_Arm_Sp, Max_Arm_Sp);
          stepper.setMaxSpeed(arm_sp);
          stepper.setSpeed(arm_sp);
          stepper.setAcceleration(20000);
          ang = ang * 30;


          Serial.println("Moving to Target Position 0 in Step3: " + String(originalPosition));
          stepper.moveTo(0);
          stepper.runToPosition();
          // delay(100);

          Data_recieve(a, n);
          AC = A[5];
          ang = A[2];
          arm_sp = A[4];
          ang = map(ang, 0, 1023, Min_Angle, Max_Angle);
          arm_sp = map(arm_sp, 0, 1023, Min_Arm_Sp, Max_Arm_Sp);
          stepper.setSpeed(arm_sp);
          ang = ang * 30;

          // Check if we need to move to Step4
          if (AC == 1) {
            state = IDLE;
            Serial.println("Skipping Step4 - Going IDLE");
            break;
          }

          Serial.println("Moving to Target Position2 in Step3: " + String(targetPosition2));
          stepper.moveTo(originalPosition - ang);
          stepper.runToPosition();

          Data_recieve(a, n);
          delay(100);
          AC = A[5];
          Serial.println(AC);

          // Check if we need to move to Step4
          if (AC == 1) {
            Serial.println("Moving To Step4 --- 2");
            state = Step4;
            break;
          }
        }
        state = Step1;  // Move back to Step1 after the loop
      }
      break;


    case Step4:
      {
        // Move to center (zero) position
        Serial.println("Moving to Center Position in Step4: 0");
        stepper.moveTo(0);
        stepper.runToPosition();
        // while (stepper.distanceToGo() != 0) {
        //   stepper.run();
        //   delay(10);  // Add a small delay to avoid high CPU usage
        // }

        // // Stop the stepper
        // stepper.stop();

        // Set state back to IDLE
        state = IDLE;
        Serial.println("Step 4 - Going to IDLE");
      }
      break;
  }
}

except this portion

It is very difficult to say. The code looks dependent on dataReceive() and A[...], and there is no way to see those or if it even compiles.

Did you see:

This other portion of code above that prevents it from executing.

Will this log help you?

10:43:56.137 -> Value of AC: 0

10:43:56.137 -> Moving to Target Position1 in Step2: 2130

10:44:00.522 -> Moving to Center Position in Step2: 0

10:44:02.694 -> Moving to Target Position2 in Step3: -2130

10:44:04.974 -> Value of AC: 0

10:44:04.974 -> Moving to Target Position1 in Step2: 2130

10:44:09.347 -> Moving to Center Position in Step2: 0

10:44:11.547 -> Moving to Target Position2 in Step3: -2130

10:44:13.834 -> Value of AC: 0

10:44:13.834 -> Moving to Target Position1 in Step2: 2130

10:44:18.240 -> Moving to Center Position in Step2: 0

10:44:20.409 -> Moving to Target Position2 in Step3: -2130

10:44:22.679 -> Value of AC: 1

10:44:22.679 -> Going to Step4 from Step2 - to reach 0 Position

10:44:22.751 -> Moving to Center Position in Step4: 0

10:44:22.788 -> Step 4 - Going to IDLE

Commented this condition and executed. It gave same result are earlier

10:49:13.299 -> Moving to Target Position1 in Step2: 2130

10:49:15.562 -> Moving to Center Position in Step2: 0

10:49:17.735 -> Moving to Target Position2 in Step3: -2130

10:49:20.023 -> Value of AC: 0

10:49:20.023 -> Moving to Target Position1 in Step2: 2130

10:49:24.424 -> Moving to Center Position in Step2: 0

10:49:26.575 -> Moving to Target Position2 in Step3: -2130

10:49:28.869 -> Value of AC: 1

10:49:28.869 -> Going to Step4 from Step2 - to reach 0 Position

10:49:28.940 -> Moving to Center Position in Step4: 0

10:49:28.978 -> Step 4 - Going to IDLE

It should take ~2.201 seconds to reach to 0 in Step4

Does the stepper move in the background?
Or does it stop executing your code until the new position is reached?
In the first case your stepper will be stopped before the target is reached... maybe there is a way to ask the stepper if it has reached the target position?

Also: do not use magical numbers... A[] does not tell anything about what is supposed to be in there....

Yes, It stops executing code and skips this portion

stepper.moveTo(0);
// stepper.runToPosition();
// Set state back to IDLE
while (stepper.distanceToGo() != 0) {
stepper.run();
// delay(10); // Add a small delay to avoid high CPU usage
}

amended code as below

stepper.moveTo(0);
// stepper.runToPosition();
// Set state back to IDLE
while (stepper.distanceToGo() != 0) {
stepper.run();
// delay(10); // Add a small delay to avoid high CPU usage
}

but it still skips this and goes to
state = IDLE;
Serial.println("Step 4 - Going to IDLE");

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