Accelstepper to execute one function and then a different function after.

Hello,

How can I get one stepper motor to execute two different moves based on a timed schedule?
**Only the first move is executed. **

I am writing the code for an automatic bottle/can sorter based on material type. I am using an Arduino mega, an 8 channel switchboard, two stepper motors (28BYJ-48) with ULN2003 drivers, an electromagnet, and 3 different sensors.

I have declared some custom functions for the stepper motors that will bounce the stepper clockwise and then back and stop or counterclockwise and back and stop.

I am trying to have one stepper motor bounce clockwise, stp1bCW(), based on a millis() schedule. However The motor will execute only the first stp1bCW(), but not anything after that. I have tested all of the functions in a separate sketch and they work, but only after the board is reset

I have added some simple outputs to the serial monitor to check if the second condition is even being evaluated, which it is. The motor simply wont make the second move.

I think the problem has to do with re-initializing the pins that drive the stepper because the LEDs on the driver stay lit instead of turning off, but I am rather unexperienced with arduinos and accelstepper.

Turing the switch off and then back on before the second move makes no difference.

Here is my code

#include <AccelStepper.h>

/*------Define a stepper and the pins it will use------*/
AccelStepper  stepper1CW (4,22,26,24,28); // stepper #1 clockwise         digital pins 22,24,26, and 28
AccelStepper  stepper1CCW(4,22,26,24,28); // stepper #1 counter clockwise "
AccelStepper  stepper2CW (4,30,34,32,36); // stepper #2 clockwise         digital pins 30,32,34, and 36
AccelStepper  stepper2CCW(4,30,34,32,36); // stepper #2 counter clockwise "
AccelStepper  stepper3CW (             ); //add third stepper here
AccelStepper  stepper3CCW(             ); 

/*-----relay switches-----*/
#define on  0
#define off 1

/*------Arduino Digital I/O pin number "relayname pin#"------*/
                    
#define stp1    2   //stepper #1
#define stp2    3   //stepper #2
#define smarts  4   //smart sensor
#define emag    5   //electro magnet
#define dumbs   6   //dumb sensor
#define capac   7   //cap sensor
#define stp3    8   //stepper #3 conveyor
#define r8      9

/*------sensor values------*/

int smartsv = analogRead (A0);
int  dumbsv = digitalRead(46);
int  capacv = digitalRead(47);

/*------(declare stepper functions)------*/

  /**stepper #1 functions**/
  void stp1bCW()                    //stepper1 bounce clockwise and return
  {
    stepper1CW.run();   
   
    if (stepper1CW.distanceToGo()==0)
          stepper1CCW.run();
  }

  void stp1bCCW()                   //stepper1 bounce counter clockwise and return
  {
    stepper1CCW.run();   
    
    if (stepper1CCW.distanceToGo()==0)
        {stepper1CW.run();}
  }
  /**stepper #2 functions**/
  void stp2bCW()                     //stepper2 bounce clockwise and return
  {
    stepper2CW.run();  
              
    if (stepper2CW.distanceToGo()==0);
        {stepper2CCW.run();}
  }

  void stp2bCCW()                   //stepper2 bounce counter clockwise and return
  {
    stepper2CCW.run();   
             
    if (stepper2CCW.distanceToGo()==0);
        {stepper2CW.run();}
  } 
   
void setup()
{
/*------pins setup------*/
  Serial.begin(9600);
  pinMode(A0, INPUT_PULLUP);

/*------stepper motor setup------*/
  stepper1CW.setMaxSpeed(100);
  stepper1CW.setAcceleration(75);\
  stepper1CW.moveTo(400);
  stepper1CCW.setMaxSpeed(100);
  stepper1CCW.setAcceleration(75);
  stepper1CCW.moveTo(-400);
  
  stepper2CW.setMaxSpeed(100);
  stepper2CW.setAcceleration(75);\
  stepper2CW.moveTo(400);
  stepper2CCW.setMaxSpeed(100);
  stepper2CCW.setAcceleration(75);
  stepper2CCW.moveTo(-400);

/*------( Initialize Pins so relays are inactive at reset)------*/
  digitalWrite(stp1,   off);
  digitalWrite(stp2,   off);
  digitalWrite(smarts, off);
  digitalWrite(emag,   off);  
  digitalWrite(dumbs,  off);
  digitalWrite(capac,  off);
  digitalWrite(stp3,   off);
  digitalWrite(r8,     off);  
  
  
/*------( THEN set pins as outputs )------*/ 
  pinMode(stp1,   OUTPUT);   
  pinMode(stp2,   OUTPUT);  
  pinMode(smarts, OUTPUT);  
  pinMode(emag,   OUTPUT);    
  pinMode(dumbs,  OUTPUT);   
  pinMode(capac,  OUTPUT);  
  pinMode(stp3,   OUTPUT);  
  pinMode(r8,     OUTPUT);
  delay(1000); //Check that all relays are inactive at Reset

}

void loop()

{
unsigned long t = millis();

  if (t>0 && t<25000)
    digitalWrite(stp1, on);
      else digitalWrite(stp1, off);
    

  if (t>2000 && t<13000)
  {
    stp1bCW();
    Serial.print(1);
    Serial.print('\n');
  }
  if (t>13000 && t<14000)
    AccelStepper disableOutputs();
    
  if (t>15000 && t<25000)
  {
    stp1bCCW();    
    Serial.print(2);
    Serial.print('\n');    
  }


}

Thanks!

ALL (let me repeat that ALL) the calls to stepper.run() should be in loop(), not inside your functions.

Your functions to select the different actions should just set the position the motor is to move to. The AccelStepper library will automatically stop it when it reaches its destination even though stepper.run() continues to be called.

You can use stepper.distanceToGo() if you need to test whether a motor has reached its destination.

...R

Yes, you're simply not allowing the library to do its stuff... You have to ensure run() is always
called in loop and none of your code blocks to prevent that running frequently. So no calls to delay()
anywhere...

Then you can start each movement whenever you want, the library does the rest in the background,
so long as you call run() all the time.