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
}
}
}
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.
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?