Driving a motor with if condition inside for loop

Hello, everyone. I had write a for loop with if condition to drive the motor and the this code is not working. There are not any error was reported when verify it. Can anyone give me some feedback on the following code?

// Define pin connections & motor's steps per revolution
const int dirPin = 2;
const int stepPin = 3;
const int stepsPerRevolution = 200;
int x = 0;
void setup()
{
	// Declare pins as Outputs
	pinMode(stepPin, OUTPUT);
	pinMode(dirPin, OUTPUT);
}

void loop()
{
	
	// Spin motor slowly
  // if the x smaller than 200, it rotate in clockwise direction
  if(x < stepsPerRevolution)
  {
    for(x = 0; x < stepsPerRevolution; x++)
    {
    //set the motor clockwise  	
    digitalWrite(dirPin, HIGH);
		digitalWrite(stepPin, HIGH);
		delayMicroseconds(200);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(200);
    delay(100); // Wait a second
    }
  }
}

Thank you so much
Steven

All that means is your syntax makes sense to the compiler.

Not a very helpful statement.
What does it do?
What do you want it to do?

You never seem to reset the value for x after you use it in the for loop. This means it will always be equal, not less than stepsPerRevolution when you encounter it in the if statement.

You may want to read this before you proceed:-
how to get the best out of this forum
It tells you the things you need to supply to give us a chance of helping you.

The for loop is using to drive the motor and I want the x count to the stepsPerRevolution (200). I am try to using your suggestion.

It is better to use different named variables for different reasons. Using the same variable, namely x is confusing you.

How can you count the stepsPerRevolution? It is something set by the stepping motor.

Sorry for misunderstanding, I mean I want to count the X to 200 which is the step of the motor!

Sorry that makes no sense to me.
X is used as the for loop index, it is counting nothing.
As I said because you don't reset it then the if statement will always be false after the first movement.

You have not explained what you want this if statement to do?

I want the for loop working before the x reach 200

You are not very good at explaining things, are you using some sort of translation program?

So what is actually happening with your code - you have never said.
What do you want to happen?

That's fine.

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