Simple code to stop a motor for 5 seconds..

You are unconditionally telling the stepper to move to position 3000 on every pass through loop(). Why?

ok, i got your point.

so made this change with the while(). But the result is still the same :’(

#include <AccelStepper.h>

AccelStepper stepperA(AccelStepper::DRIVER, 3,4);

unsigned long currentMillis;
unsigned long endFirstRunMillis;     
const long interval = 2000;
boolean waitingToStartRun2 = false;   

const int crashPin1 = 6;
int crashPin1State;

//==========
void setup() {
    stepperA.setMaxSpeed(4000);
    stepperA.setAcceleration(4000);
    Serial.begin(9600);
}

//==========
void loop() {
    currentMillis = millis();

   while (waitingToStartRun2 != false)
   {
    //Serial.println("in the while");
                      stepperA.moveTo(3000);
                      stepperA.run();
                      if (stepperA.distanceToGo()==0)
                      {
                        break;
                      }
   }                  
                      if (stepperA.distanceToGo() == 0) {
                          endFirstRunMillis = currentMillis;
                          waitingToStartRun2 = true;
                          Serial.println("first run");
                      }
                  
                      if (currentMillis - endFirstRunMillis >= interval and waitingToStartRun2 == true)
                      {
                        Serial.println("second run");
                          waitingToStartRun2 = false;
                          stepperA.moveTo(6000); 
                      }

    stepperA.run();
}

Try this version. I suspect the earlier version was getting confused about which state it was in.

#include <AccelStepper.h>

AccelStepper stepperA(AccelStepper::DRIVER, 3,4);

unsigned long currentMillis;
unsigned long endFirstRunMillis;     
const long interval = 2000;

enum motorStateENUM {
    WAITING,
    FIRSTRUN,
    BETWEENRUNS,
    SECONDRUN,
    FINISHED,
};

motorStateENUM motorState = WAITING;

//==========
void setup() {
    stepperA.setMaxSpeed(4000);
    stepperA.setAcceleration(4000);
Serial.begin(9600);
}

//==========
void loop() {
    currentMillis = millis();
    
    if (motorState == WAITING) {
        stepperA.moveTo(3000);
        motorState = FIRSTRUN;
        Serial.println("first run");
    }

    if (motorState == BETWEENRUNS) {
        if (currentMillis - endFirstRunMillis >= interval and waitingToStartRun2 == true)
        {
          Serial.println("second run");
            motorState = SECONDRUN;
            stepperA.moveTo(6000);
        }
    }

    if (stepperA.distanceToGo() == 0) {
        if (motorState == FIRSTRUN) {
            endFirstRunMillis = currentMillis;
            motorState = BETWEENRUNS;
        }
        if (motorState == SECONDRUN) {
            motorState = FINISHED;
        }
    }

    stepperA.run();
}

…R

Hi Robin, thanks for helping.

I don't understand but also with this version the result is the same again

Kumalix:
I don’t understand but also with this version the result is the same again

Please describe the symptoms in as much detail as possible.

…R

This line in my program is wrong

if (currentMillis - endFirstRunMillis >= interval and waitingToStartRun2 == true)

it should be

if (currentMillis - endFirstRunMillis >= interval)

I tried this version on my Uno, without a motor attached and it goes through all the stages

// python-build-start
// action, upload
// board, arduino:avr:uno
// port, /dev/ttyACM0
// ide, 1.6.3
// python-build-end

#include <AccelStepper.h>

AccelStepper stepperA(AccelStepper::DRIVER, 3,4);

unsigned long currentMillis;
unsigned long endFirstRunMillis;
const long interval = 2000;

enum motorStateENUM {
    WAITING,
    FIRSTRUN,
    BETWEENRUNS,
    SECONDRUN,
    FINISHED,
};

motorStateENUM motorState = WAITING;

//==========
void setup() {
    stepperA.setMaxSpeed(4000);
    stepperA.setAcceleration(4000);
    Serial.begin(115200);
}

//==========
void loop() {
    currentMillis = millis();

    if (motorState == WAITING) {
        stepperA.moveTo(3000);
        motorState = FIRSTRUN;
        Serial.println("first run");
    }

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

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

    stepperA.run();
}

…R

IT WORKS. Now i have to figure out how this version of your code works.
Have to implement it into my "real" skatch. But i think it should not be a problem.

1000 x thanks to you Robin!!!

Kumalix:
IT WORKS. Now i have to figure out how this version of your code works.

Good to hear.

I reckon the only unusual part of my program is the use of an ENUM. Google C++ ENUM and learn all about it.

If you Serial.print() the ENUM constants you will find that they just represent values 0,1,2,3 etc. But by giving them names it makes it more obvious what is going on.

...R

PS... Just ignore (and delete, if you wish) the comments at the top of my example. They are just instructions for the program I use to compile my code. I forgot to delete them before posting.

Yes, the ENUM is the one that confuses me :confused:
But i'm tying to figure it out.

While i do, can you check my "real" sketch below.

I have implemented your code. But now the motor just keeps spinning.

Do you think i have to look for the awser in the direction of the ENUM or is it more the timing that is off now?

Hoop i'm not taking to much of your time.. :blush:

For anyone else out there this video explains the basics of ENUM’s.

it helpt me to get the basics of it…

Now i know the awnser to my question… and that is that i have to look at the timing not ENUM’s…

And also i fixed the code. Thanks to Robin.

This one works perfectly:

// python-build-start
// action, upload
// board, arduino:avr:uno
// port, /dev/ttyACM0
// ide, 1.6.3
// python-build-end

#include <AccelStepper.h>

AccelStepper stepperA(AccelStepper::DRIVER, 3,4);

unsigned long currentMillis;
unsigned long endFirstRunMillis;
const long interval = 2000;

enum motorStateENUM {
    WAITING,
    FIRSTRUN,
    BETWEENRUNS,
    SECONDRUN,
    FINISHED,
};

motorStateENUM motorState = WAITING;

//==========
void setup() {
    stepperA.setMaxSpeed(4000);
    stepperA.setAcceleration(4000);
    Serial.begin(115200);
}

//==========
void loop() {
    currentMillis = millis();

    if (motorState == WAITING) {
        stepperA.moveTo(3000);
        motorState = FIRSTRUN;
        Serial.println("first run");
    }

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

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

    stepperA.run();
}

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