CNC Easydriver stepper motor draw circle problem

Hi CNC and arduino expert. I have problem with my code. I could not draw circle but instead i get weird shape. .. Can anyone check whats wrong with my code.... to much coding..hmpph.. ( I don't want to use any library, GRBL, GCODE intreprete, no Brensenham),only Arduino). Moving in x y at the same time, i don't have any problem, but if drawing circle ( im using movexy to draw circular graph) and divide the circle into some section (division) for smoother circle but got weird shape. Here is the code

int ix,iy,ixx,iyy,iyold,ixold, delayx;
float xf,yf,ixf,iyf,iyyf,ifl,directionx,directiony;
 void circle(int div,float delaycirc, float rad){  // (division(more=smooth), delay, radius)
  float xxold=0;
  float yyold=0;
  for(int idiv=0; idiv<=(div); idiv++){
    float divfloat=idiv;
    float xx=(cos(2*PI/div*divfloat)-0)*rad; int x=abs(xx-xxold); xxold=xx;
    float yy=(sin(2*PI/div*divfloat)-0)*rad; int y=abs(yy-yyold); yyold=yy;
  if( xx<0){directionx=1;}else{directionx=0;}
  if( yy<0){directiony=1;}else{directiony=0;}
   movexy(x,y,delaycirc, directionx, directiony);
 }
  delay(1000);
 }
void setup(){}
void loop(){
circle(360, 100, 10000); // (360 section, 100 delay. radius of 10000  step)}
void movexy(float xf, float yf, int delaxy, int directionx, int directiony){  
   iyold=0;
   ixold=0;
   if(xf>=yf){
   for(ifl=1; ifl<=(xf+1); ifl++){
      iyf=ifl*(yf/xf);
      if(round(iyf)>iyold){
        digitalWrite(8, directiony);
        digitalWrite(9, HIGH);}
  digitalWrite(6, directionx);
  digitalWrite(7, HIGH);
  delayMicroseconds(delaxy);
      if(round(iyf)>iyold){
         digitalWrite(9, LOW); 
         iyold=iyold+1;}  
  digitalWrite(7, LOW); 
  delayMicroseconds(delaxy); }}
  else{
     for(ifl=1; ifl<=(yf+1); ifl++){
      ixf=ifl*(xf/yf);
      if(round(ixf)>ixold){
        digitalWrite(6, directionx);
        digitalWrite(7, HIGH);}
  digitalWrite(8, directiony);
  digitalWrite(9, HIGH);
  delayMicroseconds(delaxy);
      if(round(ixf)>ixold){
         digitalWrite(7, LOW); 
         ixold=ixold+1;}  
  digitalWrite(9, LOW); 
  delayMicroseconds(delaxy);   
      }}
}

From your video your program seems to be drawing quarter circles very nicely - just in the wrong place.

...R

yup. drawing 360 lines... but anyway i already figure it out the culprit behind this using EXCEL. But i need to test it first whether it is okay or not. Need to wait for next Monday though. Feel free to use my code ,if it works :~

This is broken:

void loop(){
circle(360, 100, 10000); // (360 section, 100 delay 10000 step)}
void movexy(float xf, float yf, int delaxy, int directionx, int directiony){

You've commented out the close-curly for loop(). Please post the code you are actually
using, not something untested.

Your code lacks enough whitespace to be readable, you have multiple statements
on one line, close-curlies not on a separate line - all makes it hard to read for everyone.

Having said that its clear that

  1. You haven't called pinMode() on your output pins
  2. Your method is way more complicated than it needs to be.

This is how I would iterate a circle if I didn't know about Bressenhams.

  int cur_x = radius ;
  int cur_y = 0 ;
  float two_pi = 2.0 * PI ;
  float anglestep = 0.99 / radius ;
  for (float angle = 0.0 ; angle < two_pi ; angle += anglestep)
  {
    float x = radius * cos (angle) ;
    float y = radius * sin (angle) ;
    int  ix = round (x) ;
    int  iy = round (y) ;
    boolean  left = ix < cur_x ;
    boolean  right = ix > cur_x ;
    boolean  up = iy > cur_y ;
    boolean  down = iy < cur_y ;
    // here step the motors as required, at most one of left & right will be true
    // at most one of up & down will be true.
    // for each step made adjust cur_x or cur_y appropriately.
  }

ok. The code is too long to be posted. So i cut it. Actually i already found the culprit/bug in my codes and i will try it out soon
ok, this is the real one but never test it (will test later due to holiday)

int ix,iy,ixx,iyy,iyold,ixold, delayx;
float xf,yf,ixf,iyf,iyyf,ifl,directionx,directiony;
 void circle(int div,float delaycirc, float rad){  // (division(more=smooth), delay, radius)
  float xxold=0;
  float yyold=0;
  for(int idiv=0; idiv<=(div); idiv++){
    float divfloat=idiv;
    float xx=(cos(2*PI/div*divfloat)-0)*rad; int x=abs(xx-xxold); xxold=xx;
    float yy=(sin(2*PI/div*divfloat)-0)*rad; int y=abs(yy-yyold); yyold=yy;
  if( xx<0){directionx=1;}else{directionx=0;}
  if( yy<0){directiony=1;}else{directiony=0;}
   movexy(x,y,delaycirc, directionx, directiony);
 }
  delay(1000);
 }
void setup(){Serial.begin(9600);   pinMode(8, OUTPUT);   pinMode(9, OUTPUT); pinMode(6, OUTPUT);     pinMode(7, OUTPUT);
  pinMode(Had1,INPUT);  pinMode(Had2,INPUT);  pinMode(Had3,INPUT);  pinMode(Had4,INPUT);
  digitalWrite(8, LOW);  digitalWrite(9, LOW);  digitalWrite(6, LOW);  digitalWrite(7, LOW);}
void loop(){
circle(360, 100, 10000); // (360 section, 100ms delay. radius of 10000  step)}
void movexy(float xf, float yf, int delaxy, int directionx, int directiony){  
   iyold=0;
   ixold=0;
   if(xf>=yf){
   for(ifl=1; ifl<=(xf+1); ifl++){
      iyf=ifl*(yf/xf);
      if(round(iyf)>iyold){
        digitalWrite(8, directiony);
        digitalWrite(9, HIGH);}
  digitalWrite(6, directionx);
  digitalWrite(7, HIGH);
  delayMicroseconds(delaxy);
      if(round(iyf)>iyold){
         digitalWrite(9, LOW); 
         iyold=iyold+1;}  
  digitalWrite(7, LOW); 
  delayMicroseconds(delaxy); }}
  else{
     for(ifl=1; ifl<=(yf+1); ifl++){
      ixf=ifl*(xf/yf);
      if(round(ixf)>ixold){
        digitalWrite(6, directionx);
        digitalWrite(7, HIGH);}
  digitalWrite(8, directiony);
  digitalWrite(9, HIGH);
  delayMicroseconds(delaxy);
      if(round(ixf)>ixold){
         digitalWrite(7, LOW); 
         ixold=ixold+1;}  
  digitalWrite(9, LOW); 
  delayMicroseconds(delaxy);   
      }}
}

Finally finish the circle, even can create quarter circle..tq everybody

Good to see it working!

MarkT:
This is broken:

void loop(){

circle(360, 100, 10000); // (360 section, 100 delay 10000 step)}
void movexy(float xf, float yf, int delaxy, int directionx, int directiony){



You've commented out the close-curly for loop(). Please post the code you are actually
using, not something untested.

Your code lacks enough whitespace to be readable, you have multiple statements
on one line, close-curlies not on a separate line - all makes it hard to read for everyone.

Having said that its clear that 

1) You haven't called pinMode() on your output pins
2) Your method is way more complicated than it needs to be.

This is how I would iterate a circle if I didn't know about Bressenhams.



int cur_x = radius ;
  int cur_y = 0 ;
  float two_pi = 2.0 * PI ;
  float anglestep = 0.99 / radius ;
  for (float angle = 0.0 ; angle < two_pi ; angle += anglestep)
  {
    float x = radius * cos (angle) ;
    float y = radius * sin (angle) ;
    int  ix = round (x) ;
    int  iy = round (y) ;
    boolean  left = ix < cur_x ;
    boolean  right = ix > cur_x ;
    boolean  up = iy > cur_y ;
    boolean  down = iy < cur_y ;
    // here step the motors as required, at most one of left & right will be true
    // at most one of up & down will be true.
    // for each step made adjust cur_x or cur_y appropriately.
  }

Hello Mark, Can you please explain to me what the cur_x is and the cur_y is? also what do the boolean statements do?

Can you please explain to me what the cur_x is and the cur_y is?

Just a guess, but how about "current x position" and "current y position"?

also what do the boolean statements do?

In the code you quoted, absolutely nothing.

sorry its my first time here

boolean left = ix < cur_x ;
boolean right = ix > cur_x ;
boolean up = iy > cur_y ;
boolean down = iy < cur_y ;

in his code, he uses these statements, what does mean? I guess i'm finding it a bit difficult to understand how these would move the stepper motors individually?

if I had a stepper motor 1 and stepper motor two how do I correspond the code written by mark to move both the motors?

That code, by itself, does absolutely nothing, and the compiler may even optimise it away, because the variables are never used.

Please use code tags when posting code.

can you please tell me how I would implement the variables?

MarkT:

  int cur_x = radius ;

int cur_y = 0 ;
  float two_pi = 2.0 * PI ;
  float anglestep = 0.99 / radius ;
  for (float angle = 0.0 ; angle < two_pi ; angle += anglestep)
  {
    float x = radius * cos (angle) ;
    float y = radius * sin (angle) ;
    int  ix = round (x) ;
    int  iy = round (y) ;
    boolean  left = ix < cur_x ;
    boolean  right = ix > cur_x ;
    boolean  up = iy > cur_y ;
    boolean  down = iy < cur_y ;
    // here step the motors as required, at most one of left & right will be true
    // at most one of up & down will be true.
    // for each step made adjust cur_x or cur_y appropriately.
  }

include <Stepper.h>

const int step_360 = 200;// 360 number of steps per/rev  
const int step_180=100;            
const int step_540= 300;  
const int step_720=400;       

void setup() 
{
 // set the speed at 60 rpm:
 myStepper1.setSpeed(60);//left
 myStepper2.setSpeed(60);//right
 // initialize the serial port:
 Serial.begin(9600);
}
void loop() 
{
for(int s=0; s<step_360; s++)
{
 myStepper1.step(1);
 myStepper2.step(1);
}
 

}

this is the code I have so far that will make both motors move at the same time, but how do I implement marks code in my code?

In case it helps your understanding this

boolean  left = ix < cur_x ;

has the effect of making the variable left true if ix < cur_x and otherwise it will be false

how do I implement marks code in my code?

I have no idea how to answer that question. You need to explain what you want your program to do.

If you try to implement it and post your best attempt it will be easier to help.

...R

Robin2:
I have no idea how to answer that question. You need to explain what you want your program to do.

If you try to implement it and post your best attempt it will be easier to help.

...R

So basically what I am trying to achieve is to turn two stepper motors attached to an etch a sketch which should create a circle using code on arduino. So far I have got both stepper motors to move at the same time

sihjcvb:

include <Stepper.h>

const int step_360 = 200;// 360 number of steps per/rev 
const int step_180=100;           
const int step_540= 300; 
const int step_720=400;

void setup()
{
// set the speed at 60 rpm:
myStepper1.setSpeed(60);//left
myStepper2.setSpeed(60);//right
// initialize the serial port:
Serial.begin(9600);
}
void loop()
{
for(int s=0; s<step_360; s++)
{
myStepper1.step(1);
myStepper2.step(1);
}

}

I was wondering how I can use Mark's code in my code?

What does Mark's code do?

Have you tried to incorporate it?

You may find it easier to figure out a solution yourself rather than trying to understand and adapt another person's code.

...R

Yes I have tried to implement it, But I can't seem to figure out a way to transform the booleans statements into movements that will make the motor move

Well, show us what you've tried.

The discussion seems to have moved to the OP's own Thread (where it probably should have been all along). Maybe this one should now be locked. The earlier stuff is old.

...R