Repeat same function a specific number of times

Hi,

I've been trying to modify this simple Adafruit Motor Shield sketch, so I can use it with Easy Driver and Arduino Uno Rev. 3, and can't seem to do it.

#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(10);  // 40 rpm

  for(int i = 0; i<310; i++) {
    motor.step(25, FORWARD, INTERLEAVE); // 25 steps, 310 Times = 5 Minutes
    delay(10000);                 
  }
  motor.release();

  for(int i = 0; i<310; i++) {
    motor.step(25, BACKWARD, INTERLEAVE); // 25 steps, 310 Times = 5 Minutes
    delay(10000);
  }
  motor.release();

}

void loop() {
}

I trying to move my stepper motor forward a specific number of steps, a specific number of times and
repeat it backwards, so I can get back to where I started, just like the simple sketch above, but with and EasyDriver

Here's the easydriver sketch that I'm trying to modify, or maybe I should say trying to merge both sketches.

int Distance = 0; // records the number of steps we have taken

void setup () {
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);

}

void loop() {
  digitalWrite(9, HIGH);
  delayMicroseconds(750);
  digitalWrite(9, LOW);
  delayMicroseconds(750);
  Distance = Distance + 1; //record this step to see if we are et the end of our move

  if (Distance == 80)
  {
    if (digitalRead(8) == LOW)
    {
      digitalWrite(8 , LOW);
    }
    else
    {
      digitalWrite(8, LOW);
    }  

    Distance = 0;

    delay(500);

Can anyone help me with this? Thanks.........Antonio

By the way, I was help with this in a previous post, but I'm not sure that it's necessary to give you that link.

I think this will work. I added a variable to record the number of repeats and an associated IF statement.

Note, also, that I created a move1Step() function to de-clutter the code.

int Distance = 0; // records the number of steps we have taken
byte numRepeats = 0;
byte maxRepeats = 5

void setup () {
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);

}

void loop() {
  if (numRepeats < maxRepeats) {
  move1Step();
  Distance = Distance + 1; //record this step to see if we are et the end of our move

  if (Distance == 80)
  {
 if (digitalRead(8) == LOW)
 {
  digitalWrite(8 , LOW);
 }
 else
 {
  digitalWrite(8, LOW);
 } 

 Distance = 0;
 numRepeats ++;

 delay(500);
  }
 }
}

void move1Step() {
  digitalWrite(9, HIGH);
  delayMicroseconds(750);
  digitalWrite(9, LOW);
  delayMicroseconds(750);
}

...R
Stepper Motor Basics
Simple Stepper Code

Thanks Robin2,

This works great.

How do I make it go backwards the same number of times?

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

AntonioLopez:
How do I make it go backwards the same number of times?

I don't have time now and this is a great opportunity for you to think some more about a solution. You have all the building blocks in front of you.

...R

if (digitalRead(8) == LOW)
 {
  digitalWrite(8 , LOW);
 }
 else
 {
  digitalWrite(8, LOW);
 }

? ? ? ?
Is all that not digitalWrite(8 , LOW);?

  • Scotty