Simple code to stop a motor for 5 seconds..

Kumalix:
can you check my "real" sketch below.

No program.

...R

Sry forgot it. i was excited fixing the problem my self ;D

tnx again.. I hope i can make this work at the end.
Making an CNC machine with 2 motos and CNC rails :cold_sweat:

Hi Robin,

What i found out while implementing your code into the my, is that i need the motor to get back to position(0) zero (start position).

The reason for this is that i’m using your code in a while() statement.
And when the motor gets back to position (0) i need to get out (break;) the while().
I was thinking to use a boolean to mark the and of the cicle and then break out of the while().

I tried doing it like below. but seems it doesn’t work.
Hope you can help me adding one more state (position (0)) to the code below.

thanks a lot

Piece of code a tried with:

     while (crashPin1State == 0)
{ stepperA.currentPosition() == 0;
                digitalWrite(enableA,LOW);
                currentMillis = millis();
            
                if (motorState == WAITING) {
                    stepperA.moveTo(4500); // set to 4500
                    motorState = FIRSTRUN;
                    Serial.println("first run");
                }
            
                if (motorState == BETWEENRUNS) {
                    if (currentMillis - endFirstRunMillis >= interval)
                    {
                      Serial.println("second run");
                        motorState = SECONDRUN;
                        stepperA.moveTo(9000); // set to 9000
                    }
                }
            
                if (stepperA.distanceToGo() == 0 ) {
                    if (motorState == FIRSTRUN) {
                        endFirstRunMillis = currentMillis;
                        motorState = BETWEENRUNS;
                        Serial.println("between runs");
                    }
                    if (motorState == SECONDRUN) {
                        motorState = FINISHED;
                        stepperA.moveTo(0);
                        if (stepperA.currentPosition() == 0 ) {
                         getout = true; 
                        }
                        Serial.println("finished");
                    }
                }
                stepperA.run();
                
                //Serial.println("In the  While");
                //Serial.println(getout);
                  // Serial.println("in the while"); 
                  // delay (1000);
                 //  break;
                 homePin2State = analogRead(A0);
                // Serial.println(homePin2State);
                 if (getout == true and digitalRead(crashPin1) == 1)
            {//Serial.println(homePin2State);
              break;
            }        
} //while break

This is the full code:

//DRV8825 VREF voltage NEMA 17= 0,65V can be 0.8V
//DRV8825 VREF voltage NEMA 23= 0,6AV can be 0.6V
#include <AccelStepper.h>

//NEMA 17 (Actuator) 
const int stepsPerRevolutionA = 200;
AccelStepper stepperA(AccelStepper::DRIVER, 3,4);      // (DRIVER, Step, Directon)
int enableA = 2;

//NEMA 23 (Linear motion) 
const int stepsPerRevolutionL = 200;
AccelStepper stepperL(AccelStepper::DRIVER, 7,8);      // (DRIVER, Step, Directon)
int enableL = 11;
//_____________________________________________________________
unsigned long currentMillis;
unsigned long endFirstRunMillis;
const long interval = 2000;
enum motorStateENUM {
    WAITING,
    FIRSTRUN,
    BETWEENRUNS,
    SECONDRUN,
    FINISHED,
};
motorStateENUM motorState = WAITING;
//_______________________________________________________________
//ACTUATOR
//Actuator startloop switch
const int crashPin1 = 6;
int crashPin1State;
int crashPin1StateA;
//Actuator Home position and check position for future Stapper 2
int homePin2State = analogRead(A0);

//STARTBUTTON
const int startButton = 9;
int startButtonState = 0; //1 = open->not pressed
//STRATBUTTON TOGGLE
//int state = HIGH;      // the current state of the output pin
int startButtonReading;           // the current reading from the input pin
int previouStartButtonReading = 0;    // the previous reading from the input pin
boolean getout = false;

void setup() {//================================================================
    stepperA.setMaxSpeed(4000);
    stepperA.setAcceleration(4000);
    pinMode(crashPin1, INPUT);
    pinMode(startButton, INPUT_PULLUP);
    pinMode(enableA, OUTPUT);
    pinMode(enableL, OUTPUT);
    digitalWrite(enableA,HIGH);
    digitalWrite(enableL,HIGH);
                                          Serial.begin(9600);
///////////////////////////ACTUATOR WHILE\\\\\\\\\\\\\\\\\\    
      int homePin2State = analogRead(A0);
      while(homePin2State < 500  ) 
      {homePin2State = analogRead(A0);
      digitalWrite(enableA,LOW);
      stepperA.setSpeed(-1500);
      stepperA.runSpeed();
      stepperA.setCurrentPosition(0);
                                        //  Serial.print("Home switch state = ");Serial.print(homePin2State);Serial.println(" - in the WHILE loop");
      if( homePin2State >= 500) 
          {stepperA.setCurrentPosition(0);
          digitalWrite(enableA,HIGH);
          break;}
       }
///////////////////////////////////////////////////////////   
}

void loop() {//=========================================================
     crashPin1State = digitalRead(crashPin1);
                                        //Serial.println(crashPin1State);
////////////////////////////////////////////////////////////////////////
      startButtonReading = digitalRead(startButton);
              if (startButtonReading == 1 && previouStartButtonReading == 0)
               {if (startButtonState == 1)
                 startButtonState = 0;
                 else
                 startButtonState = 1;}
/////////////////////////////////////////////////////////////////////////
      crashPin1State = digitalRead(crashPin1);homePin2State = analogRead(A0);

            if(startButtonState == 0 && homePin2State > 500 && crashPin1State == 1)
            {
            digitalWrite(enableL,LOW);
            stepperL.setMaxSpeed(50);
            stepperL.setAcceleration(200);
            stepperL.move(800);
                                        // Serial.print(" Start button state = ");Serial.println(startButtonState);
            stepperL.run();    
            }  
                                        //Serial.print(" Start button state = ");Serial.print(startButtonState);
                                        //Serial.print(" | Home switch state = ");Serial.println(homePin2State);
       crashPin1State = digitalRead(crashPin1);
      if (startButtonState == 0)
{                                      //  Serial.print(getout);Serial.print(" Crash Pin1 state voor While = ");Serial.print(crashPin1State);
     while (crashPin1State == 0)
{ stepperA.currentPosition() == 0;
                digitalWrite(enableA,LOW);
                currentMillis = millis();
            
                if (motorState == WAITING) {
                    stepperA.moveTo(4500); // set to 4500
                    motorState = FIRSTRUN;
                    Serial.println("first run");
                }
            
                if (motorState == BETWEENRUNS) {
                    if (currentMillis - endFirstRunMillis >= interval)
                    {
                      Serial.println("second run");
                        motorState = SECONDRUN;
                        stepperA.moveTo(9000); // set to 9000
                    }
                }
            
                if (stepperA.distanceToGo() == 0 ) {
                    if (motorState == FIRSTRUN) {
                        endFirstRunMillis = currentMillis;
                        motorState = BETWEENRUNS;
                        Serial.println("between runs");
                    }
                    if (motorState == SECONDRUN) {
                        motorState = FINISHED;
                        stepperA.moveTo(0);
                        if (stepperA.currentPosition() == 0 ) {
                         getout = true; 
                        }
                        Serial.println("finished");
                    }
                }
                stepperA.run();
                
                //Serial.println("In the  While");
                //Serial.println(getout);
                  // Serial.println("in the while"); 
                 homePin2State = analogRead(A0);
                // Serial.println(homePin2State);
                 if (getout == true and digitalRead(crashPin1) == 1)
            {//Serial.println(homePin2State);
              break;
            }        
} //while break
getout == false;
    Serial.print(getout);
          digitalWrite(enableA,HIGH);
    Serial.println("uit the while");  

    


       
}
        








previouStartButtonReading = startButtonReading;
}

Kumalix:
What i found out while implementing your code into the my, is that i need the motor to get back to position(0) zero (start position).

Sounds like you need to add another state that goes in before FINISHED - maybe call it SECONDRUNDONE and then add another chunk similar to this - but modified to make it go back to zero

if (motorState == BETWEENRUNS) {
                    if (currentMillis - endFirstRunMillis >= interval)
                    {
                      Serial.println("second run");
                        motorState = SECONDRUN;
                        stepperA.moveTo(9000); // set to 9000
                    }
                }

And PLEASE before you post code again use the AutoFormat tool to lay it out neatly. The way it is now is almost impossible to read.

Finally (for now) if this was my code I would not use WHILE, I would use IF and allow loop() to do the repetition. WHILE blocks until it completes.

...R

After studying and testing for 3 hours and implementing your last post i have successfully added a 3 state 8)
it works smoothly!!!
Thank you for the help.

Finally (for now) if this was my code I would not use WHILE, I would use IF and allow loop() to do the repetition. WHILE blocks until it completes.

I have used While() because when crashpin1 is pressed it should run your code in 1 cycle.
I have tried using the if () in stead of while() but de crashpin1 gets unpressed in the FIRSTRUN of the cycle and then the cycle stops. This is not how it should work.

i Think if i would use the if() it would be nesessery to make a momentery switch out of the crashpin1.
and i don’t know yet how to do that :confused:

If you have a suggestion i would love to hear it. But for now i’s stuck with the while() and a boolean “getout” :slightly_frowning_face:

thanks for help so far! it really helpt a lot!

Full code but where i have to implement the get out boolean:

//DRV8825 VREF voltage NEMA 17= 0,65V can be 0.8V
//DRV8825 VREF voltage NEMA 23= 0,6AV can be 0.6V
#include <AccelStepper.h>

//NEMA 17 (Actuator)
const int stepsPerRevolutionA = 200;
AccelStepper stepperA(AccelStepper::DRIVER, 3, 4);     // (DRIVER, Step, Directon)
int enableA = 2;

//NEMA 23 (Linear motion)
const int stepsPerRevolutionL = 200;
AccelStepper stepperL(AccelStepper::DRIVER, 7, 8);     // (DRIVER, Step, Directon)
int enableL = 11;
//_____________________________________________________________
unsigned long currentMillis;
unsigned long endFirstRunMillis;
const long interval = 2000;
enum motorStateENUM {
  WAITING,
  FIRSTRUN,
  BETWEENRUNS,
  SECONDRUN,
  SECONDRUNDONE,
  THIRDRUN,
  FINISHED,
};
motorStateENUM motorState = WAITING;
//_______________________________________________________________
//ACTUATOR
//Actuator startloop switch
const int crashPin1 = 6;
int crashPin1State;
//Actuator Home position and check position for future Stapper 2
int homePin2State = analogRead(A0);

//STARTBUTTON
const int startButton = 9;
int startButtonState = 0; //1 = open->not pressed
//STRATBUTTON TOGGLE
//int state = HIGH;      // the current state of the output pin
int startButtonReading;           // the current reading from the input pin
int previouStartButtonReading = 0;    // the previous reading from the input pin
boolean getout = false;

void setup() {//================================================================
  stepperA.setMaxSpeed(4000);
  stepperA.setAcceleration(4000);
  pinMode(crashPin1, INPUT);
  pinMode(startButton, INPUT_PULLUP);
  pinMode(enableA, OUTPUT);
  pinMode(enableL, OUTPUT);
  digitalWrite(enableA, HIGH);
  digitalWrite(enableL, HIGH);
  Serial.begin(9600);
  ///////////////////////////ACTUATOR WHILE\\\\\\\\\\\\\\\\\\    
  int homePin2State = analogRead(A0);
  while (homePin2State < 500  )
  { homePin2State = analogRead(A0);
    digitalWrite(enableA, LOW);
    stepperA.setSpeed(-1500);
    stepperA.runSpeed();
    stepperA.setCurrentPosition(0);
    //  Serial.print("Home switch state = ");Serial.print(homePin2State);Serial.println(" - in the WHILE loop");
    if ( homePin2State >= 500)
    { stepperA.setCurrentPosition(0);
      digitalWrite(enableA, HIGH);
      break;
    }
  }
  ///////////////////////////////////////////////////////////
}

void loop() {//=========================================================
  crashPin1State = digitalRead(crashPin1);
  //Serial.println(crashPin1State);
  ////////////////////////////////////////////////////////////////////////
  startButtonReading = digitalRead(startButton);
  if (startButtonReading == 1 && previouStartButtonReading == 0)
  { if (startButtonState == 1)
      startButtonState = 0;
    else
      startButtonState = 1;
  }
  /////////////////////////////////////////////////////////////////////////
  crashPin1State = digitalRead(crashPin1); homePin2State = analogRead(A0);

  if (startButtonState == 0 && homePin2State > 500 && crashPin1State == 1)
  {
    digitalWrite(enableL, LOW);
    stepperL.setMaxSpeed(50);
    stepperL.setAcceleration(200);
    stepperL.move(800);
    // Serial.print(" Start button state = ");Serial.println(startButtonState);
    stepperL.run();
  }
  //Serial.print(" Start button state = ");Serial.print(startButtonState);
  //Serial.print(" | Home switch state = ");Serial.println(homePin2State);
  crashPin1State = digitalRead(crashPin1);
  if (startButtonState == 0)
  { //  Serial.print(getout);Serial.print(" Crash Pin1 state voor While = ");Serial.print(crashPin1State);
    while (crashPin1State == 0)
    {
      digitalWrite(enableA, LOW);
      currentMillis = millis();

      if (motorState == WAITING) {
        stepperA.moveTo(4500); // set to 4500
        motorState = FIRSTRUN;
        Serial.println("first run");
      }

      if (motorState == BETWEENRUNS) {
        if (currentMillis - endFirstRunMillis >= interval)
        {
          Serial.println("second run");
          stepperA.moveTo(9000); // set to 9000
          motorState = SECONDRUN;
        }
      }

      if (motorState == SECONDRUNDONE) {
        Serial.println("second run done");
        stepperA.moveTo(0);
        motorState = THIRDRUN;
      }

      if (stepperA.distanceToGo() == 0 ) {
        if (motorState == FIRSTRUN) {
          endFirstRunMillis = currentMillis;
          motorState = BETWEENRUNS;
          Serial.println("between runs");
        }

        if (motorState == SECONDRUN) {
          motorState = SECONDRUNDONE;
          Serial.println("at the end");
        }

        if (motorState == THIRDRUN) {
          motorState = FINISHED;
          Serial.println("finished");
        }
      }

      stepperA.run();


    } //while break
  }
  //getout == false;
  Serial.print(getout);
  digitalWrite(enableA, HIGH);
  Serial.println("uit the while");




  previouStartButtonReading = startButtonReading;
}

P.S. Sry for the lack of AutoFormat.

Kumalix:
After studying and testing for 3 hours and implementing your last post i have successfully added a 3 state 8)
it works smoothly!!!

Nice feeling when you get there.

I have used While() because when crashpin1 is pressed it should run your code in 1 cycle.
I have tried using the if () in stead of while() but de crashpin1 gets unpressed in the FIRSTRUN of the cycle and then the cycle stops. This is not how it should work.

Maybe pressing the button should set another (unrelated) state that then lets the program know that it should keep doing something. You could consider a separate ENUM or just a variable (perhaps called crashPinPushed) that can be true or false.

The ENUM concept is great when there is a succession of states but may be overkill if there are only two. However it can add clarity.

...R

I don't really know how to begin building this variabele. Think that ENUM is a little bit overkill.

Could you help me get on the way with this variabele?

It should read the crashpin1state and increment to boolean?? crashPinPushed.
Then with other piece of code it should toggle between TRUE and FALSE, right?
Just like a did with the startbutton:

startButtonReading = digitalRead(startButton);
if (startButtonReading == 1 && previouStartButtonReading == 0)
{ if (startButtonState == 1)
startButtonState = 0;
else
startButtonState = 1;
}

Does this make sense?

One way to get out of the WHILE is to add a line after FINISHED - like this

      if (motorState == THIRDRUN) {
          motorState = FINISHED;
          Serial.println("finished");
          crashPin1State = 1;    //   <---------NEW
        }

However what I am really thinking of is something like this

crashPin1State = digitalRead(crashPin1);
if (crashPin1State == 0) {
   crashButtonPressed = true;
}

Note that the crash pin can’t set it back to false.

Then, instead of

while (crashPin1State == 0)

have

if (crashButtonPressed == true)

And after FINISHED have

      if (motorState == THIRDRUN) {
          motorState = FINISHED;
          Serial.println("finished");
          crashButtonPressed = false;
        }

Of course I cannot be certain that what I am suggesting meets your requirement.

…R

Hi Robin,

Thanks again for the help.

I have made the changes you suggested.
I think also that it’s better without a block in the program.
A still have other switches and movements to make.

Issue is now that when (crashButtonPressed == true) i can hear te StepperA make loud noise while doing the FIRSTRUN, SECONDRUN etc. but it complitelly misses all steps. He’s vibrating and the barely moves.
But like i said i can hear the runs and the pauses by the loud sound of the motor.

With the while() i didn’t have this problem.

You can see the video of the problem here: Dropbox - 2018-03-02 21.56.36.mov - Simplify your life

I wanted to check if a video can be shared like this ;D

Don’t know if you are able to see the video??

The code:

//DRV8825 VREF voltage NEMA 17= 0,65V can be 0.8V
//DRV8825 VREF voltage NEMA 23= 0,6AV can be 0.6V
#include <AccelStepper.h>

//NEMA 17 (Actuator)
const int stepsPerRevolutionA = 200;
AccelStepper stepperA(AccelStepper::DRIVER, 3, 4);     // (DRIVER, Step, Directon)
int enableA = 2;

//NEMA 23 (Linear motion)
const int stepsPerRevolutionL = 200;
AccelStepper stepperL(AccelStepper::DRIVER, 7, 8);     // (DRIVER, Step, Directon)
int enableL = 11;
//_____________________________________________________________
unsigned long currentMillis;
unsigned long endFirstRunMillis;
const long interval = 2000;
enum motorStateENUM {
  WAITING,
  FIRSTRUN,
  BETWEENRUNS,
  SECONDRUN,
  SECONDRUNDONE,
  THIRDRUN,
  FINISHED,
};
motorStateENUM motorState = WAITING;
//_______________________________________________________________
//ACTUATOR
//Actuator startloop switch
const int crashPin1 = 6;
int crashPin1State;
int homePin2State;

//STARTBUTTON
const int startButton = 9;
int startButtonState = 0; //1 = open->not pressed
//STRATBUTTON TOGGLE
int startButtonReading;           // the current reading from the input pin
int previouStartButtonReading = 0;    // the previous reading from the input pin

//LINEAR MOTION MOTOR
const int homepinL = 5;
int homePinStateL;


boolean crashButtonPressed = false;
boolean atStartPosition = false;

void setup() {//================================================================
  stepperA.setMaxSpeed(4000);
  stepperA.setAcceleration(4000);
  pinMode(crashPin1, INPUT);
  pinMode(startButton, INPUT_PULLUP);
  pinMode(homepinL, INPUT_PULLUP);
  pinMode(enableA, OUTPUT);
  pinMode(enableL, OUTPUT);
  digitalWrite(enableA, HIGH);
  digitalWrite(enableL, HIGH);
  Serial.begin(9600);
  delay(200);

  ///////////////////////////GET BACK TO START POSITION\\\\\\\\\\\\\\\\\\\\\\    

  while (atStartPosition == false)
  {
    homePin2State = analogRead(A0);
    homePinStateL = digitalRead(homepinL);

    if (homePin2State < 900 )
    {
      digitalWrite(enableA, LOW);
      stepperA.setSpeed(-1900); //set to -1900
      stepperA.runSpeed();
    }
    if (homePin2State > 400)
    {
      stepperA.setCurrentPosition(0);
    }


    if (homePinStateL == 1 )
    {
      digitalWrite(enableL, LOW);
      stepperL.setMaxSpeed(300);
      stepperL.setAcceleration(300);
      stepperL.moveTo(-2500);
      stepperL.run();
    }
    if (homePinStateL == 0 )
    {
      stepperL.setCurrentPosition(0);
    }

    if (stepperA.currentPosition() == 0 and stepperL.currentPosition() == 0 )
    {
      (atStartPosition = false);
      delay(200);
      digitalWrite(enableA, HIGH);
      digitalWrite(enableL, HIGH);
      break;
    }

  }
  ///////////////////////////MOTORS AT START POSITION////////////////////////////////

}
void loop() {//=========================================================

  crashPin1State = digitalRead(crashPin1);
  if (crashPin1State == 0) {
    crashButtonPressed = true;
  }


  //Serial.println("in the main loop");
  //Serial.println (homePinStateL);
  crashPin1State = digitalRead(crashPin1);
  //Serial.println(crashPin1State);
  ////////////////////////////////////////////////////////////////////////
  startButtonReading = digitalRead(startButton);
  if (startButtonReading == 1 && previouStartButtonReading == 0)
  { if (startButtonState == 1)
      startButtonState = 0;
    else
      startButtonState = 1;
  }
  /////////////////////////////////////////////////////////////////////////
  crashPin1State = digitalRead(crashPin1); homePin2State = analogRead(A0);
  //Serial.println (homePin2State);
  if (startButtonState == 0 && homePin2State > 400 && crashButtonPressed == false)
  {
    digitalWrite(enableL, LOW);
    stepperL.setMaxSpeed(300);
    stepperL.setAcceleration(200);
    stepperL.moveTo(2385); //set to 2380
    stepperL.run();
  }


  if (startButtonState == 0)
  {

    //  Serial.print(getout);Serial.print(" Crash Pin1 state voor While = ");Serial.print(crashPin1State);


    if (crashButtonPressed == true)
    {
      digitalWrite(enableA, LOW);
      currentMillis = millis();

      if (motorState == WAITING) {
        stepperA.moveTo(4500); // set to 4500
        motorState = FIRSTRUN;
        Serial.println("first run");
      }

      if (motorState == BETWEENRUNS) {
        if (currentMillis - endFirstRunMillis >= interval)
        {
          Serial.println("second run");
          stepperA.moveTo(9000); // set to 9000
          motorState = SECONDRUN;
        }
      }

      if (motorState == SECONDRUNDONE) {
        Serial.println("second run done");
        stepperA.moveTo(0);
        motorState = THIRDRUN;
      }

      if (stepperA.distanceToGo() == 0 ) {
        if (motorState == FIRSTRUN) {
          endFirstRunMillis = currentMillis;
          motorState = BETWEENRUNS;
          Serial.println("between runs");
        }

        if (motorState == SECONDRUN) {
          motorState = SECONDRUNDONE;
          Serial.println("at the end");
        }

        if (motorState == THIRDRUN) {
          motorState = FINISHED;
          Serial.println("finished");
          crashButtonPressed = false;
        }
      }

      stepperA.run();

    }
  }
  digitalWrite(enableA, HIGH);


  previouStartButtonReading = startButtonReading;
}

I looked at your video a few times but I can't identify what you want me to see or hear.

You have

 stepperA.setMaxSpeed(4000);
  stepperA.setAcceleration(4000);

both of which seem impossibly high

Did I not mention this before?

Try changing them to 400 and 40 respectively

...R

I thought so too.
and tested it with lower numbers but still same issue. :confused:

Can you identify the exact point in your program code where the noise arises?

Can you strip back the program until you are only left with the stepperA code? Does the problem still exist? If so post the stripped back program.

Have you quadruple checked your wiring?

...R

Hi Robin,

While stripping the code i found the issue.
I was give enableA HIGH and LOW in de main loop.
So the stepper was constantly going on off while trying to step.

Thanks again for pointing me to the right direction!

Hi Robin,

So have made 1 step again towards the end ;D
Again, thank you for that. If it wasn't for you i will still be struggling at the beginning.

I have found out that the first time (after arduino reset) everything work fine.
But when the stepperA does his THIRDRUN and is at position (0).
Than when the crashpin is pressed for the second time (after arduino reset) the stepperA does not move.

I think that this is because of this line in the initial setup: motorStateENUM motorState = WAITING;
The stepper doesn't know that he is in the state WAITING.

Where do you think i should put this line?

Kumalix:
Where do you think i should put this line?

You need to post the latest version of your complete program so I can see the context.

What exactly is the purpose of the crashPin? When does it get used? It's a strange name.

...R

Hi Robin,

It is a strange name i know. Can name it productCrashPin.

But to get it in perspectief see the drowing of the CNC machine that i'm building.

This is how it should work:

When the start button is pressed STEPPERL wil move all 10 pieces of the product towards the crashPin1 (or productCrashPin). When the crashPin1 is pressed by the product, STEPPERL has to stop.
Then STEPPERA wil push 1 piece of the product towards X. Then STEPPERA wil return to it's begin position (moving STEPPERA is four ENUM code in the program).

Then when STEPPERA gets to his begin position STEPPERL wil move other 9 pieces of the product toward
crashpin1. Ahen STEPPERA wil move again... and so the whole cycle have to continue untill all products are moved. When the yellow slider (connected to STEPPERL with a timing belt) hits the EndPin1 (not yet in the program below) STEPPERL have to move all the way back to begin position.

That is the whole cycle and working of the machine.

Now the issue is that after arduino reset when piece nummer 1 hits the crashpin1 STEPPERL stops and STEPPERA does your code. But when the piece nummber 2 hits the crashPin1 STEPPERA doesn't move.

I think that this is because of this line in the initial setup: motorStateENUM motorState = WAITING;
The stepper doesn't know that he is in the state WAITING. And is endlessly in FINISH state.

The program:

//DRV8825 VREF voltage NEMA 17= 0,65V can be 0.8V
//DRV8825 VREF voltage NEMA 23= 0,6AV can be 0.6V
#include <AccelStepper.h>

//NEMA 17 (Actuator)
const int stepsPerRevolutionA = 200;
AccelStepper stepperA(AccelStepper::DRIVER, 3, 4);     // (DRIVER, Step, Directon)
int enableA = 2;

//NEMA 23 (Linear motion)
const int stepsPerRevolutionL = 200;
AccelStepper stepperL(AccelStepper::DRIVER, 7, 8);     // (DRIVER, Step, Directon)
int enableL = 11;
//_____________________________________________________________
unsigned long currentMillis;
unsigned long endFirstRunMillis;
const long interval = 2000;
enum motorStateENUM {
  WAITING,
  FIRSTRUN,
  BETWEENRUNS,
  SECONDRUN,
  SECONDRUNDONE,
  THIRDRUN,
  FINISHED,
};
motorStateENUM motorState = WAITING;
//_______________________________________________________________
//ACTUATOR
//Actuator startloop switch
const int crashPin1 = 6;
int crashPin1State;
int homePin2State;

//STARTBUTTON
const int startButton = 9;
int startButtonState = 0; //1 = open->not pressed
//STRATBUTTON TOGGLE
int startButtonReading;           // the current reading from the input pin
int previouStartButtonReading = 0;    // the previous reading from the input pin

//LINEAR MOTION MOTOR
const int homepinL = 5;
int homePinStateL;


boolean crashButtonPressed = false;
boolean atStartPosition = false;

void setup() {//================================================================
  stepperA.setMaxSpeed(4000);
  stepperA.setAcceleration(4000);
  pinMode(crashPin1, INPUT);
  pinMode(startButton, INPUT_PULLUP);
  pinMode(homepinL, INPUT_PULLUP);
  pinMode(enableA, OUTPUT);
  pinMode(enableL, OUTPUT);
  //digitalWrite(enableA, HIGH);
  digitalWrite(enableL, HIGH);
  Serial.begin(9600);
  delay(200);

  ///////////////////////////STEPPERS GET BACK TO START POSITION\\\\\\\\\\\\\\\\\\\\\\    

  while (atStartPosition == false)
  {
    homePin2State = analogRead(A0);
    homePinStateL = digitalRead(homepinL);

    if (homePin2State < 900 )
    {
      digitalWrite(enableA, LOW);
      stepperA.setSpeed(-1900); //set to -1900
      stepperA.runSpeed();
    }
    if (homePin2State > 400)
    {
      stepperA.setCurrentPosition(0);
    }


    if (homePinStateL == 1 )
    {
      digitalWrite(enableL, LOW);
      stepperL.setMaxSpeed(300);
      stepperL.setAcceleration(300);
      stepperL.moveTo(-2500);
      stepperL.run();
    }
    if (homePinStateL == 0 )
    {
      stepperL.setCurrentPosition(0);
    }

    if (stepperA.currentPosition() == 0 and stepperL.currentPosition() == 0 )
    {
      (atStartPosition = false);
      delay(200);
      //digitalWrite(enableA, HIGH);
      digitalWrite(enableL, HIGH);
      break;
    }

  }
  ///////////////////////////STEPPERS AT START POSITION////////////////////////////////

}
void loop() {//=========================================================

  crashPin1State = digitalRead(crashPin1);
  if (crashPin1State == 0) {
    crashButtonPressed = true;
  }


  //Serial.println("in the main loop");
  //Serial.println (homePinStateL);
  ////////////////////////////////////////////////////////////////////////
  startButtonReading = digitalRead(startButton);
  if (startButtonReading == 1 && previouStartButtonReading == 0)
  { if (startButtonState == 1)
      startButtonState = 0;
    else
      startButtonState = 1;
  }
  /////////////////////////////////////////////////////////////////////////
  homePin2State = analogRead(A0);
  //Serial.println (homePin2State);
  if (startButtonState == 0 && homePin2State > 400 && crashButtonPressed == false)
  {
    digitalWrite(enableL, LOW);
    stepperL.setMaxSpeed(300);
    stepperL.setAcceleration(200);
    stepperL.moveTo(2385); //set to 2380
    stepperL.run();
    motorStateENUM motorState = WAITING;
  }


  if (startButtonState == 0)
  {

    //  Serial.print(getout);Serial.print(" Crash Pin1 state voor While = ");Serial.print(crashPin1State);

    //Serial.println (crashButtonPressed);
    if (crashButtonPressed == true)
    {
      currentMillis = millis();

      if (motorState == WAITING) {
        stepperA.moveTo(4500); // set to 4500
        motorState = FIRSTRUN;
        //Serial.println("first run");
      }

      if (motorState == BETWEENRUNS) {
        if (currentMillis - endFirstRunMillis >= interval)
        {
          //Serial.println("second run");
          stepperA.moveTo(9000); // set to 9000
          motorState = SECONDRUN;
        }
      }

      if (motorState == SECONDRUNDONE) {
        //Serial.println("second run done");
        stepperA.moveTo(0);
        motorState = THIRDRUN;
      }

      if (stepperA.distanceToGo() == 0 ) {
        if (motorState == FIRSTRUN) {
          endFirstRunMillis = currentMillis;
          motorState = BETWEENRUNS;
          //Serial.println("between runs");
        }

        if (motorState == SECONDRUN) {
          motorState = SECONDRUNDONE;
          // Serial.println("at the end");
        }

        if (motorState == THIRDRUN) {
          motorState = FINISHED;

          //Serial.println("finished");
          crashButtonPressed = false;
        }
      }

      stepperA.run();

    }
  }


  previouStartButtonReading = startButtonReading;
}

Kumalix:
When the start button is pressed STEPPERL wil move all 10 pieces of the product towards the crashPin1 (or productCrashPin). When the crashPin1 is pressed by the product, STEPPERL has to stop.
Then STEPPERA wil push 1 piece of the product towards X. Then STEPPERA wil return to it's begin position (moving STEPPERA is four ENUM code in the program).

[...]

Now the issue is that after arduino reset when piece nummer 1 hits the crashpin1

Dealing with things backwards ...

There should be NO question of the Arduino being reset except when a major fault arises and the program stops behaving properly.

And ... now that I understand what you want to achieve (you should have told us in your Original Post) leave me some time to think about it because my first reaction to your description is that you are making it far too complicated.

...R

I'm sry for not telling. In the beginning i didn't know that it matterd.
I just got stuck with the does millis at the beginning.

I think i'm almost there with wath i have so far.
If you can think of something simple and better i would be pleased to hear it and implement it if not getting me back to the beginning.

The hardware as shown in the picture i have al ready built. Exept for that last endPin1.

Thank you for helping me so far.

N

It suddenly popped in my head.
I should change the FINISH state with WAITING state.

And it works now!!! 8) ;D

Good to hear. I have not had time to do any more thinking about it.

...R