halt one stepper until other has revolved once, then continue simultaneously

Hi everyone,
I would like my two motors to do the following:

  1. stepper1 and stepper2 turn simultanously for 6400 steps
  2. stepper1 stops until stepper2 has completed another 3200 steps
  3. stepper1 and stepper2 turn again simultaneously for 6400 steps
  4. stepper1 stops until stepper2 has completed another 3200 steps
  5. stepper1 and stepper2 turn again simultaneously for 6400 steps.

All of this should happen when I send a "1" from serial communication. I am using the accelstepper library (AccelStepper: AccelStepper library for Arduino).
My code so far, including serial communication and some other important chunks that have to stay in it is below. How and where do I have to modify the code? Will I have to insert another if-loop in the if (letter=='1') loop? I have tried around with different loops and commands but have not been able to figure it out.

Any advice appreciated!
Thanks,
Isa

#include <AccelStepper.h>




// Define two steppers and the pins they will use
AccelStepper stepper1(1, 9, 8);
AccelStepper stepper2(1, 7, 6);


// Define a variable to record WHEN the instruction arrived.
unsigned long instructionTime = 0;


void setup() {
   
   Serial.begin(9600);
   stepper1.setMaxSpeed(8000);
   stepper2.setMaxSpeed(8000);
   stepper1.setAcceleration(100000000);
   stepper2.setAcceleration(100000000);
}




void loop()
{
    if (Serial.available() > 0)
    {
      char letter =  Serial.read();
      if (letter=='1')
      {
 
         stepper1.move(-6400);
          stepper1.setSpeed(3350);


         stepper2.move(6400);
         stepper2.setSpeed(3350);


         instructionTime = millis();
      }
      else if (letter=='2')
      {
       
         stepper1.move(-6400);
          stepper1.setSpeed(3350);


         stepper2.move(6400);
         stepper2.setSpeed(3350);


         instructionTime = millis()+500;
      }


  // Run if EITHER stepper1 OR stepper2 has some distance left to go.
    if (stepper1.distanceToGo() != 0 || stepper2.distanceToGo() != 0)
    {
      // run stepper1 regardless of the time
      stepper1.runSpeedToPosition();


      // but only run stepper2 if the time is more than 500ms from when the
      // instruction was received
      if (millis() >= instructionTime)
      {
        stepper2.runSpeedToPosition();
       }
  
if (stepper1.distanceToGo() == 0 && stepper2.distanceToGo() == 0)      
 
    {
       Serial.print('X');
     }  
   }
 }

}

As far as I understand it the two lines

stepper1.runSpeedToPosition();

and

stepper2.runSpeedToPosition();

should NOT be where they are. They should both be at the bottom of loop() and not within any IF statements so they are called at every iteration of loop() regardless of anything else that goes on.

Try that first.

...R

Hi Robin,
thanks for your reply!I know the commands are placed in a rather unusual position but the code works fine as it is and the way I placed the runSpeedToPosition commands is due to the delay I need to induce for the else if (letter=='2') .The motors run perfectly fine but I just cant figure out how to make them go the way I want them to go as described above...

francis24:
the code works fine as it is

I just cant figure out how to make them go the way I want

Perhaps there is an inconsistency between these two statements.

My guess is that you need to move the stepper.runToPosition()s. If that stops your existing system working then fix that first (with the runToPosition() in its new place). Then tackle your new problem.

...R