user35247: I am working on Arduino code for stepper motor rotations control. I want to rotate a stepper for only 4 rotations.
The code is working fine but that will run only once as I have put that in setup().
I want to know if there will be any reverse current flowing in the circuit that would damage the Arduino or stepper. What if I want to run the stepper in the loop()? I tried but that will force the stepper to run continuously.
How can I run stepper for only 4 rotations and then stop?
I want to run it in loop().

Below is the code I am using:
#include <Stepper.h>

const int stepsPerRevolution = 2100;  
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(110);
  // initialize the serial port:
  Serial.begin(9600);
  for(int i=0;i<25;i++) {
    Serial.println("Anti-clockwise");
    myStepper.step(stepsPerRevolution);
    delay(50);
  }
}


user-1: I can't validate what you are doing in the for statement, but if you want to execute that only 4 times, this sketch will do
#include <Stepper.h>

const int stepsPerRevolution = 2100;
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup()
{
    // set the speed at 60 rpm:
    myStepper.setSpeed(110);
// initialize the serial port:
    Serial.begin(9600);
}

void loop() {
    static int count = 0;

    if(count < 4)
    {
        count++;

        for(int i=0; i<25; i++) {
            Serial.println("Anti-clockwise");
            myStepper.step(stepsPerRevolution);
            delay(50);
        }
    }
}

count keeps the count of how many times you execute the for loop. It's declared static to keep his value between loop() executions; otherwise, it will be zero on each loop

user30039: In loop add flag to change after what you desired plus a statement to check the flag.
Code below (assuming what is in for() being enough to do 4 revolutions):  
#include <Stepper.h>

const int stepsPerRevolution = 2100;  
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
bool flag = true;

void setup() {
 // set the speed at 60 rpm:
 myStepper.setSpeed(110);
 // initialize the serial port:
 Serial.begin(9600);     
}  

void loop {  
 if (flag) {  
   for(int i=0;i<25;i++) {
     Serial.println("Anti-clockwise");
     myStepper.step(stepsPerRevolution);
     delay(50);
   }
   flag = false;
 }
}

This solution gives you a flexibility to add a pushbutton to change flag and do another 4 revolutions.

user35247: I still wonder how code which I have posted earlier worked but no worries code below is working as expected.
int dirpin_T1 = 7;
int dirH_T1 = 8;
int steppin_T1 = 9;
void setup() {
  pinMode(dirpin_T1, OUTPUT);
  pinMode(dirH_T1, OUTPUT);
  pinMode(steppin_T1, OUTPUT);
}

void loop() {
  static int count = 0;
  if(count < 4) {
    count++;
    digitalWrite(dirpin_T1, LOW); // Set the direction.
    digitalWrite(dirH_T1, LOW);
    delay(1000);
    for (int i = 0; i<3193; i++) {
      // Iterate for 4000 microsteps.(32767)
      digitalWrite(steppin_T1, LOW);
      // This LOW to HIGH change is what creates the
      digitalWrite(steppin_T1, HIGH);
      // "Rising Edge" so the easydriver knows to when to step.
      delayMicroseconds(500);
    }
  }
}


35247: <p>I am working on Arduino code for stepper motor rotations control. I want to rotate a stepper for only 4 rotations.</p>

<p>The code is working fine but that will run only once as I have put that in <code>setup()</code>.</p>

<p>I want to know if there will be any reverse current flowing in the circuit that would damage the Arduino or stepper. What if I want to run the stepper in the <code>loop()</code>? I tried but that will force the stepper to run continuously.</p>

<p>How can I run stepper for only 4 rotations and then stop?</p>

<p>I want to run it in <code>loop()</code>.</p>

<p><a href="https://i.stack.imgur.com/OoHiM.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OoHiM.jpg" alt="arduino-stepper wiring"></a></p>

<p>Below is the code I am using:</p>

<pre><code>#include &lt;Stepper.h&gt;

const int stepsPerRevolution = 2100;  
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(110);
  // initialize the serial port:
  Serial.begin(9600);
  for(int i=0;i&lt;25;i++) {
    Serial.println("Anti-clockwise");
    myStepper.step(stepsPerRevolution);
    delay(50);
  }
}
</code></pre>
 -1: <p>I can't validate what you are doing in the <code>for</code> statement, but if you want to execute that only 4 times, this sketch will do</p>

<pre><code>#include &lt;Stepper.h&gt;

const int stepsPerRevolution = 2100;
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup()
{
    // set the speed at 60 rpm:
    myStepper.setSpeed(110);
// initialize the serial port:
    Serial.begin(9600);
}

void loop() {
    static int count = 0;

    if(count &lt; 4)
    {
        count++;

        for(int i=0; i&lt;25; i++) {
            Serial.println("Anti-clockwise");
            myStepper.step(stepsPerRevolution);
            delay(50);
        }
    }
}
</code></pre>

<p><code>count</code> keeps the count of how many times you execute the <code>for</code> loop. It's declared <code>static</code> to keep his value between <code>loop()</code> executions; otherwise, it will be zero on each <code>loop</code></p>
 30039: <p>In loop add flag to change after what you desired plus a statement to check the flag.<br>
Code below (assuming what is in for() being enough to do 4 revolutions):  </p>

<pre><code>#include &lt;Stepper.h&gt;

const int stepsPerRevolution = 2100;  
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
bool flag = true;

void setup() {
 // set the speed at 60 rpm:
 myStepper.setSpeed(110);
 // initialize the serial port:
 Serial.begin(9600);     
}  

void loop {  
 if (flag) {  
   for(int i=0;i&lt;25;i++) {
     Serial.println("Anti-clockwise");
     myStepper.step(stepsPerRevolution);
     delay(50);
   }
   flag = false;
 }
}
</code></pre>

<p>This solution gives you a flexibility to add a pushbutton to change flag and do another 4 revolutions.</p>
 35247: <p>I still wonder how code which I have posted earlier worked but no worries code below is working as expected.</p>

<pre><code>int dirpin_T1 = 7;
int dirH_T1 = 8;
int steppin_T1 = 9;
void setup() {
  pinMode(dirpin_T1, OUTPUT);
  pinMode(dirH_T1, OUTPUT);
  pinMode(steppin_T1, OUTPUT);
}

void loop() {
  static int count = 0;
  if(count &lt; 4) {
    count++;
    digitalWrite(dirpin_T1, LOW); // Set the direction.
    digitalWrite(dirH_T1, LOW);
    delay(1000);
    for (int i = 0; i&lt;3193; i++) {
      // Iterate for 4000 microsteps.(32767)
      digitalWrite(steppin_T1, LOW);
      // This LOW to HIGH change is what creates the
      digitalWrite(steppin_T1, HIGH);
      // "Rising Edge" so the easydriver knows to when to step.
      delayMicroseconds(500);
    }
  }
}
</code></pre>
