How to Loop Stepper Motor Ten Times with For Loop

Hello, I'm trying to get a stepper motor with a driver to move back and forth a certain distance ten times when a push button is pressed. After the ten back and forth movements, I need it to stop. I've had experience using Java and I'm familiar with for and while loops but I just can't seem to get a counter to work. Every time I try to incorporate a counter or delay function into the code, the motor just stops moving. I'm also having a lot of trouble with the push button as it does not work the same way as the simple LED code examples on this forum.

Here is the code that works without the push button or for loop: it just goes back and forth repeatedly.

#include <AccelStepper.h>


int Stepper1Pulse = 5;  // Pulse or step pin
int Stepper1Direction = 6;  // Direction pin
int speedpot = A0;   // Potentiometer
int startSwitch = 2;  //StartSwitch Pin
int val = 0;   //pushswitch initial value
int val2 = 0;




int Motor1speed = 0;
int speedmin = 0; //pulses per second
int speedmax = 4000;  //pulses per second
int distance = 20;
AccelStepper step1(1, Stepper1Pulse, Stepper1Direction);








void setup()
{
 // Change these to suit your stepper if you want
 step1.setMaxSpeed(1000);
 step1.setAcceleration(200);
 //step1.moveTo(distance);
}
void loop()
{


 step1.moveTo(100);
 step1.run();
 step1.moveTo(-100);
 step1.run();


}

Here is my code that I thought would work but doesn't:

#include <AccelStepper.h>

int Stepper1Pulse = 5;  // Pulse or step pin
int Stepper1Direction = 6;  // Direction pin
int speedpot = A0;   // Potentiometer

bool fHasLooped = false;

int startSwitch = 2;  //StartSwitch Pin
int val = 0;   //pushswitch initial value
int val2 = 0;

int Motor1speed = 0;
int speedmin = 0; //pulses per second
int speedmax = 100;  //pulses per second

AccelStepper step1(1, Stepper1Pulse, Stepper1Direction);



void setup() {
  // put your setup code here, to run once:

  step1.setMaxSpeed (speedmax);
  //step1.setSpeed(0);
  step1.setAcceleration(90);
  pinMode(Stepper1Pulse, OUTPUT);
  pinMode(Stepper1Direction, OUTPUT);
  digitalWrite(Stepper1Pulse, LOW);
  digitalWrite(Stepper1Direction, LOW);
  step1.moveTo(180);
  step1.run();
  delay(1000);
  step1.moveTo(0);

}

void loop() {
  // put your main code here, to run repeatedly:
  val = digitalRead(startSwitch);  // read input value
  if (val == HIGH || val2 > 0) {
    val2 = 1;
    if ( fHasLooped == false)
    {
      for (int x = 0; x < 10; x++)
      {
        step1.moveTo(100);
        step1.run();
        step1.moveTo(-100);
        step1.run();
      }

    }
    fHasLooped = true;
  }
}

Thanks so much for the help. Sorry if I formatted something incorrectly, I am brand new to arduino and have no experience with this language at all. I understand the concepts of what I have to do but I don't really know the syntax of this language or how to interface the arduino with the counter. Really appreciate everyone's help!

Please use the code tags in your future posts - click the </> button, and paste your code inside the box that opens.
Thanks - Moderator.

Will do. Just reformatted it.

arunmovva:
Here is my code that I thought would work but doesn't:

What does it actually do?

Where does execution go when you press the button? Add some serial prints to find out.

Put your code into code tags. Not the whole post. If we can't compile it, we probably can't help you.
Avoid excessive blank lines as it makes scrolling through the code tedious.
Use CTRL-T in the IDE to format the code.
These guidelines will assure more people will read your post.

A schematic would help us to understand how you have the switch wired.

where do you configure you startSwitch pin as an input and presumably with a pullup (i.e. INPUT_PULLIP).

where does val2 get set back to zero?
whre does fHasLooped get set back to false?

try using Serial.prints

Note Serial.prints for debugging can interfere with your Stepper speed.
Check out my tutorials Multi-tasking in Arduino which has a detailed stepper example and
Arduino Serial I/O for the Real World which covers how to print debugging messages without blocking the rest of your code.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.