0

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.

8
  • 1
    Could you please, remove the empty lines, maybe. They just make the code appear longer than it has to be. (Just my opinion) Commented Aug 5, 2019 at 12:06
  • And align the code (make sure { and } blocks are indented correctly, and indent entire code (use ctrl-k after selecting all code). Commented Aug 5, 2019 at 12:16
  • 1
    Does it work, when you provide both steppers with the same signal line (connecting to the same output pin)?
    – chrisl
    Commented Aug 5, 2019 at 12:50
  • @Duck Dodgers, Michel Keijzers In Arduino IDE, I correctly wrote my codes. I intentionally put the empty lines to read easily and also indented correctly in Arduino IDE. Commented Aug 7, 2019 at 8:21
  • 1
    @YangKeeWon, I can imagine that you did it correctly in the Arduino IDE. But the Arduino IDE, only you can see. We cannot. We see the code only in this post. And if the code isn't indented/formatted, then it becomes difficult to parse, especially for someone not familiar with the code. In any case, there is at least one closing bracket missing } for the loop() 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.:) Commented Aug 7, 2019 at 8:34

1 Answer 1

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);
...
1
  • 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. Commented Aug 13, 2019 at 2:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.