user3327290
user3327290

Reputation: 5

How to get a fixed number of delays through arduino in a stepper motor?

I have to stop the stepper motor certain number of times (for one complete rotation) with delays as the stopping parameters.Suppose my requirement is to stop the motor 20 number of times so that my delay value should be evenly distributed between this number (20) for complete one rotation.I used a for loop for these stops(20) but i get delys more than 20.My code for arduino is given below where 8000 are the number of steps in one revolution:

    #include <Stepper.h>

    const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
    // for your motor

    // initialize the stepper library on pins 8 through 11:
    Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);

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

   // step one revolution  in one direction:
   void loop() {
   int noi=20;// set the no of images here
   for(int i=0;i<=noi;i++){
    delay(8000/noi);
    }
    Serial.println("clockwise");
    myStepper.step(stepsPerRevolution);

    }

Upvotes: 0

Views: 981

Answers (1)

UncleO
UncleO

Reputation: 8449

Your question is still confusing, but more clear than before.

It looks like you have a stepper motor that drives a turntable. The motor takes 200 steps for one revolution, but it takes 8000 steps to turn the turntable one revolution.

In one sense, all that matters is the number 8000. To make the table pause, you need to divide 8000 into equal pieces, as it looks like you attempted. But you have misplaced a }.

void loop() {
    int noi=20;// set the no of images here
    for(int i=0;i<=noi;i++){
        delay(8000/noi);
    } <<<<<<<<<<<<<<<<<<<<<<<<<<< REMOVE
    Serial.println("clockwise");
    myStepper.step(stepsPerRevolution);

} 

void loop() {
    int noi=20;// set the no of images here
    for(int i=0;i<=noi;i++){
        delay(enough_delay_to_take_image);  // or trigger image here?
        Serial.println("clockwise");
        myStepper.step(8000/noi);
    }   
} 

The only place that stepsPerRevolution = 200 matters is in calculating the speed of the movement, along with myStepper.setSpeed(60);. Do you really wanting the table to move that quickly? It might cause the object to shake too much.

      myStepper.setSpeed(1);

will cause the movements betyween images to take 3 seconds. If that is too slow,

      myStepper.setSpeed(3);

will cause the movements betyween images to take 1 second.

Upvotes: 1

Related Questions