Thanks a lot for that perfect explanation, really helped a lot.
Your code works perfect as it is. I made a small change in the code so that the stepper motor will repeatedly move to the same position over and over again by setting the position of the stepper to zero everytime the index becomes equal to the number of steps. I felt like the code makes sense but the outut doesnt seem to be inline with the code.
The following is the code
#include <AccelStepper.h>
const unsigned int NUM_STEPS = 4;
unsigned int xArray[NUM_STEPS] = {0, 50, 100, 150};
unsigned int xSpeeds[NUM_STEPS] = {200, 200, 200, 200};
const byte enablePin = 8;
AccelStepper x_stepper(AccelStepper::DRIVER, 2, 5);
static unsigned int index = 0;
void setup()
{
Serial.begin(115200);
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, LOW);
x_stepper.setAcceleration(2000);
x_stepper.setMaxSpeed(200);
x_stepper.setSpeed(200);
x_stepper.setCurrentPosition(0);
}
void loop()
{
if (x_stepper.run() == 0)
{
delay(250);
Serial.println(index);
x_stepper.moveTo(xArray[index]);
x_stepper.setMaxSpeed(xSpeeds[index]);
index++;
if (index >= NUM_STEPS)
{
index = 0;
x_stepper.setCurrentPosition(0);
delay(80);
}
}
}
I am attaching the drive link to the video of how the motor is rotating. I want the motor to keep stopping at posiiton 0 50 100 and 150respectively but it is not working for some unknown reason. Could you please point out the mistakes i am making , if any...
Drive link for video: https://drive.google.com/file/d/1PJiFoTznvVhXit-91CPM4gjkczostSxQ/view?usp=sharing