Repeating a function a precise number of times.

I trying to move my stepper forward and then backward a specific number of times for my slider. I can get it to ge forward a specific number of times.

How do I get it to go backwards a specific number of times?

I’m using an uno Rev.3 with this motor

http://www.adafruit.com/images/970x728/x324-03.jpg.pagespeed.ic.V85B9JsZqw.webp

and this motorshield -

http://www.nexuscyber.com/content/images/thumbs/0000232_motor-shield-for-arduino-uno.jpeg

Here’s my code

#include <AFMotor.h>
 
 
AF_Stepper motor(200, 2);

int myCounter = 0; 
 
void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");
 
  motor.setSpeed(5);  // 40 rpm   
}

void loop() {
  if(myCounter<5) {
    motor.step(25, FORWARD, INTERLEAVE); //1
  motor.release();
  delay(3000);
    myCounter = myCounter + 1;
  }

}

Thanks…Antonio

A for loop would seem to be the obvious solution.

for (int x = 0; x < 10; x++)
{
  //code to be executed 10 times
}
  motor.release();

Why? This lets the motor freewheel. You have no clue how many steps it takes while released, so you'll never get it back to it's original position.

Thanks

Here’s my code with your changes and it kept on repeating the same thing without stopping at 10.

#include <AFMotor.h>
 
 
AF_Stepper motor(200, 2);

 
void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");
 
  motor.setSpeed(5);  // 40 rpm   
}

void loop() {
  
for (int x = 0; x < 10; x++)
{
  //code to be executed 10 times
}
 motor.step(25, FORWARD, INTERLEAVE); //1
 motor.release();
 delay(3000);
    }

Looking forward for you advise…Thanks

for (int x = 0; x < 10; x++)
{
  //code to be executed 10 times
}

Didn’t that comment mean anything to you? Like “PUT THE CODE HERE”!

Sorry,

here it is and it does the same thing.

#include <AFMotor.h>
 
 
AF_Stepper motor(200, 2);

 
void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");
 
  motor.setSpeed(30);  // 40 rpm   
}

void loop() {
  
for (int x = 0; x < 10; x++)
{
 motor.step(25, FORWARD, INTERLEAVE); //1
 motor.release();
 delay(1000);//code to be executed 10 times
}

    }

What am I doing wrong?

So, now, every time loop() runs, the stepper moves 25 steps, 10 times. Was there some part of “do this in setup()” that didn’t mean anything?

Why ARE you calling release()?

Was there some part of "do this in setup()" that didn't mean anything?

I don't know what you are trying to say. I guess I missed something in setup.

As of right now the stepper moves 25 steps for ever, not 10 times.

I removed release.

Can you be more specific?

As of right now the stepper moves 25 steps for ever, not 10 times.

Guess what the loop() function does ?
That's right, it loops. When it has finished it runs again, then again, then again......

If you really want the motor to move 10 times then put the code in setup() so that the for loop only runs once.

Thanks UKHeliBob,

Nothing that I do with the above code works, it just keeps on doing the same thing for ever, if I put the code in setup, it does nothing.

This is the code that I started out with, and it does exactly as it states, I just want to be able to go forward 5 times and then backward the same amount of times, or whatever amount of times designated.

Here’s my code

#include <AFMotor.h>
 
 
AF_Stepper motor(200, 2);

int myCounter = 0; 
 
void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");
 
  motor.setSpeed(5);  // 40 rpm   
}

void loop() {
  if(myCounter<5) {
    motor.step(25, FORWARD, INTERLEAVE); //1
    delay(3000);
    myCounter = myCounter + 1;
  }

}

I tried this, but it alternates from forward, to backward, a total of 10 times, but I want it to move forward 5 times and then backward 5 times, so it’s almost there.

#include <AFMotor.h>
 
 
AF_Stepper motor(200, 2);

int myCounter = 0; 
 
void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");
 
  motor.setSpeed(5);  // 40 rpm   
}

void loop() {
  if(myCounter<5) {
    motor.step(25, FORWARD, INTERLEAVE); //1
  motor.release();
  delay(3000);
  
  motor.step(25, BACKWARD, INTERLEAVE); //1


  delay(3000);
    myCounter = myCounter + 1;
  }

}

I guess I need to separate the forward function from the backward?

Thanks…Antonio

Dude.

Code in setup() runs once.

Code in loop() gets run over and over for as long as the chip is switched on.

Try this:

#include <AFMotor.h>

AF_Stepper motor(200, 2);

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");

  motor.setSpeed(5);  // 40 rpm

  for(int i = 0; i<5; i++) {
    motor.step(25, FORWARD, INTERLEAVE); //1
    delay(3000);
  }
  motor.release();

  for(int i = 0; i<5; i++) {
    motor.step(25, BACKWARD, INTERLEAVE); //1
    delay(3000);
  }
  motor.release();

}

void loop() {
}

I just want to be able to go forward 5 times and then backward the same amount of times, or whatever amount of times designated.

Now you have moved the goalposts, or at least added to the requirement.

For clarification, do you want the forward, backwards movements to occur as soon as power is applied to the Arduino (or it is reset) or is something going to trigger the movement, perhaps a button press or other user input ? If so, should the motor movement happen the next time the input is received or should further input be ignored ?

I have a button that gives the arduino power.

Right now I'm using my slider for video, so it moves from one side of the slider to the other continuously. If I turn it off, I can move my slider to the starting position, push the power button and the sketch starts over. This way, I don't have to push the reset button and hold it as I reposition the slider.

Now I don't care which of the sketches I use as long as they do what I want it to do.

So the only thing that I need help with in the latest sketch, is separating the forward function and the backward so the slider moves forward five times stop, and then move backward five times stop and start over, that's it.

Thanks...........Antonio

Have you tried the code in reply #10 ?

Thanks UKHeliBob,

For some reason I didn't see that post, I tried it and it works perfectly and thanks to PaulMurrayCbr.