3

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);
  }
}
dda
  • 1,595
  • 1
  • 12
  • 17
Sonali_B
  • 443
  • 4
  • 19

3 Answers3

2

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.

Sonali_B
  • 443
  • 4
  • 19
smajli
  • 472
  • 3
  • 12
2

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);
    }
  }
}
dda
  • 1,595
  • 1
  • 12
  • 17
Sonali_B
  • 443
  • 4
  • 19
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