Stepper not traveling to desired steps

Here is logic to move my stepper and my problem. (in line 301 to 309)

// setDirectionAnticlockwise();
** digitalWrite(Outgoing_Arm_Cycle1, HIGH);**
** setDirectionClockwise(); // Now change direction to clockwise**
** int targetPosition2 = originalPosition - ang; // Define new target position**

** Serial.println("Moving to Target Position2 in Step2: " + String(targetPosition2));**
** moveToPosition2(targetPosition2, arm_sp, DI);**

Here I am moving stepper in clockwise direction but it does not go to targetPosition2 and returns from originalPosition

Process 1

  1. AC == 0; DI == 1,
  2. Program moves to step1
  3. Moves to step2
  4. The program reads data and defines targetPosition1 which is in anticlockwise direction from center position.
  5. The stepper starts moving from center position to defined targetPosition1.
  6. Before reaching targetPosition1, AC is changed to AC==1.
  7. In this condition, the stepper shall travel to targetPosition1 then change direction and shall return to center position and stop.

Process 2

  1. AC == 0; DI == 1,
  2. Program moves to step1
  3. Moves to step2
  4. The program reads data and defines targetPosition1.
  5. The stepper starts moving from center position to defined targetPosition1.
  6. AC doesn’t change till the reaches to targetPosition1.
  7. In this condition, new targetPosition2 is to be defined, which will be on clockwise direction from center position.
  8. The stepper starts moving from targetPosition1 to targetPosition2, crosses center position towards targetPosition2 which is in clockwise direction from center position.
  9. Before reaching targetPosition2, AC is changed to AC==1.
  10. In this condition, the stepper shall travel to targetPosition2 then change direction and shall return to center position and stop.

Process 3

  1. AC == 0; DI == 1,
  2. Program moves to step1
  3. Moves to step2
  4. The program reads data and defines targetPosition1.
  5. The stepper starts moving from center position to defined targetPosition1.
  6. AC doesn’t change till the reaches to targetPosition1.
  7. In this condition, new targetPosition2 is to be defined, which will be on clockwise direction from center position.
  8. The stepper starts moving from targetPosition1 to targetPosition2, crosses center position towards targetPosition2 which is in clockwise direction from center position.
  9. Till reaching targetPosition2, AC is not changed.
  10. In this condition, the stepper shall travel to targetPosition2 then change direction and shall return to targetPosition1 and shall continue to do so until any of above condition are met.

Process 4

  1. In any of the above three processes, if any time DI is changed to DI == 0, the stepper shall reverse its direction and shall move by 100 steps in opposite direction.
  2. After completion of 100 steps, it should be checked again if DI == 0, this step should be repeated.
#include <AccelStepper.h>
#include <nRF24L01.h>
#include <RF24.h>

#define BAUDRATE 115200

// Define constants
const byte Surf1_address[6] = {'1', 'S', '0', '9', '1'};
const int stepPin = 7;  // Step pulse pin
const int dirPin = 6;   // Direction pin
const uint8_t Outgoing_Arm_Cycle1 = 2;  // Pin for arm cycle completion indication
const int TIMEOUT_DURATION = 1000;  // Timeout duration in milliseconds
const int Min_Angle = 17;  // Minimum angle in steps
const int Max_Angle = 67;  // Maximum angle in steps
const int Gear_Ratio = 50;  // Gear ratio for the stepper motor
const int Paint_Min_Angle = 0;
const int StepsperRev = 400;  // Steps per revolution
const long Paint_Max_Angle = (StepsperRev / 2) * Gear_Ratio;  // Maximum angle in steps for painting
const int Min_Arm_Sp = 100;  // Minimum arm speed
const int Max_Arm_Sp = 800;  // Maximum arm speed
int Delay = 000;

// Create RF24 and AccelStepper objects
RF24 radio(9, 10);  // CE, CSN
AccelStepper stepper(1, stepPin, dirPin);  // 7 pulse and 6 direction

// Declare global variables
int Incoming_Array[16];
int n = 15;
unsigned long lastRecvTime = 0;  // Timestamp for the last received data
volatile boolean Step_Direction = HIGH;  // Use volatile for variables modified by interrupts
long initial_homing = -1;  // Variable for homing stepper at startup

// State machine states
enum State {
  IDLE,
  Step1,
  Step2,
  Step3,
  Step4,
  Step5,
  IDLE1,
  P_Step1,
  P_Step2,
  P_Step3
} state = IDLE;

// Function declarations
void resetData();
void Radio();
void initStepper();
void handleReceivedData();
void homing();
void Data_recieve(int x[], int n);
void timeout();
void p_array();
void Direction();
void stepMotor(int steps, int speed);
void Arm();
void Paint_Arm();
void moveToCenterPosition(int targetPosition, int arm_sp);
void setDirectionAnticlockwise();
void setDirectionClockwise();

void setup() {
//    Serial.begin(9600);
  resetData();
  Radio();
  initStepper();
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(Outgoing_Arm_Cycle1, OUTPUT);
  Serial.println("Setup complete. Waiting for input...");
}

void loop() {
  handleReceivedData();
  timeout();
  // p_array();
}

void handleReceivedData() {
  Data_recieve(Incoming_Array, n);
  int AC = Incoming_Array[5];
  int PA = Incoming_Array[14];

  if (PA == 1 && AC == 1) {
    homing();
  } else if (PA == 1) {
    Arm();
  } else if (PA == 0) {
    Paint_Arm();
    Serial.println("In Paint Arm");
  }
}

void homing() {
  Serial.println("Homing");
  stepper.setMaxSpeed(3000);
  stepper.setSpeed(2000);
  stepper.setAcceleration(10000);  // Set Acceleration

  if (Incoming_Array[5] == 1 && Incoming_Array[9] == 0) {
    Serial.println("Left");
    stepper.moveTo(+initial_homing);  // Set the position to move to
    initial_homing++;
    delay(0);
    stepper.run();
  }
  stepper.setCurrentPosition(0);

  if (Incoming_Array[5] == 1 && Incoming_Array[10] == 0) {
    Serial.println("Right");
    stepper.setCurrentPosition(0);
    stepper.moveTo(-initial_homing);  // Set the position to move to
    initial_homing++;
    delay(0);
    stepper.run();
  }
  stepper.setCurrentPosition(0);
}

void Data_recieve(int x[], int n) {
  while (radio.available()) {
    radio.read(Incoming_Array, sizeof(Incoming_Array));
    lastRecvTime = millis();  // Here we receive the data
  }
  // Serial.println("Received");
}

void timeout() {
  unsigned long now = millis();
  if (now - lastRecvTime > TIMEOUT_DURATION) {
    Serial.println("Signal lost");
    resetData();  // Signal lost, Reset
  }
}

void resetData() {
  for (int i = 0; i < 16; i++) {
    Incoming_Array[i] = (i < 2) ? 512 : 1;
  }
  Incoming_Array[2] = 0;
  Incoming_Array[3] = 0;
  Incoming_Array[4] = 0;
  Incoming_Array[5] = 1;
}

void p_array() {
  Serial.print("Incoming Values : ");
  for (int i = 0; i < n; i++) {
    Serial.print(" | ");
    Serial.print(Incoming_Array[i]);
  }
  Serial.print(" | ");
  Serial.println();
}

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

void stepMotor(int steps, int speed) {
  for (int i = 0; i < steps; i++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(100000 / speed);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(100000 / speed);
  }
}

void Arm() {
  Data_recieve(Incoming_Array, n);
  timeout();
  int ang = Incoming_Array[2];
  int arm_sp = Incoming_Array[4];
  int AC = Incoming_Array[5];
  int DI = Incoming_Array[13];
  int PA = Incoming_Array[14];
  int stepCount = 0;
  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;

  // 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;
  }

  stepCount = 0;
  int originalPosition = stepCount;

  switch (state) {
    case IDLE:
      if (PA == 1 && AC == 1) {
        digitalWrite(stepPin, LOW);
        state = IDLE;
      } else if (AC == 0) {
        stepCount = 0;
        state = Step1;
      } else if (PA == 0) {
        state = IDLE1;
        return;
      }
      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");
      executeStep2(originalPosition, stepCount);
      break;

    case Step4:
      moveToCenterPosition(originalPosition, arm_sp);
      state = IDLE;
      break;

    case Step3:
      Data_recieve(Incoming_Array, n);
      timeout();
      if (PA == 0) {
        state = IDLE1;
      }
      break;

    case Step5:
      Data_recieve(Incoming_Array, n);
      timeout();
      if (PA == 0) {
        state = IDLE1;
      } else {
        executeStep5(originalPosition, stepCount);
      }
      break;
  }
}

void executeStep2(int &originalPosition, int &stepCount) {
  Data_recieve(Incoming_Array, n);
  timeout();
  int ang = Incoming_Array[2];
  int arm_sp = Incoming_Array[4];
  int AC = Incoming_Array[5];
  int DI = Incoming_Array[13];

  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;

  while (AC == 0) {
    Serial.println("While loop Step 2");
    digitalWrite(Outgoing_Arm_Cycle1, HIGH);
    Data_recieve(Incoming_Array, n);
    timeout();
    ang = Incoming_Array[2];
    arm_sp = Incoming_Array[4];
    AC = Incoming_Array[5];
    DI = Incoming_Array[13];

    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;

    setDirectionAnticlockwise();  // Ensure initial direction is anticlockwise
    int targetPosition1 = originalPosition + ang;  // Define first target position
    Serial.println("Moving to Target Position1 in Step2: " + String(targetPosition1));
    moveToPosition1(targetPosition1, arm_sp, DI);
    delay(500);
    Data_recieve(Incoming_Array, n);
    timeout();
    ang = Incoming_Array[2];
    arm_sp = Incoming_Array[4];
    AC = Incoming_Array[5];
    DI = Incoming_Array[13];
    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;
    delay(Delay);

    if (AC == 1) {
      Serial.println("Moving to Center Position in Step2: 0");
      delay(Delay);
      moveToCenterPosition(targetPosition1, arm_sp);
      stepCount = 0;
      digitalWrite(Outgoing_Arm_Cycle1, HIGH);
      state = IDLE;
      Serial.println("Going to IDLE from Step2 - at 0 Position");
      break;
    }
    //    setDirectionAnticlockwise();
    digitalWrite(Outgoing_Arm_Cycle1, HIGH);
    setDirectionClockwise();  // Now change direction to clockwise
    int targetPosition2 = originalPosition - ang; // Define new target position

    Serial.println("Moving to Target Position2 in Step2: " + String(targetPosition2));
    moveToPosition2(targetPosition2, arm_sp, DI);
    delay(1000);
    Serial.println("Cycle 2 Complete");

    Data_recieve(Incoming_Array, n);
    timeout();
    ang = Incoming_Array[2];
    arm_sp = Incoming_Array[4];
    AC = Incoming_Array[5];
    DI = Incoming_Array[13];
    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;

    if (AC == 1) {
      Serial.println("Stepper Position: " + String(stepper.currentPosition()));
      state = Step4;
      Serial.println("Going to Step4 from Step2 - to reach 0 Position");
      break;
    }
    delay(200);
    state = Step1;
  }
}

void executeStep5(int &originalPosition, int &stepCount) {
  Data_recieve(Incoming_Array, n);
  timeout();
  int ang = Incoming_Array[2];
  int arm_sp = Incoming_Array[4];
  int AC = Incoming_Array[5];
  int DI = Incoming_Array[13];

  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;

  while (true) {
    setDirectionClockwise();  // Ensure initial direction is clockwise
    int targetPosition2 = originalPosition - ang;  // Define new target position
    Serial.println("Moving to Target Position2 in Step5: " + String(targetPosition2));
    moveToPosition1(targetPosition2, arm_sp, DI);

    if (AC == 1) {
      Serial.println("Stepper Position: " + String(stepper.currentPosition()));
      moveToCenterPosition(targetPosition2, arm_sp);
      state = IDLE;
      Serial.println("Going to IDLE from Step5 - at 0 Position");
      break;
    }

    Data_recieve(Incoming_Array, n);
    timeout();
    ang = Incoming_Array[2];
    arm_sp = Incoming_Array[4];
    AC = Incoming_Array[5];
    DI = Incoming_Array[13];
    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;

    setDirectionAnticlockwise();  // Now change direction to anticlockwise
    int targetPosition1 = originalPosition + ang;  // Define first target position
    Serial.println("Moving to Target Position1 in Step5: " + String(targetPosition1));
    moveToPosition1(targetPosition1, arm_sp, DI);

    if (AC == 1) {
      Serial.println("Stepper Position: " + String(stepper.currentPosition()));
      moveToCenterPosition(targetPosition1, arm_sp);
      state = IDLE;
      Serial.println("Going to IDLE from Step5 - at 0 Position");
      break;
    }
  }
}

void moveToPosition2(int targetPosition2, int arm_sp, int DI) {
  Serial.println(targetPosition2);
  for (int x = targetPosition2; x < 0; x++) {
    if (DI == 0) {
      Direction();
      Serial.println("MoveToPosition Loop 2 in DI");
    }
    stepMotor(1, arm_sp);
    Data_recieve(Incoming_Array, n);
    DI = Incoming_Array[13];
    Serial.println("MoveToPosition Loop 2");
    while (DI == 0) {
      stepMotor(1, arm_sp);  // Move in the opposite direction
      delay(10);  // Small delay to allow for smooth motion
      Data_recieve(Incoming_Array, n);
      DI = Incoming_Array[13];
    }
  }
}

void moveToPosition1(int targetPosition1, int arm_sp, int DI) {
  for (int i = 0; i < targetPosition1; i++) {
    if (DI == 0) {
      Direction();
      Serial.println("MoveToPosition Loop 1 in DI");
    }
    stepMotor(1, arm_sp);
    Data_recieve(Incoming_Array, n);
    DI = Incoming_Array[13];
    Serial.println("MoveToPosition Loop 1");
    while (DI == 0) {
      stepMotor(1, arm_sp);  // Move in the opposite direction
      delay(10);  // Small delay to allow for smooth motion
      Data_recieve(Incoming_Array, n);
      DI = Incoming_Array[13];
    }
  }
}

void moveToCenterPosition(int targetPosition, int arm_sp) {
  Serial.println("Moving to Center Position: 0");
  setDirectionClockwise();
  for (int i = targetPosition; i > 0; i--) {
    stepMotor(1, arm_sp);  // Step the motor in reverse
  }
  setDirectionAnticlockwise();
  digitalWrite(Outgoing_Arm_Cycle1, HIGH);
}

void Paint_Arm() {
  // to be added }

void initStepper() {
  stepper.setMaxSpeed(5000);        // SPEED = Steps / second
  stepper.setAcceleration(200000);  // ACCELERATION = Steps / (second)^2
}

void Radio() {
  radio.begin();
  radio.openReadingPipe(0, Surf1_address);
  radio.setAutoAck(false);
  radio.setDataRate(RF24_250KBPS);
  radio.setPALevel(RF24_PA_LOW);
  radio.startListening();
}

void setDirectionAnticlockwise() {
  digitalWrite(dirPin, HIGH);
}

void setDirectionClockwise() {
  digitalWrite(dirPin, LOW);
}

First off, the code provided does not even compile. You are missing a closing brace - probably for your Paint_Arm() function.

Second, there is a lot going on with your code. I would suggest refactoring it so that it all is more of a state machine. For example, you don't need to constantly set your speed/maxspeed/acceleration every time you call home(). Those only need to be called when you begin homing. That is the power of a state machine. You put some code in the beginState() function for things that need to be done once vs. the code you put in the checkState() function which is something that needs to be done repeatedly [like stepper.run()]

I would also change the Data_receive() function so it sets a flag or something when new data is actually received vs. processing your Incoming_Array[] every time in all the other functions. This will also help you determine if there is new data which will cause you to transition to a new state vs. continue on in the current state. This should be done once in loop(), not scattered around repeatedly throughout your code.

loop() {
  // check for new data
  if ( new data ) {
    handle_data(); // and potentially transition to new state
  }
  checkState();  // do whatever needs to be done in current state
}

Yes, you are right.

Surely, will try to correct.

Please find compiled code

  #include <AccelStepper.h>
#include <nRF24L01.h>
#include <RF24.h>

#define BAUDRATE 115200

// Define constants
const byte Surf1_address[6] = {'1', 'S', '0', '9', '1'};
const int stepPin = 7;  // Step pulse pin
const int dirPin = 6;   // Direction pin
const uint8_t Outgoing_Arm_Cycle1 = 2;  // Pin for arm cycle completion indication
const int TIMEOUT_DURATION = 1000;  // Timeout duration in milliseconds
const int Min_Angle = 17;  // Minimum angle in steps
const int Max_Angle = 67;  // Maximum angle in steps
const int Gear_Ratio = 50;  // Gear ratio for the stepper motor
const int Paint_Min_Angle = 0;
const int StepsperRev = 400;  // Steps per revolution
const long Paint_Max_Angle = (StepsperRev / 2) * Gear_Ratio;  // Maximum angle in steps for painting
const int Min_Arm_Sp = 100;  // Minimum arm speed
const int Max_Arm_Sp = 800;  // Maximum arm speed
int Delay = 000;

// Create RF24 and AccelStepper objects
RF24 radio(9, 10);  // CE, CSN
AccelStepper stepper(1, stepPin, dirPin);  // 7 pulse and 6 direction

// Declare global variables
int Incoming_Array[16];
int n = 15;
unsigned long lastRecvTime = 0;  // Timestamp for the last received data
volatile boolean Step_Direction = HIGH;  // Use volatile for variables modified by interrupts
long initial_homing = -1;  // Variable for homing stepper at startup

// State machine states
enum State {
  IDLE,
  Step1,
  Step2,
  Step3,
  Step4,
  Step5,
  IDLE1,
  P_Step1,
  P_Step2,
  P_Step3
} state = IDLE;

// Function declarations
void resetData();
void Radio();
void initStepper();
void handleReceivedData();
void homing();
void Data_recieve(int x[], int n);
void timeout();
void p_array();
void Direction();
void stepMotor(int steps, int speed);
void Arm();
void Paint_Arm();
void moveToCenterPosition(int targetPosition, int arm_sp);
void setDirectionAnticlockwise();
void setDirectionClockwise();

void setup() {
  //    Serial.begin(9600);
  resetData();
  Radio();
  initStepper();
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(Outgoing_Arm_Cycle1, OUTPUT);
  Serial.println("Setup complete. Waiting for input...");
}

void loop() {
  handleReceivedData();
  timeout();
  // p_array();
}

void handleReceivedData() {
  Data_recieve(Incoming_Array, n);
  int AC = Incoming_Array[5];
  int PA = Incoming_Array[14];

  if (PA == 1 && AC == 1) {
    homing();
  } else if (PA == 1) {
    Arm();
  } else if (PA == 0) {
    Paint_Arm();
    Serial.println("In Paint Arm");
  }
}

void homing() {
  Serial.println("Homing");
  if (Incoming_Array[5] == 1 && Incoming_Array[9] == 0) {
    Serial.println("Left");
    stepper.moveTo(+initial_homing);  // Set the position to move to
    initial_homing++;
    delay(0);
    stepper.run();
  }
  stepper.setCurrentPosition(0);

  if (Incoming_Array[5] == 1 && Incoming_Array[10] == 0) {
    Serial.println("Right");
    stepper.setCurrentPosition(0);
    stepper.moveTo(-initial_homing);  // Set the position to move to
    initial_homing++;
    delay(0);
    stepper.run();
  }
  stepper.setCurrentPosition(0);
}

void Data_recieve(int x[], int n) {
  while (radio.available()) {
    radio.read(Incoming_Array, sizeof(Incoming_Array));
    lastRecvTime = millis();  // Here we receive the data
  }
  // Serial.println("Received");
}

void timeout() {
  unsigned long now = millis();
  if (now - lastRecvTime > TIMEOUT_DURATION) {
    Serial.println("Signal lost");
    resetData();  // Signal lost, Reset
  }
}

void resetData() {
  for (int i = 0; i < 16; i++) {
    Incoming_Array[i] = (i < 2) ? 512 : 1;
  }
  Incoming_Array[2] = 0;
  Incoming_Array[3] = 0;
  Incoming_Array[4] = 0;
  Incoming_Array[5] = 1;
}

void p_array() {
  Serial.print("Incoming Values : ");
  for (int i = 0; i < n; i++) {
    Serial.print(" | ");
    Serial.print(Incoming_Array[i]);
  }
  Serial.print(" | ");
  Serial.println();
}

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

void stepMotor(int steps, int speed) {
  for (int i = 0; i < steps; i++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(100000 / speed);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(100000 / speed);
  }
}

void Arm() {
  Data_recieve(Incoming_Array, n);
  timeout();
  int ang = Incoming_Array[2];
  int arm_sp = Incoming_Array[4];
  int AC = Incoming_Array[5];
  int DI = Incoming_Array[13];
  int PA = Incoming_Array[14];
  int stepCount = 0;
  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;

  // 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;
  }

  stepCount = 0;
  int originalPosition = stepCount;

  switch (state) {
    case IDLE:
      if (PA == 1 && AC == 1) {
        digitalWrite(stepPin, LOW);
        state = IDLE;
      } else if (AC == 0) {
        stepCount = 0;
        state = Step1;
      } else if (PA == 0) {
        state = IDLE1;
        return;
      }
      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");
      executeStep2(originalPosition, stepCount);
      break;

    case Step4:
      moveToCenterPosition(originalPosition, arm_sp);
      state = IDLE;
      break;

    case Step3:
      Data_recieve(Incoming_Array, n);
      timeout();
      if (PA == 0) {
        state = IDLE1;
      }
      break;

    case Step5:
      Data_recieve(Incoming_Array, n);
      timeout();
      if (PA == 0) {
        state = IDLE1;
      } else {
        executeStep5(originalPosition, stepCount);
      }
      break;
  }
}

void executeStep2(int &originalPosition, int &stepCount) {
  Data_recieve(Incoming_Array, n);
  timeout();
  int ang = Incoming_Array[2];
  int arm_sp = Incoming_Array[4];
  int AC = Incoming_Array[5];
  int DI = Incoming_Array[13];

  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;

  while (AC == 0) {
    Serial.println("While loop Step 2");
    digitalWrite(Outgoing_Arm_Cycle1, HIGH);
    Data_recieve(Incoming_Array, n);
    timeout();
    ang = Incoming_Array[2];
    arm_sp = Incoming_Array[4];
    AC = Incoming_Array[5];
    DI = Incoming_Array[13];

    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;

    setDirectionAnticlockwise();  // Ensure initial direction is anticlockwise
    int targetPosition1 = originalPosition + ang;  // Define first target position
    Serial.println("Moving to Target Position1 in Step2: " + String(targetPosition1));
    moveToPosition1(targetPosition1, arm_sp, DI);
    delay(500);
    Data_recieve(Incoming_Array, n);
    timeout();
    ang = Incoming_Array[2];
    arm_sp = Incoming_Array[4];
    AC = Incoming_Array[5];
    DI = Incoming_Array[13];
    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;
    delay(Delay);

    if (AC == 1) {
      Serial.println("Moving to Center Position in Step2: 0");
      delay(Delay);
      moveToCenterPosition(targetPosition1, arm_sp);
      stepCount = 0;
      digitalWrite(Outgoing_Arm_Cycle1, HIGH);
      state = IDLE;
      Serial.println("Going to IDLE from Step2 - at 0 Position");
      break;
    }
    //    setDirectionAnticlockwise();
    digitalWrite(Outgoing_Arm_Cycle1, HIGH);
    setDirectionClockwise();  // Now change direction to clockwise
    int targetPosition2 = originalPosition - ang; // Define new target position

    Serial.println("Moving to Target Position2 in Step2: " + String(targetPosition2));
    moveToPosition2(targetPosition2, arm_sp, DI);
    delay(1000);
    Serial.println("Cycle 2 Complete");

    Data_recieve(Incoming_Array, n);
    timeout();
    ang = Incoming_Array[2];
    arm_sp = Incoming_Array[4];
    AC = Incoming_Array[5];
    DI = Incoming_Array[13];
    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;

    if (AC == 1) {
      Serial.println("Stepper Position: " + String(stepper.currentPosition()));
      state = Step4;
      Serial.println("Going to Step4 from Step2 - to reach 0 Position");
      break;
    }
    delay(200);
    state = Step1;
  }
}

void executeStep5(int &originalPosition, int &stepCount) {
  Data_recieve(Incoming_Array, n);
  timeout();
  int ang = Incoming_Array[2];
  int arm_sp = Incoming_Array[4];
  int AC = Incoming_Array[5];
  int DI = Incoming_Array[13];

  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;

  while (true) {
    setDirectionClockwise();  // Ensure initial direction is clockwise
    int targetPosition2 = originalPosition - ang;  // Define new target position
    Serial.println("Moving to Target Position2 in Step5: " + String(targetPosition2));
    moveToPosition1(targetPosition2, arm_sp, DI);

    if (AC == 1) {
      Serial.println("Stepper Position: " + String(stepper.currentPosition()));
      moveToCenterPosition(targetPosition2, arm_sp);
      state = IDLE;
      Serial.println("Going to IDLE from Step5 - at 0 Position");
      break;
    }

    Data_recieve(Incoming_Array, n);
    timeout();
    ang = Incoming_Array[2];
    arm_sp = Incoming_Array[4];
    AC = Incoming_Array[5];
    DI = Incoming_Array[13];
    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;

    setDirectionAnticlockwise();  // Now change direction to anticlockwise
    int targetPosition1 = originalPosition + ang;  // Define first target position
    Serial.println("Moving to Target Position1 in Step5: " + String(targetPosition1));
    moveToPosition1(targetPosition1, arm_sp, DI);

    if (AC == 1) {
      Serial.println("Stepper Position: " + String(stepper.currentPosition()));
      moveToCenterPosition(targetPosition1, arm_sp);
      state = IDLE;
      Serial.println("Going to IDLE from Step5 - at 0 Position");
      break;
    }
  }
}

void moveToPosition2(int targetPosition2, int arm_sp, int DI) {
  Serial.println(targetPosition2);
  for (int x = targetPosition2; x < 0; x++) {
    if (DI == 0) {
      Direction();
      Serial.println("MoveToPosition Loop 2 in DI");
    }
    stepMotor(1, arm_sp);
    Data_recieve(Incoming_Array, n);
    DI = Incoming_Array[13];
    Serial.println("MoveToPosition Loop 2");
    while (DI == 0) {
      stepMotor(1, arm_sp);  // Move in the opposite direction
      delay(10);  // Small delay to allow for smooth motion
      Data_recieve(Incoming_Array, n);
      DI = Incoming_Array[13];
    }
  }
}

void moveToPosition1(int targetPosition1, int arm_sp, int DI) {
  for (int i = 0; i < targetPosition1; i++) {
    if (DI == 0) {
      Direction();
      Serial.println("MoveToPosition Loop 1 in DI");
    }
    stepMotor(1, arm_sp);
    Data_recieve(Incoming_Array, n);
    DI = Incoming_Array[13];
    Serial.println("MoveToPosition Loop 1");
    while (DI == 0) {
      stepMotor(1, arm_sp);  // Move in the opposite direction
      delay(10);  // Small delay to allow for smooth motion
      Data_recieve(Incoming_Array, n);
      DI = Incoming_Array[13];
    }
  }
}

void moveToCenterPosition(int targetPosition, int arm_sp) {
  Serial.println("Moving to Center Position: 0");
  setDirectionClockwise();
  for (int i = targetPosition; i > 0; i--) {
    stepMotor(1, arm_sp);  // Step the motor in reverse
  }
  setDirectionAnticlockwise();
  digitalWrite(Outgoing_Arm_Cycle1, HIGH);
}

void Paint_Arm() {
  // to be added
}

void initStepper() {
  stepper.setMaxSpeed(5000);
  stepper.setSpeed(2000);
  stepper.setAcceleration(50000);  // Set Acceleration
}

void Radio() {
  radio.begin();
  radio.openReadingPipe(0, Surf1_address);
  radio.setAutoAck(false);
  radio.setDataRate(RF24_250KBPS);
  radio.setPALevel(RF24_PA_LOW);
  radio.startListening();
}

void setDirectionAnticlockwise() {
  digitalWrite(dirPin, HIGH);
}

void setDirectionClockwise() {
  digitalWrite(dirPin, LOW);
}

Your code looks like you have combined several programs. You use the AccelStepper library for homing, but you do it yourself when you are moving to postion1 or position2, etc.

I would start again using a state machine approach.

Yes, earlier this was in Accel Library later I am required to add process 4. So I am trying to convert it in state machine. I am yet to include Process 4 fully. Accel Library code is working fine with ** int targetPosition2 = originalPosition - ang; // Define new target position** and here it is not.

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