To stop stepper motor from repeat process in loop

Hi arduino expert,

I using two stepper motor to make the scanner and driver A4988 .My intention as below :

  1. Press tact switch - stepper motor 1 run left then hit limit switch left, motor turn to the right then hit limit switch right, moto turn left back. This moving left and right continuosly with limit switch as input to change direction.
  2. Press tact switch again - stepper motor 1 stop moving or back to initial position. After that, stepper motor 2 moving 1000 step, then stop the stepper motor 2. Program end.

My problem is stepper motor 2 not stop moving after 1000 step but repeat the process in else loop. Need your help and advise from you guys how to stop the stepper motor 2 then end the program.

here I attach my source code.

    // defines pins numbers
    const int stepPin = 5; //step stepper motor 1
    const int dirPin = 6; //direction stepper motor 1
    const int stepPin1 = 8; //step stepper motor 2
    const int dirPin1 =9; //direction stepper motor 2
    
    const byte sw1 = 2; //tact switch
    const byte limitsw = 3; //limit switch as input to change direction

    
    int enableout = 7; //pin enable stepper motor 1
    int enableoutsatu = 10; //pin enable stepper motor 2
    volatile byte state = HIGH;      // the current state of the output pin
    int reading;           // the current reading from the input pin
    int previous = HIGH;    // the previous reading from the input pin

    long time = 0;         // the last time the output pin was toggled
    long debounce = 50;   // the debounce time, increase if the output flickers
     
    void setup() {
      // Sets the two pins as Outputs
      pinMode(stepPin,OUTPUT); 
      pinMode(dirPin,OUTPUT);
      pinMode(stepPin1,OUTPUT); 
      pinMode(dirPin1,OUTPUT);
      pinMode(sw1,INPUT);
      pinMode(limitsw,INPUT);
      pinMode(enableout,OUTPUT);
      pinMode(enableoutsatu,OUTPUT);
      attachInterrupt(digitalPinToInterrupt(sw1), toggle, CHANGE);
    }
     void loop(){
      
     if (state == LOW){
     digitalWrite(enableout,LOW); //output stepper motor 1 enable
     digitalWrite(enableoutsatu,HIGH); //output stepper motor 2 disable   
     digitalWrite(dirPin,LOW); // Enables the motor to move in a particular direction
      // Makes 200 pulses for making one full cycle rotation
      for(int x = 0; digitalRead(limitsw) == HIGH || x < 200 ; x++) {   
        digitalWrite(stepPin,HIGH); 
        delayMicroseconds(500); 
        digitalWrite(stepPin,LOW); 
        delayMicroseconds(500);     
      } 
      delay(100); // One second delay
      
      digitalWrite(dirPin,HIGH); //Changes the rotations direction
      // Makes 400 pulses for making two full cycle rotation
      for(int x = 0; digitalRead(limitsw) == HIGH || x < 200; x++) {
        digitalWrite(stepPin,HIGH); 
        delayMicroseconds(500);
        digitalWrite(stepPin,LOW);
        delayMicroseconds(500);
      } 
      delay(100);        
    } 
   
    else{
      digitalWrite(enableout,HIGH); //disable output stepper motor 1, motor stop
      delay(1000);
      for(int i = 0; i < 3000 ; i++) {  //set total step 3000
      digitalWrite(enableoutsatu,LOW); // enable output stepper motor 2
      digitalWrite(dirPin1,HIGH); // moving to the left
        if( i >= 0 && i<=1000){ // set total step to moving 1000
          digitalWrite(stepPin1,HIGH); 
          delayMicroseconds(1000); 
          digitalWrite(stepPin1,LOW); 
          delayMicroseconds(1000);
            
          }
        else if(i == 1000){  // set if total step more that 1000, stop the stepper motor 2
            digitalWrite(enableoutsatu,HIGH); //disable output stepper motor 2, motor stop
             break; //stop the loop

            }

        }
    }
          
}

    void toggle() {
          reading = digitalRead(sw1);

     if (reading == HIGH && previous == LOW && millis() - time > debounce) {
        if (state == HIGH)
          state = LOW;
       else
         state = HIGH;

      time = millis();    
     }
       previous = reading;
    }

stepper_motor_scanner.ino (3.33 KB)

It is much easier if you include short programs in your Post using the code button </> so it looks like this

    // defines pins numbers
    const int stepPin = 5; //step stepper motor 1
    const int dirPin = 6; //direction stepper motor 1
    const int stepPin1 = 8; //step stepper motor 2
    const int dirPin1 =9; //direction stepper motor 2
    
    const byte sw1 = 2; //tact switch
    const byte limitsw = 3; //limit switch as input to change direction

    
    int enableout = 7; //pin enable stepper motor 1
    int enableoutsatu = 10; //pin enable stepper motor 2
    volatile byte state = HIGH;      // the current state of the output pin
    int reading;           // the current reading from the input pin
    int previous = HIGH;    // the previous reading from the input pin

    long time = 0;         // the last time the output pin was toggled
    long debounce = 50;   // the debounce time, increase if the output flickers
     
    void setup() {
      // Sets the two pins as Outputs
      pinMode(stepPin,OUTPUT); 
      pinMode(dirPin,OUTPUT);
      pinMode(stepPin1,OUTPUT); 
      pinMode(dirPin1,OUTPUT);
      pinMode(sw1,INPUT);
      pinMode(limitsw,INPUT);
      pinMode(enableout,OUTPUT);
      pinMode(enableoutsatu,OUTPUT);
      attachInterrupt(digitalPinToInterrupt(sw1), toggle, CHANGE);
    }
     void loop(){
      
      if (state == LOW){
   
     digitalWrite(enableout,LOW); //output stepper motor 1 enable
     digitalWrite(enableoutsatu,HIGH); //output stepper motor 2 disable   
     digitalWrite(dirPin,LOW); // Enables the motor to move in a particular direction
      // Makes 200 pulses for making one full cycle rotation
      for(int x = 0; digitalRead(limitsw) == HIGH || x < 200 ; x++) {   
        digitalWrite(stepPin,HIGH); 
        delayMicroseconds(500); 
        digitalWrite(stepPin,LOW); 
        delayMicroseconds(500);     
      } 
      delay(100); // One second delay
      
      digitalWrite(dirPin,HIGH); //Changes the rotations direction
      // Makes 400 pulses for making two full cycle rotation
      for(int x = 0; digitalRead(limitsw) == HIGH || x < 200; x++) {
        digitalWrite(stepPin,HIGH); 
        delayMicroseconds(500);
        digitalWrite(stepPin,LOW);
        delayMicroseconds(500);
      } 
      delay(100);        
    }
    
   
    else{
      digitalWrite(enableout,HIGH); //disable output stepper motor 1, motor stop
      delay(1000);
      for(int i = 0; i < 3000 ; i++) {  //set total step 3000
      digitalWrite(enableoutsatu,LOW); // enable output stepper motor 2
      digitalWrite(dirPin1,HIGH); // moving to the left
        if( i >= 0 && i<=1000){ // set total step to moving 1000
          digitalWrite(stepPin1,HIGH); 
          delayMicroseconds(1000); 
          digitalWrite(stepPin1,LOW); 
          delayMicroseconds(1000);
            
          }
        else if(i == 1000){  // set if total step more that 1000, stop the stepper motor 2
            digitalWrite(enableoutsatu,HIGH); //disable output stepper motor 2, motor stop
             break; //stop the loop

            }

        }
    }
          
}

    void toggle() {
          reading = digitalRead(sw1);

     if (reading == HIGH && previous == LOW && millis() - time > debounce) {
        if (state == HIGH)
          state = LOW;
       else
         state = HIGH;

      time = millis();    
     }
       previous = reading;
    }

I can't figure out how your program is intended to work. I assume the poorly named variable called "state" is really the indicator of which direction the motor is moving.

I can't see any attempt in the code to check a button to "interfere" with the normal movement.

I strongly suspect all those delay()s will make your program unresponsive. The second example in this Simple Stepper Code shows how to use non-blocking timing.

...R

HI Robin2,

Thanks for your reply and teach how to put the code.

"state" is for toggle switch function which mean I used push reset button to on/off switch.

I will try your suggestion.

isz4530:
"state" is for toggle switch function which mean I used push reset button to on/off switch.

Using meaningful variable names makes code easier to understand and to maintain.

I can think of no reason to use an interrupt to detect a toggle switch. Just check the state of the switch in each iteration of loop()

...R