I'm using an Uno and two stepper(X and R) connected to a CNC shield for bench testing (A different shield is used on the prototype machine but that is not the issue)
My intent is to run a "sled" (X) back and forth, from position 0->500->0->500 and so on and at the same time have a rotating arm (R) moving from position 0->300->0->300 and so on BUT, I want R to only move at the end of the sled stroke, currently set to start when the sled has 150 steps left to move before it's endpoint.
See below for youtube video of my prototype machine, Serial printout and source code. I'm open to any suggestion on this.
Regards,
Emil
This video show a prototype of the machine I'm working on where I have one stepper moving at the time in sequence(with no issues), but I want the rotation of the arm to start it's movement earlier with acceleration, before the sled reach its endpoint Prototype machine
It starts off right but when the sled hit 500 and turn, the R stepper loose it's target.
I added a while(1) to stop the loop after one cycle to check the serial printout for troubleshooting and the R starts fine...
.
.
.
2:37:48.784 -> X steps to go 150
22:37:48.784 -> R steps to go 0
22:37:48.784 -> X steps to go 149
22:37:48.784 -> R steps to go 0
22:37:48.784 -> R starts ---------------------------------
22:37:48.784 -> X steps to go 148
22:37:48.784 -> R steps to go 299
22:37:48.824 -> X steps to go 147
22:37:48.824 -> R steps to go 299
22:37:48.824 -> X steps to go 146
22:37:48.824 -> R steps to go 299
22:37:48.824 -> X steps to go 145
22:37:48.824 -> R steps to go 299
Then, when X get to 0 then R jumps from 200 steps left to go to -101 to go. (I have tried different speeds, accelerations, endpoint targets and get similar issue but different values.)
.
.
.
.
22:37:50.844 -> X steps to go 1
22:37:50.884 -> R steps to go 201
22:37:50.884 -> X steps to go 1
22:37:50.884 -> R steps to go 201
22:37:50.884 -> X steps to go 0
22:37:50.884 -> R steps to go 200
22:37:50.884 -> R starts ---------------------------------
22:37:50.884 -> X steps to go -500
22:37:50.884 -> R steps to go -101
22:37:50.884 -> X steps to go -500
22:37:50.884 -> R steps to go -101
22:37:50.884 -> X steps to go -500
22:37:50.884 -> R steps to go -102
22:37:50.925 -> X steps to go -500
22:37:50.925 -> R steps to go -102
22:37:50.925 -> X steps to go -500
My guess is that I'm not using the library correctly for multiple steppers, or I have "found" a bug or a limitation?
#include <AccelStepper.h>
// Stepper Driver pins
const byte XstepPin = 2; // Stepper motor for slide(X)
const byte XdirectionPin = 5;
const byte RstepPin = 3; // Stepper motor for rotating arm(R)
const byte RdirectionPin = 6;
const byte enablePin = 8; // Enable pin for both drivers
const int xMax = 500; // End position in steps for X
const int rMax = 300; // End position in steps for R
const int rStartPoint = 150; // R starts when X have rStartPoint steps left to reach its endpoint
byte xDirection = 0; // Direction of x(0 = going Right, 1 = going left)
// Define steppers (Mode, Step Pin, Direction Pin)
AccelStepper stepperX(AccelStepper:: DRIVER, XstepPin, XdirectionPin); // Slider stepper
AccelStepper stepperR(AccelStepper:: DRIVER, RstepPin, RdirectionPin); // Rotationg arm stepper
void setup()
{
Serial.begin(57600);
pinMode(enablePin, OUTPUT); // Setting up enable pin(LOW active, HIGH off)
digitalWrite(enablePin, LOW); // Set steppers to active
stepperX.setMaxSpeed(500); // Max speed for X stepper
stepperX.setAcceleration(100); // Acceleration for R stepper
stepperR.setMaxSpeed(100 ); // Max speed for R stepper
stepperR.setAcceleration(50); // Acceleration for R stepper
stepperX.moveTo(xMax); // Set initial target for slide, to move right (X stepper)
}
void loop()
{
// Moving X to the Right ------------------------------------------------------------------------------------------------
while (stepperX.distanceToGo() != 0) { // Move side if distance to go is more then 0
if (stepperX.distanceToGo() < rStartPoint && xDirection == 0) { // Set goal for R steper when slide is close to endpoint
xDirection = 1;
Serial.println("R starts ---------------------------------"); // For trouble shooting
stepperR.moveTo(rMax);
}
stepperX.run(); // Run steppers, X always, R only if it has a new goal
stepperR.run();
currentPositions();
}
stepperX.moveTo(0); // Set new target for slide, to move left (X stepper)
// Moving X to the Right ------------------------------------------------------------------------------------------------
while (stepperX.distanceToGo() != 0) { // Move side if distance to go is more then 0
if (stepperX.distanceToGo() < rStartPoint && xDirection == 1) { // Set goal for R steper when slide is close to endpoin
xDirection = 0;
Serial.println("R starts ---------------------------------"); // For trouble shooting
stepperR.moveTo(0);
}
stepperX.run();
stepperR.run();
currentPositions();
}
digitalWrite(enablePin, HIGH); // Disable the stepper dirvers if trouble shooting
while (1); // Stop here, used for trouble shooting
stepperX.moveTo(xMax); // Set new target for slide, to move left (X stepper)
}
// Trouble shooting printouts of distances to go
void currentPositions() {
Serial.print("X steps to go ");
Serial.println(stepperX.distanceToGo());
Serial.print("R steps to go ");
Serial.println(stepperR.distanceToGo());
}

