I'm using Arduino Mega 2560, A4988 motor driver for 5 stepper motors. There are X1, X2, Y, Z, A axis, and if I control Y and Z at once, it works. But when I just change axis from Y&Z to X1&X2, only X1 stepper motor spins. X1&X2 need to move at once(simultaneously) due to they are connected in single bar.
My Arduino IDE code is,
==============================
const int X2step = 5;
const int X2dir = 4;
const int X1step = 3;
const int X1dir = 2;
void setup() {
pinMode(X1step,OUTPUT);
pinMode(X1dir,OUTPUT);
pinMode(X2step,OUTPUT);
pinMode(X2dir,OUTPUT);
}
void loop() {
digitalWrite(X1dir,HIGH);
digitalWrite(X2dir,HIGH);
for(int x = 0; x < 4000; x++) {
digitalWrite(X1step,HIGH);
delayMicroseconds(2000);
digitalWrite(X1step,LOW);
delayMicroseconds(2000);
digitalWrite(X2step,HIGH);
delayMicroseconds(2000);
digitalWrite(X2step,LOW);
delayMicroseconds(2000);
}
delay(1000);
digitalWrite(X1dir,LOW);
digitalWrite(X2dir,LOW);
for(int x = 0; x < 4000; x++) {
digitalWrite(X1step,HIGH);
delayMicroseconds(2000);
digitalWrite(X1step,LOW);
delayMicroseconds(2000);
digitalWrite(X2step,HIGH);
delayMicroseconds(2000);
digitalWrite(X2step,LOW);
delayMicroseconds(2000);
}
exit(0);
=================================================
Any suggestion would be helpful for me. Thanks.
1 Answer 1
You likely have other issues to boot based on what you said about only X1 moving, but first I suggest resolving the bug in your code. It appears to be that you have delays between your digital writes to x1, x2. And set their directions to the same.
At the very least you should be writing to X2 step every time you write to X1 step. If your wiring is identical and the motors are essentially attached mirrored on opposite sides of a shaft, don't forget to set the directions opposite before you start sending steps. (If they are on opposite sides of the shaft, one spinning counterclockwise means the other spins clockwise. You can also play wiring tricks with what coil goes where and how, but identical is simpler.)
digitalWrite(X1dir,HIGH);
digitalWrite(X2dir,LOW);
Or
digitalWrite(X1dir,LOW);
digitalWrite(X2dir,HIGH);
Then step it like
digitalWrite(X1step,HIGH);
digitalWrite(X2step,HIGH);
delayMicroseconds(2000);
digitalWrite(X1step,LOW);
digitalWrite(X2step,LOW);
delayMicroseconds(2000);
...
-
Thank you. I didn't realized the motors had to spin opposite direction. The problem was just simple wiring, but after that your comment helped me a lot. Thanks for your kind answer. Best regards, YK.Yang Kee Won– Yang Kee Won08/13/2019 02:58:10Commented Aug 13, 2019 at 2:58
Explore related questions
See similar questions with these tags.
}
for theloop()
function in your code (which would've been nearly impossible to detect without the code being indented&so on). Sorry to be pedantic about this. Just saying that people can only help you, if you help them see your problem clearly.:)