Hello, I need help with Bresenhem's line alghorithm to move steppers. Here is my code -
//digital pin 3 and 5 are x and y stepper direction pin.
//digital pin 2 and 4 are x and y stepper pulse pin.
// i am using tb6600 stepper driver and using arduino uno as microcontroller.
void coordinated_Stepper_Movement(int newx, int newy) {
if (newx < 0) {
digitalWrite(3, LOW);
}
else {
digitalWrite(3, HIGH);
}
if (newy < 0) {
digitalWrite(5, LOW);
}
else {
digitalWrite(5, HIGH);
}
int xi = abs(newx);
int yi = abs(newy);
int xin = xi * 5;
int yin = yi * 5;
int pk = (2 * yin) - xin;
unsigned long i = 0;
int highest_Distance = 0;
if (xin > yin) {
highest_Distance = xin;
}
else {
highest_Distance = yin;
}
for (; i <= highest_Distance ;) {
if (xin < yin) {
if (pk < 0) {
digitalWrite(4, HIGH);
digitalWrite(4, LOW);
pk = pk + (2 * xin);
}
else {
digitalWrite(2, HIGH);
digitalWrite(2, LOW);
digitalWrite(4, HIGH);
digitalWrite(4, LOW);
pk = pk + (2 * xin) - (2 * yin);
}
} else {
if (pk < 0) {
digitalWrite(2, HIGH);
digitalWrite(2, LOW);
pk = pk + (2 * yin);
}
else {
digitalWrite(2, HIGH);
digitalWrite(2, LOW);
digitalWrite(4, HIGH);
digitalWrite(4, LOW);
pk = pk + (2 * yin) - (2 * xin);
}
}
i++;
}
}
First 2 points or coordinates are always the current position. So did not put starting coordinates there as it is always 0. Is the above code having any problem? i tried this in steppers, but the design or anything i draw always shift or does not come perfect. There is always some shift in the design. Btw i am using this to make diy computerized embroidery machine for my mom. the decoded data which comes to newx and newy are correct, i have tested it in c# by drawing the design. above function moves stepper 1 stitch only and i am ignoring z axis, its basically like pen plotter. I am new to this please help.
Update : Problem solved here is the updated code
void coordinated_Stepper_Movement(int newx, int newy) {
if (newx < 0) {
digitalWrite(3, LOW);
}
else {
digitalWrite(3, HIGH);
}
if (newy < 0) {
digitalWrite(5, LOW);
}
else {
digitalWrite(5, HIGH);
}
int xi = abs(newx);
int yi = abs(newy);
int xin = xi * 15;
int yin = yi * 15;
int pk = (2 * yin) - xin;
int highest_Distance = 0;
if (xin > yin) {
highest_Distance = xin;
}
else {
highest_Distance = yin;
}
for (unsigned long i = 0; i <= highest_Distance ; i++) {
if (xin < yin) {
if (pk < 0) {
digitalWrite(4, HIGH);
//adding delay here solved the problem
delayMicroseconds(500);
digitalWrite(4, LOW);
//adding delay here solved the problem
delayMicroseconds(500);
pk = pk + (2 * xin);
}
else {
digitalWrite(2, HIGH);
//adding delay here solved the problem
delayMicroseconds(500);
digitalWrite(2, LOW);
//adding delay here solved the problem
delayMicroseconds(500);
digitalWrite(4, HIGH);
//adding delay here solved the problem
delayMicroseconds(500);
digitalWrite(4, LOW);
//adding delay here solved the problem
delayMicroseconds(500);
pk = pk + (2 * xin) - (2 * yin);
}
} else {
if (pk < 0) {
digitalWrite(2, HIGH);
//adding delay here solved the problem
delayMicroseconds(500);
digitalWrite(2, LOW);
//adding delay here solved the problem
delayMicroseconds(500);
pk = pk + (2 * yin);
}
else {
digitalWrite(2, HIGH);
//adding delay here solved the problem
delayMicroseconds(500);
digitalWrite(2, LOW);
//adding delay here solved the problem
delayMicroseconds(500);
digitalWrite(4, HIGH);
//adding delay here solved the problem
delayMicroseconds(500);
digitalWrite(4, LOW);
//adding delay here solved the problem
delayMicroseconds(500);
pk = pk + (2 * yin) - (2 * xin);
}
}
// I was only adding delay here that is after the for loop. i added more delay,
// the machine or for now the plotter slowed down but had same problem.
// i should have added delay above between high and low signal to increase the pulse
// width so that stepper driver could take it.
delayMicroseconds(500);
}
}