controlling 2 steppers with a fixed target position & stopping at step intervals

Hello, this is my first post and I am new to the Arduino programming world.
I've read through many forums and can't find anything that matches my problem.

The problem I am having is controlling 2 stepper motors. Stepper 1 needs to run to a target position but, I want it to stop at step intervals along the way to the target. My final target will be approx. 31000 steps but I want it to stop at steps (800 or 2400 for example) along the way for a specific time period (5 minutes for example). I have tried to stay way from the delay command for these longs delays as I have read it will stop your entire program.
Stepper 2 needs to step a certain amount of steps after stepper 1 has reached each interval. For example 5 steps when stepper 1 reaches the first interval and 10 steps when stepper 1 reaches it's second interval.

I have tried many different ways but can't seem to figure out how to get stepper 1 to move to a target position with these intervals in between. The move to () always runs to the absolute target position.
If I just add the move () stepper 1 will keep moving the specified steps over and over without allowing stepper 2 to run.

For the purpose of saving time I have set my move to () at 800 for now and move () at 200.

Sorry, I am new to this but, is there any way to get these intervals with a fixed target position with time delays in between?
I have attached my sketch below. Sorry, there are many notes as I have been trying different things.
Any help would be very appreciated.

#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

Adafruit_MotorShield AFMSbot(0x61); // Rightmost jumper closed
Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers

// Connect two steppers with 200 steps per revolution 1.8 degrees
// to the top shield
Adafruit_StepperMotor *myStepper1 = AFMStop.getStepper(200, 1);
Adafruit_StepperMotor *myStepper2 = AFMStop.getStepper(200, 2);


void forwardstep1() {  
  myStepper1->onestep(FORWARD, SINGLE);
}

// wrappers for the second motor!
void forwardstep2() {  
  myStepper2->onestep(FORWARD, SINGLE);
} 

// Now we'll wrap the 2 steppers in an AccelStepper object
// running both stepper motors forward to destination point

AccelStepper stepper1(forwardstep1, forwardstep1);
AccelStepper stepper2(forwardstep2, forwardstep2);

// define the start time of the program 
 long starttime = 0;
 long stepper2starttime = 0;
 long previousmillis = 0;
 long stepper2previousmillis = 0;
 long lastStopmillis = 0;

 long delayinterval = 10000; // 10 second delay interval
 long stepper2delayinterval = 10000; // 30 second delay interval
 
 long stepcount = 0;
 
 
void setup()
{  
    Serial.begin(9600);
    AFMStop.begin(); // Start the top shield

  stepper1.setMaxSpeed(100.0);
  stepper1.setAcceleration(100.0);
  
  // this will rotate stepper 1 (800 steps or 1 inch of travel)
  // Note: (200 steps is 360 degrees of rotation of the spindle) 
  stepper1.moveTo(800);    
   
  stepper2.setMaxSpeed(100.00);
  stepper2.setAcceleration(100.0);

  // this will rotate the stepper 1 revolutions (200 steps is 360 degrees)
   //stepper2.moveTo(200);

//starttime = millis();
//stepper2starttime = millis ();
}


void loop()
{
  
    //step stepper 1 to 800 steps (1 inch)
    //stepper1.moveTo(800);
    //stepper1.move(200);
  
    stepper1.runToPosition();  //blocks other functions until stepper reaches target steps
       //Serial.println(stepcount);
     if (stepcount <= 800)
     
     
     stepper1.run();
          
     stepcount = stepcount +1;    //increments how far we've stepped by 1
     //Serial.println(stepcount);
     
    // while (stepcount == 800)
     if (stepcount == 800)  
     
              
    // if (stepper2currentmillis - stepper2previousmillis > stepper2delayinterval)
      stepper2.moveTo(200);   
               
      stepper2.runToPosition();  //blocks other functions until stepper reaches target steps   
      stepper2.run();
      
    // unsigned long stepper2currentmillis = millis (); 
    // stepper2previousmillis = stepper2currentmillis; 
     
     
   
     //once stepper1 has moved a total of 800 steps (1 inch) set the stepcounter to 0
     //so we can repeat the move
     
      
     // if (stepcount > 800)
     //{
     //   stepcount = 0;
     //}
     
          
}

I should add.
The way the program is right now it will run Stepper1 800 steps and then stop and then run Stepper2 200 steps and then stop.

I'm not really clear what you want to happen. Maybe you can write it down with one element on each line like

stepperA move 50
stepperB move100
stepperA move 75
etc

Do you want the two motors to move at the same time ?

There are two modes for using Accelstepper - you seem to be using the Blocking method runToPosition() which completes the move before anything else happens

The other way is to set a destination with moveTo() or move() and then cause the motor to move with repeated calls to run(). In this case you need to ensure that your code allows loop() to repeat quickly so that it can call run() often enough.

If you want the motor to stop along the way I think the easiest thing is just to get it to move to the intermediate position and later tell it to go to the next position. You can use distanceToGo() to determine when it has reached the position.

The demo several things at a time illustrates the use of millis() to manage timing without blocking

...R
Stepper Motor Basics

Thanks for the reply.
I am trying to get the motors to run separately, Stepper1 to complete it's steps and then Stepper2 to complete it's steps. The only way I could accomplish this was to use the blocking "runtoposition ()".
Right now stepper1 moves 800steps and when that is finished stepper 2 moves 200 steps.
This is an intermediate position and I still need to figure out how to get this to repeat to get to a final position.

My problem is that I can't get it to loop to repeat this function.
And I am trying to set a delay of a certain time before it repeats the loop.

I have tried the "if" command using "millis" to set up a delay and then set my "stepcount" to 0 to repeat the loop but, it doesn't work for me.
something like this......

if (stepper2currentmillis - stepper2previousmillis > stepper2delayinterval)
{
stepcount = 0;
}

or......................to reset the stepcount so it will repeat the loop

if (stepcount == 800)
{
stepcount = 0;
}


This is where I am now. Still can't get it to work??

unsigned long currentmillis = 0; //stores the value of millis
unsigned long previousStepTime = 0; //will store the previous step time
unsigned long stepDelayInterval = 10000; // 10 second delay interval
int stepcount = 0;

void setup()
{
Serial.begin(9600);
AFMStop.begin(); // Start the top shield

stepper1.setMaxSpeed(100.0);
stepper1.setAcceleration(100.0);

stepper2.setMaxSpeed(100.00);
stepper2.setAcceleration(100.0);

void loop()
{
unsigned long currentmillis = millis ();

//step stepper 1 to 800 steps (1 inch)

stepper1.moveTo(800);

stepper1.runToPosition(); //blocks other functions until stepper reaches target steps

if (stepcount < 800)

stepper1.run();

stepcount = stepcount +1; //increments how far we've stepped by 1

if (stepcount == 800)

stepper2.moveTo(200);

stepper2.runToPosition(); //blocks other functions until stepper reaches target steps
stepper2.run();

if (currentmillis - previousStepTime >= stepDelayInterval)
{
stepcount = 0;

}

//once stepper1 has moved a total of 800 steps (1 inch) set the stepcounter to 0
//so we can repeat the move

if (stepcount == 800)
{
stepcount = 0;
}

}

Please use the code button for your code - as in your Original Post - and reduce the number of blank lines.

If you want a sequence of moves you could store the destinations in an array and iterate through the array. Something like (very crudely)

int motorPositions[] = {200,400,800};
byte numPositions = 3;
byte curPosition = 0;


void loop() {
   curMillis = millis();
   if (curMillis - prevMoveMillis >= intervalMillis) {
      prevMoveMillis += intervalMillis;
      if (curPosition < numPositions) {
          myMotor.runToPosition(motorPositions[curPosition]);
          curPosition ++;
          // other stuff
       }
   }
}

...R

Thank you.
Using the array will be helpful but, I can't seem to get it to work.

Where the [curPosition] is in the line below. Is that not where I should put my index number for the array (0 or 1 or 2)??

myMotor.runToPosition(motorPositions[curPosition]);

Also, with my previous code.
I can't seem to figure out why but,
Could anyone tell me the reason that my loop will not loop? And also delay?

[code]

 
 
 unsigned long currentmillis = 0;           //stores the value of millis
 unsigned long previousStepTime = 0;        //will store the previous step time 
 unsigned long stepDelayInterval = 10000;   // 10 second delay interval 
 int stepcount = 0;
 
 
void setup()
{  
    Serial.begin(9600);
    AFMStop.begin(); // Start the top shield

  stepper1.setMaxSpeed(100.0);
  stepper1.setAcceleration(100.0);
  
  
  stepper2.setMaxSpeed(100.00);
  stepper2.setAcceleration(100.0);

}


void loop()
{
    unsigned long currentmillis = millis ();
   
    
    //step stepper 1 to 800 steps (1 inch)
    stepper1.moveTo(800);
   
  
    stepper1.runToPosition();  //blocks other functions until stepper reaches target steps
       
     if (stepcount < 800)    
     
     stepper1.run();
          
     stepcount = stepcount +1;    //increments how far we've stepped by 1
     
     if (stepcount == 800)  
     
              
      stepper2.moveTo(200);   
               
      stepper2.runToPosition();  //blocks other functions until stepper reaches target steps   
      stepper2.run();
      
    
   
    
    if (currentmillis - previousStepTime >= stepDelayInterval)
    {
     stepcount = 0;
    }
     
            
     //once stepper1 has moved a total of 800 steps (1 inch) set the stepcounter to 0
     //so we can repeat the move
     
      
      if (stepcount == 800)
     {
        stepcount = 0;
     }
     
          
}

[/code]

Your code has lots wrong with (see below) but, most importantly, it does not have an array of positions or code to move through the array so I can't help you with that part. Try to apply the system I suggested and post the code that includes them.

You need to distiguish in your mind between steps for the motor (which are managed using moveTo) and the different positions that will be held in the array. The code I have annotated seems to be mixing them up.

 unsigned long currentmillis = 0;           //stores the value of millis
 unsigned long previousStepTime = 0;        //will store the previous step time
 unsigned long stepDelayInterval = 10000;   // 10 second delay interval
 int stepcount = 0;
 
 
void setup()
{  
  Serial.begin(9600);
  AFMStop.begin(); // Start the top shield

  stepper1.setMaxSpeed(100.0);
  stepper1.setAcceleration(100.0);

  stepper2.setMaxSpeed(100.00);
  stepper2.setAcceleration(100.0);
}

void loop()
{
    currentmillis = millis ();  //=== don't use unsigned long here as you already have it at the top
    //step stepper 1 to 800 steps (1 inch)
    stepper1.moveTo(800);
    stepper1.runToPosition();  //blocks other functions until stepper reaches target steps
       
     //~ if (stepcount < 800)    //==== these lines are irrelevant if you use runToPosition
     //~ stepper1.run();
     //~ stepcount = stepcount +1;    //increments how far we've stepped by 1
     //~ if (stepcount == 800) //==== end of irrelevant
     
      stepper2.moveTo(200);   
               
      stepper2.runToPosition();  //blocks other functions until stepper reaches target steps   
      //~ stepper2.run();    //==== this line is irrelevant if you use runToPosition

    if (currentmillis - previousStepTime >= stepDelayInterval) //===== I am lost here
    {
     stepcount = 0;
    }
     //once stepper1 has moved a total of 800 steps (1 inch) set the stepcounter to 0
     //so we can repeat the move
      if (stepcount == 800)
     {
        stepcount = 0;
     }
}

...R

I was able to get my motors running with that particular code that you annotated. I just could not get a millis delay to delay the code from looping. I have tried many examples and millis does not seem to delay on the motors the way it should.

I have also tried the system that you suggested with the array. Due to my lack of knowledge with the stepper library I can't figure out how to give my index number and therefore, the sketch will not verify

In this particular line of the code....... myMotor.runToPosition(motorPositions[curPosition]);
(this is where the verify error is)

Where the [curPosition] is in the line below. Is that not where I should put my index number for the array (0 or 1 or 2 or 3)??
I have tried the 0 in my code. Which I assumed would be 200 steps for stepper1 or 20 steps for stepper2?

[code]

// Requires the Adafruit_Motorshield v2 library 
//   https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library
// And AccelStepper with AFMotor support 
//   https://github.com/adafruit/AccelStepper


#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

Adafruit_MotorShield AFMSbot(0x61); // Rightmost jumper closed
Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers

// Connect two steppers with 200 steps per revolution 1.8 degrees
// to the top shield
Adafruit_StepperMotor *myStepper1 = AFMStop.getStepper(200, 1);
Adafruit_StepperMotor *myStepper2 = AFMStop.getStepper(200, 2);

// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
// wrappers for the first motor!

void forwardstep1() {  
  myStepper1->onestep(FORWARD, SINGLE);
}

// wrappers for the second motor!
void forwardstep2() {  
  myStepper2->onestep(FORWARD, SINGLE);
} 

// Now we'll wrap the 2 steppers in an AccelStepper object
// running both stepper motors forward to destination point
AccelStepper stepper1(forwardstep1, forwardstep1);
AccelStepper stepper2(forwardstep2, forwardstep2);

// define the start time of the program  
 unsigned long previousStepTime = 0;          //will store the previous step time 
 unsigned long stepper2previousStepTime = 0;  //will store stepper2 previous step time 
 unsigned long stepDelayInterval = 10000;     // 10 second delay interval 
 unsigned long stepDelayInterval2 = 15000;    // 15 second delay interval
 int stepcount = 0;
 int stepper1State = HIGH;
 
 int motorPositions[] = {200,400,800,1600};    //sample using 4 positions
 int numPositions = 4;
 int curPosition = 0;

 int stepper2motorPositions[] = {20,40,80,160};    //sample using 4 positions
 int stepper2numPositions = 4;
 int stepper2curPosition = 0;
 
void setup()
{  
    Serial.begin(9600);
    AFMStop.begin(); // Start the top shield

  stepper1.setMaxSpeed(100.0);
  stepper1.setAcceleration(100.0);
  
    //stepper1.moveTo(800);    //i assume this is not used
   
  stepper2.setMaxSpeed(100.00);
  stepper2.setAcceleration(100.0);

   //stepper2.moveTo(200);    // i assume this is not used

}

void loop()
{
    unsigned long currentmillis = millis ();
        
        //step stepper 1 to 800 steps (1 inch)
       // stepper1.moveTo(800);    
  
    if (currentmillis - previousStepTime >= stepDelayInterval)
      {
       previousStepTime += stepDelayInterval;
    if (curPosition < numPositions) 
      {
       stepper1.runToPosition(motorPositions[0]);
       curPosition ++;
      }
      
      
      if (currentmillis - stepper2previousStepTime >= stepDelayInterval2)
      {
       stepper2previousStepTime += stepDelayInterval2;
    if (stepper2curPosition < stepper2numPositions) 
      {
       stepper1.runToPosition(stepper2motorPositions[0]);
       stepper2curPosition ++;
      }
         
 //******************temporary trial code ************************
 //now I need to figure out a millis delay in order to delay the repeating loop
 // for a specific time before it starts again
 // i tried to change the motor between a low (off) and high (on)
       //Serial.println(currentmillis);

       // if (currentmillis - previousStepTime >= stepDelayInterval1)
       //{
       // stepper1 = LOW;
       //previousStepTime = currentmillis;
       //Serial.println("Step delay interval 1 check true");  //should be 15000 milliseconds
       // } 
    
       //else if (currentmillis - previousStepTime >= stepDelayInterval)
       //{            
       //stepper1 = HIGH;
       //Serial.println("Step delay interval check true");
       // previousStepTime = currentmillis; 
       //}
      
     }        
}

[/code]

You seem to have some of your } in the wrong place so that IFs are nested when they should not be. If you indent your code carefully and consistently that sort of thing becomes obvious.

Try this version

void loop()
{
 unsigned long currentmillis = millis ();

 if (currentmillis - previousStepTime >= stepDelayInterval)
 {
 previousStepTime += stepDelayInterval;
 if (curPosition < numPositions)
 {
 stepper1.runToPosition(motorPositions[curPosition]); //==== changed
 curPosition ++;
 }
 
 } //===== NEW
 if (currentmillis - stepper2previousStepTime >= stepDelayInterval2)
 {
 stepper2previousStepTime += stepDelayInterval2;
 if (stepper2curPosition < stepper2numPositions)
 {
 stepper1.runToPosition(stepper2motorPositions[stepper2curPosition]); //==== changed
 stepper2curPosition ++;
 }
 } //======NEW
}

Life will be much easier if you put the stepper move code into functions so that the code in loop() can be cleaner.

Look at planning and implementing a program.

...R

I tried the code that you annotated. Thank you for the annotation.
But, I seem to be getting the same error when I verify the sketch.

In this particular line of the code....... myMotor.runToPosition(motorPositions[curPosition]);
(this is where the verify error is)
note: void AccelStepper::runToPosition()
void runToPosition();
note: candidate expects 0 arguments, 1 provided
no matching function for call to 'AccelStepper::runToPosition(int&)'

I am not sure what this error means. This is why I was thinking that I needed to give it an index number for the array? Where the [curPosition] is in the line below. Do I need to put the (0 or 1 or 2 or 3)??
I had tried the 0 in my previous code. Which I assumed would be 200 steps for stepper1 or 20 steps for stepper2. Should I be placing the index number in another spot? I am not sure how to make the array function.

// define the start time of the program  
 unsigned long previousStepTime = 0;          //will store the previous step time 
 unsigned long stepper2previousStepTime = 0;  //will store stepper2 previous step time 
 unsigned long stepDelayInterval = 10000;     // 10 second delay interval 
 unsigned long stepDelayInterval2 = 15000;    // 15 second delay interval
 int stepcount = 0;
 int stepper1State = HIGH;
 
 int motorPositions[] = {200,400,800,1600};    //sample using 4 positions
 int numPositions = 4;
 int curPosition = 0;

 int stepper2motorPositions[] = {20,40,80,160};    //sample using 4 positions
 int stepper2numPositions = 4;
 int stepper2curPosition = 0;
 
void setup()
{  
    Serial.begin(9600);
    AFMStop.begin(); // Start the top shield

  stepper1.setMaxSpeed(100.0);
  stepper1.setAcceleration(100.0);
  
    //stepper1.moveTo(800);    //i assume this is not used
   
  stepper2.setMaxSpeed(100.00);
  stepper2.setAcceleration(100.0);

   //stepper2.moveTo(200);    // i assume this is not used

}

void loop()
{
 unsigned long currentmillis = millis ();

 if (currentmillis - previousStepTime >= stepDelayInterval)
 {
 previousStepTime += stepDelayInterval;
 if (curPosition < numPositions)
 {
 stepper1.runToPosition(motorPositions[curPosition]); //==== changed
 curPosition ++;
 }
 
 } //===== NEW
 if (currentmillis - stepper2previousStepTime >= stepDelayInterval2)
 {
 stepper2previousStepTime += stepDelayInterval2;
 if (stepper2curPosition < stepper2numPositions)
 {
 stepper2.runToPosition(stepper2motorPositions[stepper2curPosition]); //==== changed
 stepper2curPosition ++;
 }
 } //======NEW
}

I am not sure what this error means.

It means exactly what it says. The runToPosition() method tells the stepper to move to the last position that it was told to go to. It takes no arguments. You are trying to supply one - presumably a new position. That is wrong. You need to call the correct method to define a new position, and then call runToPosition() to make the stepper get there, waiting until the stepper has moved.

That's what I am not sure of. I thought the array may be able to index the stepper through a series of steps (different positions) along the way to a final position. I have not been able to figure this out??

If anyone could help with this it would be appreciated.

I had used the moveto() command and then a runtoposition() command on a previous program but, this only runs the motor to one specified position. It does not give me steps along the way to a final position.

I thought the array may be able to index the stepper through a series of steps (different positions) along the way to a final position.

I have no idea why you thought that.

I have not been able to figure this out??

Clearly.

I had used the moveto() command and then a runtoposition() command on a previous program but, this only runs the motor to one specified position. It does not give me steps along the way to a final position.

I really don't understand the problem. Suppose that you want to move the stepper to 10, 20, 30, 40, and 50, with 10 minute delays between positions. You would do a moveTo(10), followed by a runToPosition(), followed by some means of waiting 10 minutes. Then, you'd do a moveTo(20), followed by a runToPosition(), followed by some means of waiting 10 minutes. You'd repeat that to move to 30, 40, and 50.

You would NOT try to make runToPosition() do something it is not designed for.

roym:
That's what I am not sure of. I thought the array may be able to index the stepper through a series of steps

YOU need to take each value from the array in turn and give it to the appropriate AccelStepper function using either moveTo() or move(). Then you can call runToPosition() to make it move to that position

...R

I have resolved to just use the moveTo() and then call the runToPosition() in my program.
They way I have it now, after a delay using millis() I can run stepper1 and then run stepper2 and then the loop will continue. I have used the runToPosition() because I want them run in sequence.

I have now tried to add in a limit switch to stop the loop. It currently will stop the loop the way I have it but, it will not stop stepper1 and stepper2 from running while they are running because of the runToPostition() which blocks. It will only stop them after they are finished.

I tried to put an "if" command for the limit switch in between the moveTo() and the runToPosition() to see if that would do it but, this still does not stop it. Only after they are finished again.

Is there any way I could do this?

void forwardstep1() {  
  myStepper1->onestep(FORWARD, SINGLE);
}
void backwardstep1() {  
  myStepper1->onestep(BACKWARD, SINGLE);
}

// wrappers for the second motor!
void forwardstep2() {  
  myStepper2->onestep(FORWARD, SINGLE);
}
void backwardstep2() {  
  myStepper2->onestep(BACKWARD, SINGLE);
} 

// Now we'll wrap the 2 steppers in an AccelStepper object
// running both stepper motors forward to destination point
AccelStepper stepper1(forwardstep1, backwardstep1);
AccelStepper stepper2(forwardstep2, backwardstep2);

// define the start time of the program 
 long starttime = 0;
 long stepper2starttime = 0; 
 
 long stepper2previousmillis = 0;
 long lastStopmillis = 0;

 
 // constants won't change. Used here to set a pin number :
 const int ledPin = 24;                  // stepper activity LED connected to digital pin 24 
 const int LimitSwitchPin = 47;
 
 // Variables will change :
 unsigned long currentmillis = 0;    //stores value of millis
 unsigned long previousStepTime = 0;        //will store the previous step time 
 unsigned long stepDelayInterval = 15000;   // 15 second delay interval
 unsigned long stepDelayInterval1 = 15000;   // 10 second delay interval 
 int stepcount = 0;
 int stepper1State = LOW;             //HIGH is ON
 int stepper2State = LOW;             //HIGH is ON
 int ledState = LOW;                   // ledState used to stepper activity (operation)
 int LimitSwitchState = HIGH;
  
void setup()
{  
    Serial.begin(9600);
    AFMStop.begin(); // Start the top shield
    
   pinMode(ledPin, OUTPUT);           //set the activity LED pin as an ouput
   digitalWrite(ledPin, LOW);         //turn off the activity LED pin
   pinMode(LimitSwitchPin, INPUT);    //set the limit switch as an input
   
  stepper1.setMaxSpeed(100.0);
  stepper1.setAcceleration(100.0);
  
  stepper2.setMaxSpeed(100.00);
  stepper2.setAcceleration(100.0);
}

void loop()
{
      Serial.println("Loop starts");      //show us that the loop is running
      currentmillis = millis ();            //stores the value of millis 
      LimitSwitchState = digitalRead(LimitSwitchPin);
 
  if (stepper1State == LOW)            
    {      
  if (currentmillis - previousStepTime >= stepDelayInterval)
   {       
     Serial.println("Step delay interval 1 check true");  
     //time is up so change the state of stepper1 & stepper 2 to HIGH (on)
    stepper1State = HIGH;             
    stepper2State = HIGH;
       previousStepTime += stepDelayInterval;
     Serial.println("Stepper 1&2 are high");
     Serial.println(currentmillis);
             
         //step stepper 1 to 800 steps (1 inch)
      
    stepper1.moveTo(800);           //this will step an absolute value to 800 steps
       //stepper1.move(200);       //this will override moveto and keep looping 200 steps
    

      // Change direction at the limit switch
 if (LimitSwitchState == LOW)
    {	
      Serial.println("Limit Switch set to LOW");
      stepper1.moveTo(-400);
      stepper2.moveTo(-200);
      
      stepper1.runToPosition(); 
      stepper2.runToPosition();
      while(LimitSwitchState == LOW) { } //this while line runs an endless loop
                                                           //it will stops everything else
    }
      
    stepper1.runToPosition();      //blocks other functions until stepper reaches target steps
     
    digitalWrite(ledPin, HIGH);     // LED ON at digital pin24 shows stepper activity
    
    
    stepper2.moveTo(200);   
         //stepper1.move(200);      //this will move 200 steps and keep looping 200 steps
    stepper2.runToPosition();      //blocks other functions until stepper reaches target steps   
         //stepper2.run();
     
    digitalWrite(ledPin, LOW);    // LED OFF at digital pin24 shows no stepper activity
   }  
    }
 
 else 
  {  
    if (currentmillis - previousStepTime >= stepDelayInterval1)
    {
    stepper1State = LOW;             
    stepper2State = LOW;
     previousStepTime += stepDelayInterval1;
     Serial.println("Stepper 1&2 are low");
     Serial.println(currentmillis);
    }
  }
              
      
   if (stepper1.currentPosition()== 800)
    {
      stepper1.setCurrentPosition(0);
      stepper2.setCurrentPosition(0);
    
      Serial.println("Stepper1&2 set to 0 position"); 
    } 
           
 }
[/code

Is there any way I could do this?

Why is you post in a code block? There is no code.

Yes, there is a way to do what you want. Adjust your thinking.

Instead of:

servo1.moveTo(somePositionFarAway);
servo1.runToPosition();
if(checkSwitch() == SHOULD_STOP)
// Stop
// Oops, we busted the switch slamming into it

do something like:

servo1.moveTo(somePositionOneStepAway);
while(!checkSwitch() == SHOULD_STOP)
{
   servo1.runToPosition();
   somePositionOneStepAway++;
}

Of course, since you are DELIBERATELY running one motor at a time, it makes NO sense to be using anything other than the blocking Stepper library, which is FAR simpler to use.

CustomStepper and AccelStepper are useful when the blocking nature of Stepper gets in the way of running two or more steppers at the same time. They are not useful when the blocking nature of Stepper is being deliberately recreated.

@roym, please modify your Reply #14 so that it is readable.

...R

I have now made my program to control 2 stepper motors using the moveto() command and I am using the same code in 6 cycles to get to a final position.

The problem I am having now is trying to delay between the cycles using millis(). I can get the first cycle delay or stepinterval to work but, after that the next cycle begins right away with no delay. I have found a few other examples and they all seem to just change the delay interval but, for some reason I can't get it to work.
I have used stepdelayinterval, stepdelayintervalcycle2, stepdelayintervalcycle3.......as so on

Can anybody help me getting these delays to work?

AccelStepper stepper1(forwardstep1, backwardstep1);
AccelStepper stepper2(forwardstep2, backwardstep2);

 // constants won't change. Used here to set a pin number :
 const int ledPin = 24;                  // stepper activity LED connected to digital pin 24 
 
 
 // define the start time of the program  
 // Variables will change :
 unsigned long currentmillis = 0;               //stores value of millis
 unsigned long previousStepTime = 0;            //will store the previous step time 
 unsigned long stepDelayInterval = 5000;        // 5 second delay interval (cycle1)
 unsigned long stepDelayInterval1 = 10000;      // 10 second delay interval
 unsigned long stepDelayIntervalcycle2 = 6000;     
 unsigned long stepDelayIntervalcycle3 = 7000;   
 unsigned long stepDelayIntervalcycle4 = 8000;   
 unsigned long stepDelayIntervalcycle5 = 9000;   
 unsigned long stepDelayIntervalcycle6 = 10000;   
 int stepcount = 0;
 int stepper1State = LOW;             //HIGH is ON
 int stepper2State = LOW;             //HIGH is ON
 int ledState = LOW;                   // ledState used to stepper activity (operation)
 
  
void setup()
{  
    Serial.begin(9600);
    AFMStop.begin(); // Start the top shield
    
   pinMode(ledPin, OUTPUT);           //set the activity LED pin as an ouput
   digitalWrite(ledPin, LOW);         //turn off the activity LED pin
   
  stepper1.setMaxSpeed(100.0);
  stepper1.setAcceleration(100.0);
   
  stepper2.setMaxSpeed(100.00);
  stepper2.setAcceleration(100.0);
}

void loop()
{
      Serial.println("Loop starts");      //show us that the loop is running
      currentmillis = millis ();  //stores the value of millis (notes the time on a clock)
     
      
  if (stepper1State == LOW)
   {      
  if (currentmillis - previousStepTime >= stepDelayInterval)
   {   
     Serial.println("Cycle 1 of 6 is active");      
     Serial.println("Step delay interval 1 check true");  
     //time is up so change the state of stepper1 & stepper 2 to HIGH (on)
    stepper1State = HIGH;             
    stepper2State = HIGH;
       previousStepTime += stepDelayInterval;
       
     Serial.println("Stepper 1&2 are high");
     Serial.println(currentmillis);
      
    stepper1.moveTo(800);           //this will step an absolute value to 800 steps
    stepper1.runToPosition();  //blocks other functions until stepper reaches target steps
     
    digitalWrite(ledPin, HIGH);   // LED ON at digital pin24 shows stepper activity
       
    stepper2.moveTo(200);   
    stepper2.runToPosition();  //blocks other functions until stepper reaches target steps   
     
    digitalWrite(ledPin, LOW);    // LED OFF at digital pin24 shows no stepper activity
   }
   delay (1000);
       Serial.println("continue to the next cycle");
   }
   
    //---------cycle2-------------------------------------------  
   if (stepper1State == HIGH)
   currentmillis = millis ();
   {  
  if (currentmillis - previousStepTime >= stepDelayIntervalcycle2)
   {   
     Serial.println("Cycle 2 of 6 is active");      
     Serial.println("Step delay cycle 2 check true");  
     //time is up so change the state of stepper1 & stepper 2 to HIGH (on)
    stepper1State = HIGH;             
    stepper2State = HIGH;
       previousStepTime += stepDelayInterval;
      
     Serial.println("Stepper 1&2 are high");
     Serial.println(currentmillis);

    stepper1.moveTo(1600);           //this will step an absolute value to 800 steps
    stepper1.runToPosition();  //blocks other functions until stepper reaches target steps
     
    digitalWrite(ledPin, HIGH);   // LED ON at digital pin24 shows stepper activity
       
    stepper2.moveTo(400);   
    stepper2.runToPosition();  //blocks other functions until stepper reaches target steps   
     
    digitalWrite(ledPin, LOW);    // LED OFF at digital pin24 shows no stepper activity
   }
    delay (1000);
       Serial.println("continue to the next cycle");
   }

   //---------cycle3--------------------------------------------  
   if (stepper1State == HIGH)
   currentmillis = millis ();
   {
  if (currentmillis - previousStepTime >= stepDelayIntervalcycle3)
   {   
     Serial.println("Cycle 3 of 6 is active");      
     Serial.println("Step delay cycle 3 check true");  
     //time is up so change the state of stepper1 & stepper 2 to HIGH (on)
    stepper1State = HIGH;             
    stepper2State = HIGH;
       previousStepTime += stepDelayInterval;
       
     Serial.println("Stepper 1&2 are high");
     Serial.println(currentmillis);

    stepper1.moveTo(2400);           //this will step an absolute value to 800 steps
    stepper1.runToPosition();  //blocks other functions until stepper reaches target steps
     
    digitalWrite(ledPin, HIGH);   // LED ON at digital pin24 shows stepper activity
       
    stepper2.moveTo(600);   
    stepper2.runToPosition();  //blocks other functions until stepper reaches target steps   
     
    digitalWrite(ledPin, LOW);    // LED OFF at digital pin24 shows no stepper activity
   }
   delay (1000);
       Serial.println("continue to the next cycle");
   }
 
   //---------cycle4--------------------------------------------------
   //---------cycle5--------------------------------------------------
 
      //---------cycle6--------------------------------------------------
   if (stepper1State == HIGH)
   currentmillis = millis ();
   {
   if (currentmillis - previousStepTime >= stepDelayIntervalcycle6)
   {   
     Serial.println("Cycle 6 of 6 is active");      
     Serial.println("Step delay cycle 6 check true");  
     //time is up so change the state of stepper1 & stepper 2 to HIGH (on)
    stepper1State = HIGH;             
    stepper2State = HIGH;
       previousStepTime += stepDelayInterval;
       
     Serial.println("Stepper 1&2 are high");
     Serial.println(currentmillis);
    stepper1.moveTo(4800);           //this will step an absolute value to 800 steps
    stepper1.runToPosition();  //blocks other functions until stepper reaches target steps
     
    digitalWrite(ledPin, HIGH);   // LED ON at digital pin24 shows stepper activity
 
    stepper2.moveTo(1200);   
    stepper2.runToPosition();  //blocks other functions until stepper reaches target steps   
     
       // Change direction at the limits-----------------------------------
    if (stepper1.distanceToGo() == 0)
     {
       Serial.println("Cycle 6 of 6 Reverse Stepper1");	
       currentmillis = millis ();
       if (currentmillis - previousStepTime >= stepDelayInterval+5000)
        {
          Serial.println("Step delay until reverse");
          //previousStepTime += stepDelayInterval;
           previousStepTime += currentmillis;
        }
       
        stepper1.moveTo(-0);     //setting of 0 actually moves stepper1 back the 4800steps
        //stepper1.moveTo(-stepper1.currentPosition());
        stepper1.runToPosition();
        stepper1State = LOW;
    if (stepper2.distanceToGo() == 0)
       Serial.println("Cycle 6 of 6 Reverse Stepper2");       
	stepper2.moveTo(-0);     //setting of 0 actually moves stepper2 back
        //stepper2.moveTo(-stepper2.currentPosition());
        stepper2.runToPosition();
        stepper2State = LOW;
     }
    digitalWrite(ledPin, LOW);    // LED OFF at digital pin24 shows no stepper activity
    Serial.println("End of the 6 cycles");
    while(1) { }                        //this while line runs an endless loop
                                        //it will stop everything else
    }
   }
 }

[/code]

Lines 143 and 150 refer to different step delays - would that have anything to do with ?

Printing currentMillis is not likely to be informative.

You still have delay() all over the place. You should take them ALL out.

...R

Sorry, I did not update those lines to be the same as I was trying different interval lines to see if I could somehow get a delay between cycles.

Using these lines (see code) seems to work for other examples I have seen but, I can't seem to get a delay between cycles. Just the first delay interval works and after that it just continues from cycle 2 to cycle 6 with no millis() delay between the cycles.

    //--------------------------cycle2-------------------------------------------  
   if (stepper1State == HIGH)
   currentmillis = millis ();
   {  
  if (currentmillis - previousStepTime >= stepDelayIntervalcycle2)
   {   
     Serial.println("Cycle 2 of 6 is active");      
     Serial.println("Step delay cycle 2 check true");  
     //time is up so change the state of stepper1 & stepper 2 to HIGH (on)
    stepper1State = HIGH;             
    stepper2State = HIGH;
       previousStepTime += stepDelayIntervalcycle2;
      
     Serial.println("Stepper 1&2 are high");
     Serial.println(currentmillis);

    stepper1.moveTo(1600);          //this will step an absolute value to 1600 steps (800+800)
    stepper1.runToPosition();        //blocks other functions until stepper reaches target steps
     
    digitalWrite(ledPin, HIGH);      // LED ON at digital pin24 shows stepper activity
       
    stepper2.moveTo(400);   
    stepper2.runToPosition();       //blocks other functions until stepper reaches target steps   
     
    digitalWrite(ledPin, LOW);      // LED OFF at digital pin24 shows no stepper activity
   }
    
   };