# Run 2 pieces of code at the same time.

I am making a homemade printer that draws shapes and I’m trying to make a circle but the stepper motors are not working good. They aren’t as fluid as they should. Here is the code:

``````void loop() {
for (int angle = 0; angle <360; angle ++) {
if (x <0) {
digitalWrite(dirPinX, LOW);
}
if (x >0) {
digitalWrite(dirPinX, HIGH);
}
x = abs(x);
if (y<0) {
digitalWrite(dirPinY, LOW);
digitalWrite(dirPinY2, LOW);
}
if (y>0) {
digitalWrite(dirPinY, HIGH);
digitalWrite(dirPinY2, HIGH);
}
x = abs(x);
y = abs(y)*2;
Serial.println(x);
for (int i=0; i<x; i++) {
digitalWrite(stepPinX, LOW);
digitalWrite(stepPinX, HIGH);
delayMicroseconds(200);
}
for (int j=0; j<y; j++) {
digitalWrite(stepPinY, LOW);
digitalWrite(stepPinY, HIGH);
digitalWrite(stepPinY2, LOW);
digitalWrite(stepPinY2, HIGH);
delayMicroseconds(200);
}
}
while(true);
}
``````

The I thought that if I could run the “X axis” FOR and the “Y axis” FOR at the same time I will solve the problem. Is there anyway to do it?

Thanks

What do these connect to:
dirPinX
(no dirPinX2?)
dirPinY
dirPinY2
stepPinX
(no stepPinX2?)
stepPinY
stepPinY2

]What do these connect to:
dirPinX
(no dirPinX2?)
dirPinY
dirPinY2
stepPinX
(no stepPinX2?)
stepPinY
stepPinY2

I have 3 step motors, 1 for the x axis and 2 for the y axis. Each step motor needs a direction and step pin.

You can't run two for loops at the same time. You need one loop that makes the motors keep stepping until the complete motion has been completed. Keep in mind that only one motor can step at a time. There are plenty of examples around for driving two stepper motors simultaneously (involving making each complete only a tiny fraction of the overall motion on each loop iteration). Time (for you) to do some searching.

Perhaps start with looking for Grumpy Mike's web site. I know he's posted code to drive two motors at once.

Thanks PaulS, I am going to have a look on the website