Stepper Motor Code

I am trying to run this code only once, but cant't as the code is inside void() loop. I just want it to run once and stop where I wrote "END".
So basically, I want my motor to go 100 steps clockwise then 100 steps anti-clockwise then 200 steps clockwise and again 100 steps anticlockwise . Finally, 300 steps clockwise and stops.

I am using NEMA 17 motor with Arduino UNO and driver A4988.

It would be great if someone can help.

Thanks in advance

const int stepPin = 5; 
const int dirPin = 4; 
 
void setup() {
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
  
}
void loop() {
  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(500); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(500); 
  }
  delay(1000); // One second delay
  
  digitalWrite(dirPin,LOW); //Changes the rotations direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a 1st direction
  for(int x = 0; x < 200; x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(500); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(500); 
  }
  delay(1000); // One second delay
  
  digitalWrite(dirPin,LOW); //Changes the rotations direction to 2nd direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a 1st direction
  for(int x = 0; x < 300; x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(500); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(500); 
  }
  
  delay(1000);

  END
  }

If you replace "END" with

while(true){};

it will put the code in an infinite loop that can only be ended by resetting the Arduino.

The more usual way to deal with code that you only want to run once is to put it in setup() rather than in loop(). It is perfectly OK to have an empty loop() like this

void loop() {
}

If you plan to develop your program to do more stuff then the best way to make something run once is to use a variable that records how many times it has run.

...R
Stepper Motor Basics
Simple Stepper Code

Thank You Robin for your reply.
I tried to run my motor by leaving the loop empty and writing the code inside setup (). But my motor doesn't move at all. However, writing the same code inside the motor runs perfectly but in continuous loop(which I don't want).
I have attached the code herein. Any suggestion is appreciated.

const int stepPin = 5; 
const int dirPin = 4; 
 
void setup() {
  // Sets the two pins as Outputs
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(500); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(500); 
  }
  delay(1000); // One second delay
  
  digitalWrite(dirPin,LOW); //Changes the rotations direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);  
  }
void loop() {
  }

The code in your Reply #2 seems very different from the code in your Original Post.

Try changing the code in your Original Post so it is like this - and note how few changes I made

const int stepPin = 5;
const int dirPin = 4;
 
void setup() {
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);
 
// }    // CHANGE
// void loop() {  // CHANGE
  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000); // One second delay
 
  digitalWrite(dirPin,LOW); //Changes the rotations direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a 1st direction
  for(int x = 0; x < 200; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000); // One second delay
 
  digitalWrite(dirPin,LOW); //Changes the rotations direction to 2nd direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a 1st direction
  for(int x = 0; x < 300; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
 
  delay(1000);

//  END // CHANGE
  }
  
void loop() {  // CHANGE
}   // CHANGE

...R

Hello Robin Thank you for your reply again!!

I tried to run a smaller code to retest what you suggested.
I have attached my original code here .
I wrote the code inside void setup() instead of void loop(). But my motor is not moving at all. When I am writing the same code inside the void loop () the motor is running in infinite loop.

const int stepPin = 5;
const int dirPin = 4;
 
void setup() {
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);
 

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000); // One second delay
 
  digitalWrite(dirPin,LOW); //Changes the rotations direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a 1st direction
  for(int x = 0; x < 200; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000); // One second delay
 
  digitalWrite(dirPin,LOW); //Changes the rotations direction to 2nd direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a 1st direction
  for(int x = 0; x < 300; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
 
  delay(1000);

  }
  
void loop() { 
}

devika9:
I tried to run a smaller code to retest what you suggested.

Let's try to be systematic and methodical. It is by far the fastest way to debug a problem,

In your Original Reply you posted a program that was working - albeit not stopping after one iteration.

In Reply #1 I suggested a solution and in Reply #3 I suggested how that could be implemented with few changes - and, importantly, no change to the central part of the program.

Have you tried the code in Reply #3? What was the result? Let's not go any further until we get that working.

...R

Hello Robin!!

Thank You for your patience!!

Yes, I did try to run the code in reply #3 and my motor is not moving at all.

You are expecting a NEMA 17 motor to go from a standstill to 1000 steps/second instantly.
It can't and it won't.

Hello Mark,

I didn't get you.

Please explain me little more what are you telling here.

Hello Mark,

I think I found what are you talking about.

"High" steps/second is 2000 to 3000, and this will require requires very fast waveforms and fast magnetic field changes, so the stepper driver is critical for high speeds. It is 2000 steps/sec for 200 steps/rev motor. Is this what you meant?

While @MarkT may have a very good point, I have been assuming that whatever code you have in your Original Post does make the motors work? Is that correct? If so the speed of the motor must have been OK. Please test the code in your Original Post again and confirm that it works.

If it does work then I would expect the change I suggested in Reply #3 to work as its essentials are identical.

So the question is, what has not been carried over from the Original Post to Reply #3?

...R

Hello Robin,

I am posting my original code again to eliminate any confusion.

This code runs perfectly fine but in infinite loop( which I don't need).

const int stepPin = 5; 
const int dirPin = 4; 
 
void setup() {
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
  
}
void loop() {
  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(500); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(500); 
  }
  delay(1000); // One second delay
  
  digitalWrite(dirPin,LOW); //Changes the rotations direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a 1st direction
  for(int x = 0; x < 200; x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(500); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(500); 
  }
  delay(1000); // One second delay
  
  digitalWrite(dirPin,LOW); //Changes the rotations direction to 2nd direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a 1st direction
  for(int x = 0; x < 300; x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(500); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(500); 
  }
  
  delay(1000);

   }

I tested the code in reply#3 and motor just sits there doing nothing.

Post the EXACT code that you uploaded based on Reply #3.

...R

Hello Robin,

Here is the exact code uploaded based on reply #3.

const int stepPin = 5;
const int dirPin = 4;
 
void setup() {
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);
 
  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000); // One second delay
 
  digitalWrite(dirPin,LOW); //Changes the rotations direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a 1st direction
  for(int x = 0; x < 200; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000); // One second delay
 
  digitalWrite(dirPin,LOW); //Changes the rotations direction to 2nd direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a 1st direction
  for(int x = 0; x < 300; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);
  }
  
void loop() {  
}

On running this code, my motor doesn't move at all.

I don't understand why it does not work.

Can you describe in detail what happens (i.e. what the motor does) when you run the code in Reply #11.

Also, can you make a backup copy of the code that does not work and then change it back to the way it was before my suggested changes were included. Try to do it by commenting out parts and re-inserting the parts I had commented out so that it is easy to swap between the two versions.

Are you 400% 800% certain that you have posted the code that you have actually tried on your Arduino?

...R

Hello Robin,

Yes, I am 800% sure. All the codes I have posted so far I have tried them first on my motor with arduino.

The code in reply #11, works the exact way it is coded. The motor runs 100 steps in direction #1 and then goes 100 step in direction #2. Then goes 200 steps in direction #1 next it takes 100 steps in direction #2. Lastly, it takes 300 steps in direction #1. Then it starts again to repeat the cycle with 100 steps in direction #1

Now, I am posting here two codes, #1 doesn't work and #2 does works.

#1

const int stepPin = 5;
const int dirPin = 4;
 
void setup() {
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);
 
  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000); // One second delay
 
  digitalWrite(dirPin,LOW); //Changes the rotations direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a 1st direction
  for(int x = 0; x < 200; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000); // One second delay
 
  digitalWrite(dirPin,LOW); //Changes the rotations direction to 2nd direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a 1st direction
  for(int x = 0; x < 300; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);
  }
  
void loop() {  
}

#2

const int stepPin = 5;
const int dirPin = 4;
 
void setup() {
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);
} //change

void loop() { //change
  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000); // One second delay
 
  digitalWrite(dirPin,LOW); //Changes the rotations direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a 1st direction
  for(int x = 0; x < 200; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000); // One second delay
 
  digitalWrite(dirPin,LOW); //Changes the rotations direction to 2nd direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a 1st direction
  for(int x = 0; x < 300; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);
  }
  
// void loop() {  /// change, basically bringing the main code inside loop
//}

#2 Works exactly as I mentioned above. I am running at a voltage supply of 19 volts.
When I am running the code #2 I see current fluctuation between 1.07 A to .14 A. Whereas, there is no current fluctuation while running code #1( which does not work, the motor doesn't moves at all).

I have copied both the codes from Arduino software window & pasted here after uploading as well as running them on my Arduino board.

Hello Robin,

I got this code working the exact way I want by using "while" command.

const int stepPin = 5;
const int dirPin = 4;
 
void setup() {
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);
  digitalWrite(dirPin,LOW); //change
  digitalWrite(stepPin,LOW); // change
  
} //change

void loop() { //change
  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000); // One second delay
 
  digitalWrite(dirPin,LOW); //Changes the rotations direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a 1st direction
  for(int x = 0; x < 200; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000); // One second delay
 
  digitalWrite(dirPin,LOW); //Changes the rotations direction to 2nd direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a 1st direction
  for(int x = 0; x < 301; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
    if (x==300){//change
      while(true)/*Do nothing*/;//change
    } 
    }
  }

  
// void loop() {  /// change, basically bringing the main code inside loop
//}

After running once, either I have to reload the code or press reset button to run the code again which is completely fine for me.

However, I am not sure if I am doing something wrong to my motor or board by using the above code.

I will still try to figure out why my code #1 in previous reply didn't work. I read everywhere that the code should work if it's in setup () instead loop().

I tried your code with LEDs in place of your motors and, as usual, @MarkT was correct back in Reply #7.

I reckon the real problem is that your original program is not working properly and only appears to work because the code is in loop(). When it is taken out of loop() its weakness is exposed.

Rather than delayMicroseconds(500) try delay(100) for a step rate of about 5 per second. If that works then you can try faster speeds.

...R