Odd increment

OK, so I have this code I am making for my XY table. It works, however the Y motor seems to increment for no specific reason.

For example, if I send <50,50>, its fine.
Then send <1,1>, it goes back and lines up
Send <50,50> again, it goes back
Then <1,1>, lines up
<50,50> again
Then, when I type <1,1> again, it doesn’t light up any longer, it’s slightly lower.
Back to <50,50>
Then to <1,1> again, its slightly further out of alignment
And just keeps going further out of alignment from there.

This appears to be only happening for the Y axis, and I can’t seem to find the problem.

Here is my code:

int currentx;
int currenty;
int pos = 0;
int pos2 = 0;

#include <Stepper.h>

// change this to the number of steps on your motor
#define STEPS 200

// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepperx(STEPS, 2, 3, 4, 5);
Stepper steppery(STEPS, 6, 7, 8, 9);


void setup()
{
  Serial.begin(9600);
  stepperx.setSpeed(50);
  steppery.setSpeed(50);
}
void loop()
{
 if(Serial.available() > 0)
  {
    char aChar = Serial.read();
    if(aChar == '<')
    {
        pos = 0;
        do {
          while(Serial.available() <1); // wait for a character
          aChar = Serial.read();
        if(aChar >= '0' && aChar <= '9')
          pos = pos * 10 + aChar - '0';
      } while(aChar != ',');
        pos2 = 0;
        do {
          while(Serial.available() <1); // wait for a character
          aChar = Serial.read();
        if(aChar >= '0' && aChar <= '9')
          pos2 = pos2 * 10 + aChar - '0';
      } while(aChar != '>');
    }
  }
  if(currentx != pos){
    if(currentx > pos){
      stepperx.step((pos - currentx));
    }
    if(currentx < pos){
      stepperx.step((pos + currentx));
    }
    currentx = pos;
  }
    if(currenty != pos2){
      if(currenty > pos2){
      steppery.step((pos2 - currenty));
    }
    if(currenty < pos2){
      steppery.step((pos2 + currenty));
    }
    currenty = pos2;
    Serial.println(currenty);
  }
}

Cheers,
Dan

One of the issues with stepper motors, especially if you are trying to move them at speed, is that they are not highly repeatable. To get the accuracy needed for an CNC machine, you will need rotary encoders on the shafts, to assure that that motor HAS actually stepped the correct amount.

If you rename stepperx to steppery and rename steppery to stepperx, leaving the pin values as they are, does the non-repeatability swap to the x axis. If so, it's a code issue. If not, it's a hardware issue.

Swapping stepperx and steppery does change the problem to the other stepper. I still can't seem to find the problem though.

I don’t know. My eyes cross trying to read your code. Your indenting is terrible. The difference between pos and pos2 is relatively easy to see, but the difference between currentx and currenty is not.

There is a reason why variable are generally camelCase, and short. currX and currY are easier to distinguish. Consistency is also good. oldX and newX versus currentx and pos.

Adding an extra variable, deltaX in the X block (and deltaY in the Y block) would make it easier to see where the difference is.

Any better?

int oldX;
int oldY;
int newX = 0;
int newY = 0;

#include <Stepper.h>

// change this to the number of steps on your motor
#define STEPS 200

// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepperx(STEPS, 2, 3, 4, 5);
Stepper steppery(STEPS, 6, 7, 8, 9);


void setup()
{
  Serial.begin(9600);
  stepperx.setSpeed(50);
  steppery.setSpeed(50);
}
void loop()
{
 if(Serial.available() > 0)
  {
    char aChar = Serial.read();
    if(aChar == '<')
    {
        newX = 0;
        do {
          while(Serial.available() <1); // wait for a character
          aChar = Serial.read();
          if(aChar >= '0' && aChar <= '9')
          newX = newX * 10 + aChar - '0';
            }
          while(aChar != ',');
        newY = 0;
        do {
          while(Serial.available() <1); // wait for a character
          aChar = Serial.read();
          if(aChar >= '0' && aChar <= '9')
          newY = newY * 10 + aChar - '0';
            }
          while(aChar != '>');
          }
}
  if(oldX != newX)
  {
    if(oldX > newX)
    {
      stepperx.step((newX - oldX));
    }
    if(oldX < newX)
    {
      stepperx.step((newX + oldX));
    }
    oldX = newX;
}
    if(oldY != newY)
    {
      if(oldY > newY)
      {
      steppery.step((newY - oldY));
}
    if(oldY < newY)
    {
      steppery.step((newY + oldY));
    }
    oldY = newY;
  }
}

Actually, it seems to be a problem with both steppers. I think what is happening, is when I set them to <1,1>, oldX and oldY becomes 1, then it adds on the next value, and it accumulates. Let me try remove the 1’s.

EDIT: Yep, that was the problem :wink:

Found another interesting bug though.

If I start with <50,50>, they move 90 degrees
Then if I do <100,100>, they should only move another 50 steps to get to 100, but instead they seem to move 150 steps.

EDIT: Figured that out too :slight_smile:

EDIT: Figured that out too

Excellent. While you were doing that I was making pies and rolls and cornbread and getting the turkeys out of the brine.