1

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().

arduino-stepper wiring

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);
  }
}
1

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.

1

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);
    }
  }
}
  • please make sure you reset the arduino everytime you power it on to run the stepper because it won't run the loop again, just four rotations and it will stop. – Sonali_B Nov 29 '17 at 7:49
  • Learn to properly indent your code. Press cmd/ctrl-T in the Arduino IDE regularly to clean up your code. – dda Dec 29 '17 at 14:17
0

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

  • I have used for loop to rotate the stepper four times.If I would remove for loop stepper will keep rotating continuously. The command "myStepper.step(stepsPerRevolution)" will force the stepper until it completes the count of "stepsPerRevolution" and increasing and decreasing the count just affect the speed of stepper that is not what I want. I want four times rotation of stepper and for loop makes it easier. – Sonali_B Nov 27 '17 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.