I'm trying to make a stepper motor run "x" number of rotations clockwise and "y" number of rotations counter clockwise.
This is the code I am using:
#define dirPin 2
#define stepPin 3
#define stepsPerRevolution 1600
int pd = 100;
void setup()
{
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop() {
digitalWrite(dirPin, HIGH);
for (int i = 0; i < 20 * stepsPerRevolution; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(pd);
digitalWrite(stepPin, LOW);
delayMicroseconds(pd);
}
delay(1000);
digitalWrite(dirPin, LOW);
for (int i = 0; i < 20 * stepsPerRevolution; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(pd);
digitalWrite(stepPin, LOW);
delayMicroseconds(pd);
}
delay(1000);
}
The line "20 * stepsPerRevolution" appears in the code two times, when the motor is rotating clockwise and when the motor is rotating counter clockwise. If both numbers are anything below 21, the motor behaves normally. But if the numbers are anything above 20, the motor doesn't spin at all.
And if one of the numbers is 20 and the other is anything above 20, the motor doesn't change directions, but spins 20 rotations in one direction, stops, and spins again 20 rotations in the same direction.
The motor I'm using is a 4.2A Nema23 motor, driver is DM556, connected to Arduino Mega.
Maybe someone has idea why this is happening, or can suggest some other code for me.
Thank you.