Hi - I have a dead end that I'd like to find a way around, but anything I think of adds a lot of code.
I have two stepper motors - while one spins constantly in a single direction, the other strafes back and forth. There are numerous 'blocks' of commands in arrays that run sequentially.
void blockWinder() {
s2_aCon(); // this performs math and populates the arrays with speeds and destinations for the steppers.
stepper1.enableOutputs();
stepper2.enableOutputs();
s2_zero(); // this moves the strafing motor to an end stop and sets the position to 0.
for (int i = 0; i < 20; i ++) { // there are 20 'blocks' in the arrays
stepper1.setCurrentPosition(0); // stepper one moves in one direction until finished, and starts again on the next block
stepper1.setMaxSpeed(MaxSpeed1);
stepper1.setAcceleration(Accel1);
stepper2.setMaxSpeed(MaxSpeed2[i]); //variable speed for stepper 2
stepper2.setAcceleration(MAX_XCL);
stepper2.moveTo(Move2_A[i]); // position A is the starting place for the strafing motion
stepper1.moveTo(Move1[i]); // each block's number of winds differs
/* *** RUNNER *** */
while (stepper1.distanceToGo() > 0) {
stepper1.run();
if (stepper2.currentPosition() == Move2_A[i]) {stepper2.moveTo(Move2_B[i]);}
if (stepper2.currentPosition() == Move2_B[i]) {stepper2.moveTo(Move2_A[i]);}
stepper2.run();
};
/* *** RUNNER *** */
};
stepper2.moveTo(0);
stepper2.runToPosition();
windStatus = false;
stepper1.disableOutputs();
stepper2.disableOutputs();
}
void loop() {
while (windStatus == true) {
// blockWinder();
}
}
OK - sorry for the pile of crap, but the problem area is in the RUNNER part up there. It constricts the loop to run out each block's # of winds on stepper1, whilst strafing back and forth with stepper2, then moving to the next block.
My issue is with this:
if (stepper2.currentPosition() == Move2_A[i]) {stepper2.moveTo(Move2_B[i]);}
if (stepper2.currentPosition() == Move2_B[i]) {stepper2.moveTo(Move2_A[i]);}
Move2_A and Move2_B are derived from 6 possible locations, all using the same 0 point established at the beginning of the function for stepper2.
Sometimes, the stepper motor starts overrunning the bounds established when there's a block change - I'm guessing it's getting confused when it hits a step location at the end of a movement and sets that same location on the new block and accepts a command to move the other way.
I would like to avoid having to reset my 0 point for every block because it's a lot of calculations every time the block changes, so I'm just wondering how I can get the strafing action between the two defined points without relying on the method above.
Sorry if this is really incoherent, I can post the whole code below.