Start and stop 3 stepper motor with push button

Hi, regarding with my subject above, I want to ask something about the mistake or problem that exists in my coding below.

Here are the items that I use:
2 28BYJ-48 stepper motor + 2 ULN2003 driver
1 Nema 14 stepper motor + A4988 driver
1 push button + 1 LED

The program or the system I want to make is like a pipe scanner.
The system start when you press push button, and then the LED will start light up and so the Nema 14 will rotate forward and reverse (in defined distance) and after that both 28BYJ-48 will start rotate froward in CW and CCW respectively. I want to make the 28BYJ-48 to step or rotate about 50 degree rotation (I just set it 500 in coding, it might be wrong but its okay). The idea is to make the system keep working with LED is lighten up and Nema 14 rotate and then also 28BYJ-48 rotating until I press the push button again which make it stop working.

So, the problem here is, the system stop working after only ONE loop or cycle.
My question is how to make it keep moving until I press the push button again to make it stop?

/*

#include <AccelStepper.h>
/*-----( Declare Constants and Pin Numbers )-----*/
/*declare define for 28byj-48 stepper motor*/

#define dirPin 2        // Nema 14 direction pin
#define stepPin 3       // Nema 14 step pin

//push button and LED
#define buttonPin 12    // push button
#define ledPin 13       // LED 

/*-----( Declare objects )-----*/
// NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepper2(HALFSTEP, motorPin5, motorPin7, motorPin6, motorPin8);
AccelStepper stepper3(1,stepPin, dirPin);

/*-----( Declare Variables )-----*/
int ledState = LOW;
int buttonState;
int previous = LOW;
long time = 0;
long debounce = 200;

void setup()   /****** SETUP: RUNS ONCE ******/
{
  stepper1.setMaxSpeed(2000.0);
  stepper1.setAcceleration(8000.0);
  stepper1.setSpeed(1000);
   
  stepper2.setMaxSpeed(2000.0);
  stepper2.setAcceleration(8000.0);
  stepper2.setSpeed(1000);
  
  stepper3.setMaxSpeed(1000.0);
  stepper3.setAcceleration(1000.0);
  // Sets the two pins (A4988 driver) as Outputs
  pinMode(stepPin,OUTPUT);       
  pinMode(dirPin,OUTPUT);       

  pinMode(buttonPin, INPUT);    // set push button as input
  pinMode(ledPin, OUTPUT);      // set LED as output

}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  buttonState = digitalRead(buttonPin);  
  if(buttonState == HIGH && previous == LOW && millis() - time > debounce) 
  {
    if(ledState == HIGH)
    {
      ledState = LOW; 
      
      if (stepper1.distanceToGo() == 0 && stepper2.distanceToGo() == 0) 
        {
          stepper1.move(500); // rotate about 50 degree CW
          stepper2.move(-500); // rotate about 50 degree CCW
      
         digitalWrite(dirPin,LOW); //Enables the motor to move the pulley(front view) to the left
         // Makes 1300 pulses for making full six and half cycle rotation (300 mm distance)
        for(int x = 0; x < 1300; x++) {
          digitalWrite(stepPin,HIGH);
          delayMicroseconds(500);
          digitalWrite(stepPin,LOW);
          delayMicroseconds(500);
          }
          delay(200);  

         digitalWrite(dirPin,HIGH); // Enables the motor to move the pulley(front view) to the right
          // Makes 1300 pulses for making full six and half cycle rotation (300 mm distance)
        for(int x = 0; x < 1300; x++) {
           digitalWrite(stepPin,HIGH); 
           delayMicroseconds(500); 
           digitalWrite(stepPin,LOW); 
           delayMicroseconds(500); 
           }           
         }
    } else {
      ledState = HIGH; 
    }
    time = millis();
  }
  digitalWrite(ledPin, ledState);

  stepper1.run();
  stepper2.run();

  previous == buttonState;

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/
//none
//*********( THE END )***********

Do you have a pulldown resistor (10k) from buttonPin to GND?

This type of code will make your program very unresponsive

      for(int x = 0; x < 1300; x++) {
          digitalWrite(stepPin,HIGH);
          delayMicroseconds(500);
          digitalWrite(stepPin,LOW);
          delayMicroseconds(500);
          }
          delay(200);

Nothing can happen until that FOR and that DELAY() complete - so your button won't be detected. Have a look at how millis() is used to manage timing without blocking in Several things at a time. You can use the same technique with micros().

And replace the FOR with IF and allow loop() to do the iteration.

...R

756E6C:
Do you have a pulldown resistor (10k) from buttonPin to GND?

I only use 220 ohm. Before this it working but after some time it seem not functioning well

Robin2:
T
Nothing can happen until that FOR and that DELAY() complete - so your button won't be detected. Have a look at how millis() is used to manage timing without blocking in Several things at a time. You can use the same technique with micros().

And replace the FOR with IF and allow loop() to do the iteration.

...R

I use this one because i want to set the roation step (1300), i found some other Nema 14 or 17 with a4988 coding but they does not show how to set the target.

MatZuhdi36:
I use this one because i want to set the roation step (1300),

You can achieve the same result without using a FOR loop. Have a look at the second example in this Simple Stepper Code

...R

why do use push button with so many for loop and delay functions in main loop?
why not just use selector switch?

Robin2:
You can achieve the same result without using a FOR loop. Have a look at the second example in this Simple Stepper Code

...R

Okay, i understand a little bit about coding in this sample..but i'm still new in this
So my other question are:

  1. push button in simple stepper code is just for momentary..but I want to make it maintained..how?
  2. and again..which part that i can set those 1300 step rotation? in singleStep loop or the other loop?

push button in simple stepper code is just for momentary..but I want to make it maintained..how?

You need to detect when the button becomes pressed not when it is pressed. Look at the StateChangeDetection example in the IDE

MatZuhdi36:

  1. push button in simple stepper code is just for momentary..but I want to make it maintained..how?

As mentioned in Reply #8 you need to detect when the button is pressed and use that to change the value in a variable - perhaps like this pseudo code

if (button is pressed) {
  if (numStepsToGo == 0) {
    numStepsToGo = 1300;
  }
}
  1. and again..which part that i can set those 1300 step rotation? in singleStep loop or the other loop?

then your motor code could be something like

if (numStepsToGo > 0) {
   // code for a single step
   numStepsToGo -= 1;
}

...R