Plotting Circles with Steppers

i am using two stepper motors and two drives if we give pulse to the driver stepper motor will rotate
but problem how to run two motors at a same time to draw a xy plot like drawing circle.

i used one code but it is rotate like Diamond shape

this is my code

#define motor1pul 2
#define motor1dir 3
#define motor2pul 4
#define motor2dir 5     // 2, 3 first motor puls and direc
void setup() 
{
 
 pinMode(motor1pul, OUTPUT);
 pinMode(motor1dir, OUTPUT);
 pinMode(motor2pul, OUTPUT);
 pinMode(motor2dir, OUTPUT);
}


void loop() 
{
             digitalWrite(motor1dir, HIGH); 
             digitalWrite(motor2dir, HIGH); 
             
             for(i=0; i<12000; i++)
             {
             digitalWrite(motor1pul, HIGH); 
             digitalWrite(motor2pul, HIGH);  
             delay(1); 
             digitalWrite(motor1pul, LOW);
             digitalWrite(motor2pul, LOW);             
             delay(1);             
           }

           digitalWrite(motor1dir, HIGH); 
           digitalWrite(motor2dir, LOW);
           for(i=0; i<12000; i++)
             {
             digitalWrite(motor1pul, HIGH); 
             digitalWrite(motor2pul, HIGH);  
             delay(1); 
             digitalWrite(motor1pul, LOW);
             digitalWrite(motor2pul, LOW);             
             delay(1);             
           }

           digitalWrite(motor1dir, LOW); 
           digitalWrite(motor2dir, LOW);
           for(i=0; i<12000; i++)
             {
             digitalWrite(motor1pul, HIGH); 
             digitalWrite(motor2pul, HIGH);  
             delay(1); 
             digitalWrite(motor1pul, LOW);
             digitalWrite(motor2pul, LOW);             
             delay(1);             
           }

           digitalWrite(motor1dir, LOW); 
           digitalWrite(motor2dir, HIGH);
           for(i=0; i<12000; i++)
             {
             digitalWrite(motor1pul, HIGH); 
             digitalWrite(motor2pul, HIGH);  
             delay(1); 
             digitalWrite(motor1pul, LOW);
             digitalWrite(motor2pul, LOW);             
             delay(1);             
           }

}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

please rectify this code

Nah, it's your job to rectify it. Use the sin() and cos() functions to make a circle.

give some examples using sin() and cos functions please

Is it the sin() and cos() functions that you need help with (Google is your friend) or using them to draw a circle (Google is your friend again) ?

To draw a circle (or anything other than a straight line) your code needs to interleave the steps of the two motors. For example to draw a line at 45 deg each motor must take 1 step in turn.

From a brief look at your code you have for(i=0; i<12000; i++) which will (I assume) make one motor do 12,000 steps while the other does nothing. A completely different approach is needed in which there is one repeating loop that deals with both motors.

If you want to follow a straight line that is not at 45 deg you will (perhaps) need to move one motor by one step for every 3 steps the other one takes.

If you want to follow a complex path such as a circle the amount one motor moves relative to the other will need to change continually.

It may be worth looking up Bressingham's Bresenham's algorithm. I think he figured out an algorithm for circles as well as straight lines.

...R

Robin2:
It may be worth looking up Bressingham's algorithm.

Bresenham I believe. But it's probably over the OP's head. Wikipedia has a good section on different line and circle algorithms (including the butt simple ones).

aarg:
Bresenham I believe.

Thanks. I have corrected my earlier Reply.

...R

To draw a quarter circle, starting at (100,0), ending at (0,100), centered on (0,0):

x = 100;
y = 0;

move_x_stepper_to(x);
move_y_stepper_to(y);

while(x !=0 || y != 100) {
  if(x ==0) {
    move_y_stepper_to(++y);
  }
  else if(y==0) {
    move_x_stepper_to(--x);
  }
  else {
    // this would be more accurate if we took the square root, but meh. I think this will be ok.

    long error_moving_x = abs( ((long)x-1)*((long)x-1) + (long)y*(long)y - 100L*100L ); 
    long error_moving_y = abs( ((long)x)*((long)x) + ((long)y+1)*((long)y+1) - 100L*100L); 

    if(error_moving_x < error_moving_y) {
      move_x_stepper_to(--x);
    }
    else {
      move_y_stepper_to(++y);
    }
  }
}

See the algorithm? "I can move x or y this step, so work out which one would be closer to the circle I am trying to draw and move that one".

For more accuracy, use floating point and square root:

    float error_moving_x = fabs( sqrt(((float)x-1)*((float)x-1) + (float)y*(float)y) - 100 );

Naturally, you need to turn this inside-out to make it work in a loop().

PaulMurrayCbr:
To draw a quarter circle,

Thanks. I have bookmarked that.

...R